Skip to content

Commit

Permalink
FIX GroupRepository::add() => ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
goten4 committed Nov 3, 2015
1 parent f713e80 commit 9f84cd8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/KmbZendDbInfrastructure/Service/GroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function add(AggregateRootInterface $aggregateRoot)
$result = $this->performRead($select)->current();
$ordering = $result['ordering'] === null ? 0 : $result['ordering'] + 1;
$aggregateRoot->setOrdering($ordering);
} else {
$query = 'UPDATE ' . $this->getTableName() . ' SET ordering = ordering + 1 WHERE ordering >= ? and revision_id = ?';
$statement = $this->getDbAdapter()->query($query);
$statement->execute([$aggregateRoot->getOrdering(), $aggregateRoot->getRevision()->getId()]);
}
parent::add($aggregateRoot);

Expand Down
10 changes: 7 additions & 3 deletions test/KmbZendDbInfrastructureTest/Service/GroupRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function canAdd()
static::$repository->add($group);

$this->assertEquals(8, $group->getId());
$this->assertEquals(4, $group->getOrdering());
$this->assertEquals(3, $group->getOrdering());
$this->assertEquals(12, $group->getClassByName('dns')->getId());
}

Expand All @@ -56,12 +56,16 @@ public function canAddWithOrdering()
{
$revision = Bootstrap::getServiceManager()->get('RevisionRepository')->getById(8);
$group = new Group('new group');
$group->setOrdering(8);
$group->setOrdering(1);
$group->setRevision($revision);

static::$repository->add($group);

$this->assertEquals(8, $group->getOrdering());
$this->assertEquals(9, $group->getId()); // Sequence is not reinit during test setup
$this->assertEquals(1, intval(static::$connection->query('SELECT ordering FROM groups WHERE id = 9')->fetchColumn()));
$this->assertEquals(0, intval(static::$connection->query('SELECT ordering FROM groups WHERE id = 1')->fetchColumn()));
$this->assertEquals(3, intval(static::$connection->query('SELECT ordering FROM groups WHERE id = 2')->fetchColumn()));
$this->assertEquals(2, intval(static::$connection->query('SELECT ordering FROM groups WHERE id = 3')->fetchColumn()));
}

/** @test */
Expand Down
14 changes: 7 additions & 7 deletions test/data/fixtures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@
<revisions_logs id="3" revision_id="8" created_at="2014-09-10 10:05:15" created_by="John DOE" comment="Update group default" />

<!-- RELEASE -->
<groups id="1" revision_id="8" name="default" include_pattern=".*" exclude_pattern="" ordering="1" />
<groups id="2" revision_id="8" name="proxy" include_pattern="" exclude_pattern="" ordering="3" />
<groups id="3" revision_id="8" name="web" include_pattern="" exclude_pattern="" ordering="2" />
<groups id="1" revision_id="8" name="default" include_pattern=".*" exclude_pattern="" ordering="0" />
<groups id="2" revision_id="8" name="proxy" include_pattern="" exclude_pattern="" ordering="2" />
<groups id="3" revision_id="8" name="web" include_pattern="" exclude_pattern="" ordering="1" />
<!-- CURRENT -->
<groups id="4" revision_id="9" name="default" include_pattern=".*" exclude_pattern="" ordering="1" />
<groups id="5" revision_id="9" name="proxy" include_pattern="" exclude_pattern="" ordering="3" />
<groups id="6" revision_id="9" name="web" include_pattern="" exclude_pattern="" ordering="2" />
<groups id="7" revision_id="9" name="sql" include_pattern="" exclude_pattern="" ordering="4" />
<groups id="4" revision_id="9" name="default" include_pattern=".*" exclude_pattern="" ordering="0" />
<groups id="5" revision_id="9" name="proxy" include_pattern="" exclude_pattern="" ordering="2" />
<groups id="6" revision_id="9" name="web" include_pattern="" exclude_pattern="" ordering="1" />
<groups id="7" revision_id="9" name="sql" include_pattern="" exclude_pattern="" ordering="3" />

<!-- RELEASE -->
<group_classes id="1" name="dns" group_id="1" />
Expand Down

0 comments on commit 9f84cd8

Please sign in to comment.