@@ -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