@@ -18,6 +18,18 @@ public function testConstructor()
1818 $ buffer ->on ('error ' , $ this ->expectCallableNever ());
1919 }
2020
21+ /**
22+ * @covers React\Stream\Buffer::__construct
23+ * @expectedException InvalidArgumentException
24+ */
25+ public function testConstructorThrowsIfNotAValidStreamResource ()
26+ {
27+ $ stream = null ;
28+ $ loop = $ this ->createLoopMock ();
29+
30+ new Buffer ($ stream , $ loop );
31+ }
32+
2133 /**
2234 * @covers React\Stream\Buffer::write
2335 * @covers React\Stream\Buffer::handleWrite
@@ -84,6 +96,20 @@ public function testWriteReturnsFalseWhenBufferIsFull()
8496 $ this ->assertFalse ($ buffer ->write ("bar \n" ));
8597 }
8698
99+ /**
100+ * @covers React\Stream\Buffer::write
101+ */
102+ public function testWriteReturnsFalseWhenBufferIsExactlyFull ()
103+ {
104+ $ stream = fopen ('php://temp ' , 'r+ ' );
105+ $ loop = $ this ->createLoopMock ();
106+
107+ $ buffer = new Buffer ($ stream , $ loop );
108+ $ buffer ->softLimit = 3 ;
109+
110+ $ this ->assertFalse ($ buffer ->write ("foo " ));
111+ }
112+
87113 /**
88114 * @covers React\Stream\Buffer::write
89115 * @covers React\Stream\Buffer::handleWrite
@@ -152,8 +178,6 @@ public function testDrain()
152178 */
153179 public function testWriteInDrain ()
154180 {
155- $ writeStreams = array ();
156-
157181 $ stream = fopen ('php://temp ' , 'r+ ' );
158182 $ loop = $ this ->createWriteableLoopMock ();
159183 $ loop ->preventWrites = true ;
@@ -176,6 +200,47 @@ public function testWriteInDrain()
176200 $ this ->assertSame ("foo \nbar \n" , stream_get_contents ($ stream ));
177201 }
178202
203+ /**
204+ * @covers React\Stream\Buffer::write
205+ * @covers React\Stream\Buffer::handleWrite
206+ */
207+ public function testDrainAndFullDrainAfterWrite ()
208+ {
209+ $ stream = fopen ('php://temp ' , 'r+ ' );
210+ $ loop = $ this ->createLoopMock ();
211+
212+ $ buffer = new Buffer ($ stream , $ loop );
213+ $ buffer ->softLimit = 2 ;
214+
215+ $ buffer ->on ('drain ' , $ this ->expectCallableOnce ());
216+ $ buffer ->on ('full-drain ' , $ this ->expectCallableOnce ());
217+
218+ $ buffer ->write ("foo " );
219+ $ buffer ->handleWrite ();
220+ }
221+
222+ /**
223+ * @covers React\Stream\Buffer::write
224+ * @covers React\Stream\Buffer::handleWrite
225+ */
226+ public function testCloseDuringDrainWillNotEmitFullDrain ()
227+ {
228+ $ stream = fopen ('php://temp ' , 'r+ ' );
229+ $ loop = $ this ->createLoopMock ();
230+
231+ $ buffer = new Buffer ($ stream , $ loop );
232+ $ buffer ->softLimit = 2 ;
233+
234+ // close buffer on drain event => expect close event, but no full-drain after
235+ $ buffer ->on ('drain ' , $ this ->expectCallableOnce ());
236+ $ buffer ->on ('drain ' , array ($ buffer , 'close ' ));
237+ $ buffer ->on ('close ' , $ this ->expectCallableOnce ());
238+ $ buffer ->on ('full-drain ' , $ this ->expectCallableNever ());
239+
240+ $ buffer ->write ("foo " );
241+ $ buffer ->handleWrite ();
242+ }
243+
179244 /**
180245 * @covers React\Stream\Buffer::end
181246 */
@@ -249,11 +314,10 @@ public function testWritingToClosedBufferShouldNotWriteToStream()
249314
250315 /**
251316 * @covers React\Stream\Buffer::handleWrite
252- * @covers React\Stream\Buffer::errorHandler
253317 */
254- public function testError ()
318+ public function testErrorWhenStreamResourceIsInvalid ()
255319 {
256- $ stream = null ;
320+ $ stream = fopen ( ' php://temp ' , ' r+ ' ) ;
257321 $ loop = $ this ->createWriteableLoopMock ();
258322
259323 $ error = null ;
@@ -263,9 +327,16 @@ public function testError()
263327 $ error = $ message ;
264328 });
265329
330+ // invalidate stream resource
331+ fclose ($ stream );
332+
266333 $ buffer ->write ('Attempting to write to bad stream ' );
334+
267335 $ this ->assertInstanceOf ('Exception ' , $ error );
268- $ this ->assertSame ('Tried to write to invalid stream. ' , $ error ->getMessage ());
336+
337+ // the error messages differ between PHP versions, let's just check substrings
338+ $ this ->assertContains ('Unable to write to stream: ' , $ error ->getMessage ());
339+ $ this ->assertContains (' not a valid stream resource ' , $ error ->getMessage (), '' , true );
269340 }
270341
271342 public function testWritingToClosedStream ()
@@ -290,7 +361,7 @@ public function testWritingToClosedStream()
290361 $ buffer ->write ('bar ' );
291362
292363 $ this ->assertInstanceOf ('Exception ' , $ error );
293- $ this ->assertSame ('fwrite(): send of 3 bytes failed with errno=32 Broken pipe ' , $ error ->getMessage ());
364+ $ this ->assertSame ('Unable to write to stream: fwrite(): send of 3 bytes failed with errno=32 Broken pipe ' , $ error ->getMessage ());
294365 }
295366
296367 private function createWriteableLoopMock ()
0 commit comments