@@ -135,6 +135,53 @@ public function testReadWriteReadWithNullByte()
135
135
$ this ->assertSame ($ sessionData , $ readData , 'Written value can be read back correctly ' );
136
136
}
137
137
138
+ public function testReadConvertsStreamToString ()
139
+ {
140
+ $ pdo = new MockPdo ('pgsql ' );
141
+ $ pdo ->prepareResult = $ this ->getMock ('PDOStatement ' );
142
+
143
+ $ content = 'foobar ' ;
144
+ $ stream = $ this ->createStream ($ content );
145
+
146
+ $ pdo ->prepareResult ->expects ($ this ->once ())->method ('fetchAll ' )
147
+ ->will ($ this ->returnValue (array (array ($ stream , 42 , time ()))));
148
+
149
+ $ storage = new PdoSessionHandler ($ pdo );
150
+ $ result = $ storage ->read ('foo ' );
151
+
152
+ $ this ->assertSame ($ content , $ result );
153
+ }
154
+
155
+ public function testReadLockedConvertsStreamToString ()
156
+ {
157
+ $ pdo = new MockPdo ('pgsql ' );
158
+ $ selectStmt = $ this ->getMock ('PDOStatement ' );
159
+ $ insertStmt = $ this ->getMock ('PDOStatement ' );
160
+
161
+ $ pdo ->prepareResult = function ($ statement ) use ($ selectStmt , $ insertStmt ) {
162
+ return 0 === strpos ($ statement , 'INSERT ' ) ? $ insertStmt : $ selectStmt ;
163
+ };
164
+
165
+ $ content = 'foobar ' ;
166
+ $ stream = $ this ->createStream ($ content );
167
+ $ exception = null ;
168
+
169
+ $ selectStmt ->expects ($ this ->atLeast (2 ))->method ('fetchAll ' )
170
+ ->will ($ this ->returnCallback (function () use (&$ exception , $ stream ) {
171
+ return $ exception ? array (array ($ stream , 42 , time ())) : array ();
172
+ }));
173
+
174
+ $ insertStmt ->expects ($ this ->once ())->method ('execute ' )
175
+ ->will ($ this ->returnCallback (function () use (&$ exception ) {
176
+ throw $ exception = new \PDOException ('' , '23 ' );
177
+ }));
178
+
179
+ $ storage = new PdoSessionHandler ($ pdo );
180
+ $ result = $ storage ->read ('foo ' );
181
+
182
+ $ this ->assertSame ($ content , $ result );
183
+ }
184
+
138
185
public function testReadingRequiresExactlySameId ()
139
186
{
140
187
$ storage = new PdoSessionHandler ($ this ->getMemorySqlitePdo ());
@@ -263,4 +310,50 @@ public function testGetConnectionConnectsIfNeeded()
263
310
264
311
$ this ->assertInstanceOf ('\PDO ' , $ method ->invoke ($ storage ));
265
312
}
313
+
314
+ private function createStream ($ content )
315
+ {
316
+ $ stream = tmpfile ();
317
+ fwrite ($ stream , $ content );
318
+ fseek ($ stream , 0 );
319
+
320
+ return $ stream ;
321
+ }
322
+ }
323
+
324
+ class MockPdo extends \PDO
325
+ {
326
+ public $ prepareResult ;
327
+ private $ driverName ;
328
+ private $ errorMode ;
329
+
330
+ public function __construct ($ driverName = null , $ errorMode = null )
331
+ {
332
+ $ this ->driverName = $ driverName ;
333
+ $ this ->errorMode = null !== $ errorMode ?: \PDO ::ERRMODE_EXCEPTION ;
334
+ }
335
+
336
+ public function getAttribute ($ attribute )
337
+ {
338
+ if (\PDO ::ATTR_ERRMODE === $ attribute ) {
339
+ return $ this ->errorMode ;
340
+ }
341
+
342
+ if (\PDO ::ATTR_DRIVER_NAME === $ attribute ) {
343
+ return $ this ->driverName ;
344
+ }
345
+
346
+ return parent ::getAttribute ($ attribute );
347
+ }
348
+
349
+ public function prepare ($ statement , $ driverOptions = array ())
350
+ {
351
+ return is_callable ($ this ->prepareResult )
352
+ ? call_user_func ($ this ->prepareResult , $ statement , $ driverOptions )
353
+ : $ this ->prepareResult ;
354
+ }
355
+
356
+ public function beginTransaction ()
357
+ {
358
+ }
266
359
}
0 commit comments