From 0eb9ec40d8f6faa52e2a8ff00cf0ffd7777c4396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Thu, 2 Jul 2015 21:24:30 +0200 Subject: [PATCH] Add Tests to DataboxRepository --- .../Phrasea/Databox/DataboxFactory.php | 20 +++++ .../Phrasea/Databox/DataboxRepository.php | 9 +- .../Phrasea/Databox/DataboxRepositoryTest.php | 86 ++++++++++++++++++- 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Databox/DataboxFactory.php diff --git a/lib/Alchemy/Phrasea/Databox/DataboxFactory.php b/lib/Alchemy/Phrasea/Databox/DataboxFactory.php new file mode 100644 index 0000000000..0efd408454 --- /dev/null +++ b/lib/Alchemy/Phrasea/Databox/DataboxFactory.php @@ -0,0 +1,20 @@ +app = $app; $this->appbox = $appbox; + $this->factory = $factory ?: new DataboxFactory(); } /** @@ -32,7 +35,7 @@ class DataboxRepository implements DataboxRepositoryInterface public function find($id) { try { - $databox = new \databox($this->app, (int)$id); + $databox = $this->factory->create($this->app, (int)$id); } catch (NotFoundHttpException $exception) { $databox = null; } @@ -65,7 +68,7 @@ class DataboxRepository implements DataboxRepositoryInterface $databoxes = array(); foreach ($rows as $row) { - $databox = new \databox($this->app, (int)$row['sbas_id'], $row); + $databox = $this->factory->create($this->app, (int)$row['sbas_id'], $row); $databoxes[$databox->get_sbas_id()] = $databox; } diff --git a/tests/Alchemy/Tests/Phrasea/Databox/DataboxRepositoryTest.php b/tests/Alchemy/Tests/Phrasea/Databox/DataboxRepositoryTest.php index 4910e25d0f..1d8a189bdb 100644 --- a/tests/Alchemy/Tests/Phrasea/Databox/DataboxRepositoryTest.php +++ b/tests/Alchemy/Tests/Phrasea/Databox/DataboxRepositoryTest.php @@ -10,9 +10,13 @@ namespace Alchemy\Tests\Phrasea\Databox; use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Databox\DataboxFactory; use Alchemy\Phrasea\Databox\DataboxRepository; use Alchemy\Phrasea\Databox\DataboxRepositoryInterface; +use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\Statement; use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; final class DataboxRepositoryTest extends \PHPUnit_Framework_TestCase { @@ -20,6 +24,8 @@ final class DataboxRepositoryTest extends \PHPUnit_Framework_TestCase private $app; /** @var ObjectProphecy */ private $appbox; + /** @var ObjectProphecy */ + private $factory; /** @var DataboxRepository */ private $sut; @@ -28,12 +34,90 @@ final class DataboxRepositoryTest extends \PHPUnit_Framework_TestCase { $this->app = $this->prophesize(Application::class); $this->appbox = $this->prophesize(\appbox::class); + $this->factory = $this->prophesize(DataboxFactory::class); - $this->sut = new DataboxRepository($this->app->reveal(), $this->appbox->reveal()); + $this->sut = new DataboxRepository($this->app->reveal(), $this->appbox->reveal(), $this->factory->reveal()); } public function testItImplementsDataboxRepositoryInterface() { $this->assertInstanceOf(DataboxRepositoryInterface::class, $this->sut); } + + public function testItFindsDataboxProperly() + { + $databox = $this->prophesize(\databox::class); + $this->factory->create($this->app->reveal(), 42)->willReturn($databox->reveal()); + + $this->assertSame($databox->reveal(), $this->sut->find(42)); + } + + public function testItReturnsNullOnNonExistentDatabox() + { + $this->factory->create($this->app->reveal(), 42)->willThrow(new NotFoundHttpException()); + + $this->assertNull($this->sut->find(42)); + } + + public function testItFindsAllDeclaredDataboxes() + { + $databox1 = $this->prophesize(\databox::class); + $databox1->get_sbas_id() + ->willReturn(1); + $databox1_cache = ['sbas_id' => 1, 'foo' => 'bar']; + + $databox2 = $this->prophesize(\databox::class); + $databox2->get_sbas_id() + ->willReturn(2); + $databox2_cache = ['sbas_id' => 2, 'bar' => 'baz']; + + $this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES) + ->willReturn([ + $databox1_cache, + $databox2_cache, + ]); + + $this->factory->create($this->app->reveal(), 1, $databox1_cache) + ->willReturn($databox1->reveal()); + + $this->factory->create($this->app->reveal(), 2, $databox2_cache) + ->willReturn($databox2->reveal()); + + $this->assertEquals([ + 1 => $databox1->reveal(), + 2 => $databox2->reveal(), + ], $this->sut->findAll()); + } + + public function testItFindsAllDeclaredDataboxesWhenNoCacheIsPresent() + { + $this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES) + ->willReturn(false); + + $connection = $this->prophesize(Connection::class); + $this->appbox->get_connection() + ->willReturn($connection->reveal()); + + $statement = $this->prophesize(Statement::class); + $connection->prepare('SELECT sbas_id, ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas') + ->willReturn($statement->reveal()); + + $databox1 = $this->prophesize(\databox::class); + $databox1->get_sbas_id() + ->willReturn(1); + $cache = [['sbas_id' => 1, 'foo' => 'bar']]; + + $statement->execute()->shouldBeCalled(); + $statement->fetchAll(\PDO::FETCH_ASSOC) + ->willReturn($cache); + $statement->closeCursor()->shouldBeCalled(); + + $this->appbox->set_data_to_cache($cache, \appbox::CACHE_LIST_BASES) + ->shouldBeCalled(); + + $this->factory->create($this->app->reveal(), 1, $cache[0]) + ->willReturn($databox1->reveal()); + + $this->assertEquals([1 => $databox1->reveal()], $this->sut->findAll()); + } }