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

Commit 53fcd5f

Browse files
committed
Merge branch 'issues/142' into develop
Close #146 Fix #142
2 parents 4a7395c + 56a919e commit 53fcd5f

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file, in reverse
2424

2525
### Fixed
2626

27-
- Nothing.
27+
- [#146](https://github.com/zendframework/zend-db/pull/146) fixes setting correct value for `lastInsertValue` pre-insert in `SequenceFeature`.
2828

2929
## 2.10.2 - TBD
3030

src/TableGateway/Feature/SequenceFeature.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public function __construct($primaryKeyField, $sequenceName)
4747
*/
4848
public function preInsert(Insert $insert)
4949
{
50+
$this->tableGateway->lastInsertValue = $this->lastSequenceId();
51+
5052
$columns = $insert->getRawState('columns');
5153
$values = $insert->getRawState('values');
5254
$key = array_search($this->primaryKeyField, $columns);
@@ -70,9 +72,7 @@ public function preInsert(Insert $insert)
7072
*/
7173
public function postInsert(StatementInterface $statement, ResultInterface $result)
7274
{
73-
if ($this->sequenceValue !== null) {
74-
$this->tableGateway->lastInsertValue = $this->sequenceValue;
75-
}
75+
$this->tableGateway->lastInsertValue = $this->sequenceValue;
7676
}
7777

7878
/**

test/unit/TableGateway/Feature/SequenceFeatureTest.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,88 @@ public function testNextSequenceId($platformName, $statementSql)
9090
$this->feature->nextSequenceId();
9191
}
9292

93+
/**
94+
* @dataProvider lastSequenceIdProvider
95+
*/
96+
public function testPreInsertWillReturnLastInsertValueIfPrimaryKeySetInColumnsData($platformName, $statementSql)
97+
{
98+
$platform = $this->getMockForAbstractClass('Zend\Db\Adapter\Platform\PlatformInterface', ['getName']);
99+
$platform->expects($this->any())
100+
->method('getName')
101+
->will($this->returnValue($platformName));
102+
$platform->expects($this->any())
103+
->method('quoteIdentifier')
104+
->will($this->returnValue($this->sequenceName));
105+
$adapter = $this->getMockBuilder('Zend\Db\Adapter\Adapter')
106+
->setMethods(['getPlatform', 'createStatement'])
107+
->disableOriginalConstructor()
108+
->getMock();
109+
$adapter->expects($this->any())
110+
->method('getPlatform')
111+
->will($this->returnValue($platform));
112+
$result = $this->getMockForAbstractClass(
113+
'Zend\Db\Adapter\Driver\ResultInterface',
114+
[],
115+
'',
116+
false,
117+
true,
118+
true,
119+
['current']
120+
);
121+
$result->expects($this->any())
122+
->method('current')
123+
->will($this->returnValue(['currval' => 1]));
124+
$statement = $this->getMockForAbstractClass(
125+
'Zend\Db\Adapter\Driver\StatementInterface',
126+
[],
127+
'',
128+
false,
129+
true,
130+
true,
131+
['prepare', 'execute']
132+
);
133+
$statement->expects($this->any())
134+
->method('execute')
135+
->will($this->returnValue($result));
136+
$statement->expects($this->any())
137+
->method('prepare')
138+
->with($statementSql);
139+
$adapter->expects($this->any())
140+
->method('createStatement')
141+
->will($this->returnValue($statement));
142+
$this->tableGateway = $this->getMockForAbstractClass(
143+
'Zend\Db\TableGateway\TableGateway',
144+
['table', $adapter],
145+
'',
146+
true
147+
);
148+
$this->feature->setTableGateway($this->tableGateway);
149+
$insert = $this->getMockBuilder('Zend\Db\Sql\Insert')
150+
->setMethods(['getPlatform', 'createStatement', 'getRawState'])
151+
->disableOriginalConstructor()
152+
->getMock();
153+
$insert->expects($this->at(0))
154+
->method('getRawState')
155+
->with('columns')
156+
->will($this->returnValue(['id']));
157+
/** @var \Zend\Db\Sql\Insert $insert */
158+
$this->feature->preInsert($insert);
159+
$this->assertEquals(1, $this->tableGateway->getLastInsertValue());
160+
}
161+
93162
public function nextSequenceIdProvider()
94163
{
95-
return [['PostgreSQL', 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'],
96-
['Oracle', 'SELECT ' . $this->sequenceName . '.NEXTVAL as "nextval" FROM dual']];
164+
return [
165+
['PostgreSQL', 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'],
166+
['Oracle', 'SELECT ' . $this->sequenceName . '.NEXTVAL as "nextval" FROM dual'],
167+
];
168+
}
169+
170+
public function lastSequenceIdProvider()
171+
{
172+
return [
173+
['PostgreSQL', 'SELECT CURRVAL(\'' . $this->sequenceName . '\')'],
174+
['Oracle', 'SELECT ' . $this->sequenceName . '.CURRVAL as "currval" FROM dual'],
175+
];
97176
}
98177
}

0 commit comments

Comments
 (0)