From 0ff3ddddba13f737c82ac88091d78f7c337ad709 Mon Sep 17 00:00:00 2001 From: Emmanuel Bouton Date: Thu, 31 Jul 2014 15:14:01 +0200 Subject: [PATCH] Add isdefault to environment --- data/migrations/mysql/schema.sql | 3 +- data/migrations/postgresql/schema.sql | 3 +- data/migrations/sqlite/schema.sql | 3 +- .../Model/EnvironmentHydrator.php | 2 ++ .../Service/EnvironmentRepository.php | 5 +++ .../Model/EnvironmentHydratorTest.php | 9 +++-- .../Service/EnvironmentRepositoryTest.php | 8 +++-- test/data/fixtures.xml | 36 +++++++++---------- 8 files changed, 44 insertions(+), 25 deletions(-) diff --git a/data/migrations/mysql/schema.sql b/data/migrations/mysql/schema.sql index 8c2191c..392682b 100644 --- a/data/migrations/mysql/schema.sql +++ b/data/migrations/mysql/schema.sql @@ -10,7 +10,8 @@ CREATE TABLE `users` ( DROP TABLE IF EXISTS `environments`; CREATE TABLE `environments` ( `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, - `name` VARCHAR(256) + `name` VARCHAR(256), + `isdefault` TINYINT NOT NULL DEFAULT 0 ); DROP TABLE IF EXISTS `environments_paths`; diff --git a/data/migrations/postgresql/schema.sql b/data/migrations/postgresql/schema.sql index 890b230..1143a74 100644 --- a/data/migrations/postgresql/schema.sql +++ b/data/migrations/postgresql/schema.sql @@ -10,7 +10,8 @@ CREATE TABLE users ( DROP TABLE IF EXISTS environments; CREATE TABLE environments ( id SERIAL PRIMARY KEY, - name VARCHAR(256) + name VARCHAR(256), + isdefault SMALLINT NOT NULL DEFAULT 0 ); DROP TABLE IF EXISTS environments_paths; diff --git a/data/migrations/sqlite/schema.sql b/data/migrations/sqlite/schema.sql index 3f3b70b..fcd9205 100644 --- a/data/migrations/sqlite/schema.sql +++ b/data/migrations/sqlite/schema.sql @@ -10,7 +10,8 @@ CREATE TABLE `users` ( DROP TABLE IF EXISTS `environments`; CREATE TABLE `environments` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - `name` VARCHAR(256) + `name` VARCHAR(256), + `isdefault` TINYINT NOT NULL DEFAULT 0 ); DROP TABLE IF EXISTS `environments_paths`; diff --git a/src/KmbZendDbInfrastructure/Model/EnvironmentHydrator.php b/src/KmbZendDbInfrastructure/Model/EnvironmentHydrator.php index 2beb444..6647cf1 100644 --- a/src/KmbZendDbInfrastructure/Model/EnvironmentHydrator.php +++ b/src/KmbZendDbInfrastructure/Model/EnvironmentHydrator.php @@ -38,6 +38,7 @@ public function extract($object) $data['id'] = $object->getId(); } $data['name'] = $object->getName(); + $data['isdefault'] = $object->isDefault() ? 1 : 0; return $data; } @@ -52,6 +53,7 @@ public function hydrate(array $data, $object) { $object->setId($data['id']); $object->setName($data['name']); + $object->setDefault($data['isdefault'] == 1); return $object; } } diff --git a/src/KmbZendDbInfrastructure/Service/EnvironmentRepository.php b/src/KmbZendDbInfrastructure/Service/EnvironmentRepository.php index 5ab83ff..753a651 100644 --- a/src/KmbZendDbInfrastructure/Service/EnvironmentRepository.php +++ b/src/KmbZendDbInfrastructure/Service/EnvironmentRepository.php @@ -74,6 +74,11 @@ function (UserInterface $user) use ($aggregateRoot) { ); $connection = $this->getDbAdapter()->getDriver()->getConnection()->beginTransaction(); try { + $initDefault = $this->getMasterSql()->update($this->getTableName())->set([ + 'isdefault' => 0 + ]); + $this->performWrite($initDefault); + parent::update($aggregateRoot); $delete = $this->getMasterSql()->delete('environments_users'); diff --git a/test/KmbZendDbInfrastructureTest/Model/EnvironmentHydratorTest.php b/test/KmbZendDbInfrastructureTest/Model/EnvironmentHydratorTest.php index 61ec91b..798af0c 100644 --- a/test/KmbZendDbInfrastructureTest/Model/EnvironmentHydratorTest.php +++ b/test/KmbZendDbInfrastructureTest/Model/EnvironmentHydratorTest.php @@ -12,11 +12,13 @@ public function canExtract() $environment = new Environment(); $environment->setId(1); $environment->setName('STABLE'); + $environment->setDefault(true); $hydrator = new EnvironmentHydrator(); $this->assertEquals([ 'id' => 1, - 'name' => 'STABLE' + 'name' => 'STABLE', + 'isdefault' => 1 ], $hydrator->extract($environment)); } @@ -28,7 +30,8 @@ public function canExtractWithNullId() $hydrator = new EnvironmentHydrator(); $this->assertEquals([ - 'name' => 'STABLE' + 'name' => 'STABLE', + 'isdefault' => 0, ], $hydrator->extract($environment)); } @@ -41,9 +44,11 @@ public function canHydrate() $hydratedEnvironment = $hydrator->hydrate([ 'id' => 1, 'name' => 'STABLE', + 'isdefault' => 1 ], $environment); $this->assertEquals(1, $hydratedEnvironment->getId()); $this->assertEquals('STABLE', $hydratedEnvironment->getName()); + $this->assertTrue($hydratedEnvironment->isDefault()); } } diff --git a/test/KmbZendDbInfrastructureTest/Service/EnvironmentRepositoryTest.php b/test/KmbZendDbInfrastructureTest/Service/EnvironmentRepositoryTest.php index a51f23d..ce66e6a 100644 --- a/test/KmbZendDbInfrastructureTest/Service/EnvironmentRepositoryTest.php +++ b/test/KmbZendDbInfrastructureTest/Service/EnvironmentRepositoryTest.php @@ -88,10 +88,13 @@ public function canUpdate() $aggregateRoot->setParent($newParent); $aggregateRoot->setName('PF4'); $aggregateRoot->addUsers([$mike, $nick]); + $aggregateRoot->setDefault(true); static::$repository->update($aggregateRoot); $this->assertEquals('PF4', static::$connection->query('SELECT name FROM environments WHERE id = 4')->fetchColumn()); + $this->assertEquals('1', static::$connection->query('SELECT isdefault FROM environments WHERE id = 4')->fetchColumn()); + $this->assertEquals('0', static::$connection->query('SELECT isdefault FROM environments WHERE id = 3')->fetchColumn()); $this->assertEquals('UNSTABLE', static::$connection->query('select name from environments join environments_paths on id = ancestor_id where length = 1 and descendant_id = 4')->fetchColumn()); $this->assertEquals('PF4', static::$connection->query('select name from environments join environments_paths on id = ancestor_id where length = 1 and descendant_id = 7')->fetchColumn()); $this->assertEquals([[3], [4], [6]], static::$connection->query('SELECT user_id FROM environments_users WHERE environment_id = 4')->fetchAll(\PDO::FETCH_NUM)); @@ -131,9 +134,10 @@ public function cannotGetRootByUnknownRootName() /** @test */ public function canGetRootByName() { - $environment = static::$repository->getRootByName('STABLE'); + $environment = static::$repository->getRootByName('DEFAULT'); - $this->assertEquals(1, $environment->getId()); + $this->assertEquals(3, $environment->getId()); + $this->assertTrue($environment->isDefault()); } /** @test */ diff --git a/test/data/fixtures.xml b/test/data/fixtures.xml index 4a13358..fb94d7e 100644 --- a/test/data/fixtures.xml +++ b/test/data/fixtures.xml @@ -15,24 +15,24 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + +