Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
SequenceFeature doesn't set lastInsertValue correctly before the inse…
Browse files Browse the repository at this point in the history
…rt was done
  • Loading branch information
Aleksandr Kovalenko committed Jul 6, 2016
1 parent 94218b4 commit dbc8534
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/TableGateway/Feature/SequenceFeature.php
Expand Up @@ -47,6 +47,8 @@ public function __construct($primaryKeyField, $sequenceName)
*/
public function preInsert(Insert $insert)
{
$this->tableGateway->lastInsertValue = $this->lastSequenceId();

$columns = $insert->getRawState('columns');
$values = $insert->getRawState('values');
$key = array_search($this->primaryKeyField, $columns);
Expand All @@ -70,9 +72,7 @@ public function preInsert(Insert $insert)
*/
public function postInsert(StatementInterface $statement, ResultInterface $result)
{
if ($this->sequenceValue !== null) {
$this->tableGateway->lastInsertValue = $this->sequenceValue;
}
$this->tableGateway->lastInsertValue = $this->sequenceValue;
}

/**
Expand Down
49 changes: 49 additions & 0 deletions test/TableGateway/Feature/SequenceFeatureTest.php
Expand Up @@ -66,9 +66,58 @@ public function testNextSequenceId($platformName, $statementSql)
$this->feature->nextSequenceId();
}

/**
* @dataProvider lastSequenceIdProvider
*/
public function testPreInsertWillReturnLastInsertValueIfPrimaryKeySetInColumnsData($platformName, $statementSql)
{
$platform = $this->getMockForAbstractClass('Zend\Db\Adapter\Platform\PlatformInterface', ['getName']);
$platform->expects($this->any())
->method('getName')
->will($this->returnValue($platformName));
$platform->expects($this->any())
->method('quoteIdentifier')
->will($this->returnValue($this->sequenceName));
$adapter = $this->getMock('Zend\Db\Adapter\Adapter', ['getPlatform', 'createStatement'], [], '', false);
$adapter->expects($this->any())
->method('getPlatform')
->will($this->returnValue($platform));
$result = $this->getMockForAbstractClass('Zend\Db\Adapter\Driver\ResultInterface', [], '', false, true, true,
['current']);
$result->expects($this->any())
->method('current')
->will($this->returnValue(['currval' => 1]));
$statement = $this->getMockForAbstractClass('Zend\Db\Adapter\Driver\StatementInterface', [], '', false, true, true, ['prepare', 'execute']);
$statement->expects($this->any())
->method('execute')
->will($this->returnValue($result));
$statement->expects($this->any())
->method('prepare')
->with($statementSql);
$adapter->expects($this->any())
->method('createStatement')
->will($this->returnValue($statement));
$this->tableGateway = $this->getMockForAbstractClass('Zend\Db\TableGateway\TableGateway', ['table', $adapter], '', true);
$this->feature->setTableGateway($this->tableGateway);
$insert = $this->getMock('Zend\Db\Sql\Insert', ['getPlatform', 'createStatement', 'getRawState'], [], '', false);
$insert->expects($this->at(0))
->method('getRawState')
->with('columns')
->will($this->returnValue(['id']));
/** @var \Zend\Db\Sql\Insert $insert */
$this->feature->preInsert($insert);
$this->assertEquals(1, $this->tableGateway->getLastInsertValue());
}

public function nextSequenceIdProvider()
{
return [['PostgreSQL', 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'],
['Oracle', 'SELECT ' . $this->sequenceName . '.NEXTVAL as "nextval" FROM dual']];
}

public function lastSequenceIdProvider()
{
return [['PostgreSQL', 'SELECT CURRVAL(\'' . $this->sequenceName . '\')'],
['Oracle', 'SELECT ' . $this->sequenceName . '.CURRVAL as "currval" FROM dual']];
}
}

0 comments on commit dbc8534

Please sign in to comment.