99use PHPUnit \Framework \TestCase ;
1010use stdClass ;
1111
12+ /**
13+ * @covers \Flowcontrol\React\Inotify\InotifyStream
14+ */
1215class InotifyStreamTest extends TestCase
1316{
14- /**
15- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
16- */
1717 public function testInitNoStream (): void
1818 {
1919 /** @var \React\EventLoop\LoopInterface */
@@ -22,9 +22,6 @@ public function testInitNoStream(): void
2222 new InotifyStream (null , $ loop );
2323 }
2424
25- /**
26- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
27- */
2825 public function testInitStreamNotInReadMode (): void
2926 {
3027 /** @var \React\EventLoop\LoopInterface */
@@ -34,11 +31,6 @@ public function testInitStreamNotInReadMode(): void
3431 new InotifyStream ($ fd , $ loop );
3532 }
3633
37- /**
38- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
39- * @covers \Flowcontrol\React\Inotify\InotifyStream::resume
40- * @covers \Flowcontrol\React\Inotify\InotifyStream::handleData
41- */
4234 public function testValidStreamWithoutEvent (): void
4335 {
4436 /** @var \React\EventLoop\LoopInterface */
@@ -47,15 +39,12 @@ public function testValidStreamWithoutEvent(): void
4739 inotify_add_watch ($ fd , __DIR__ , IN_CLOSE_WRITE );
4840 $ watcher = new InotifyStream ($ fd , $ loop );
4941 $ watcher ->on ('event ' , $ this ->expectCallableNever ());
42+ $ watcher ->on ('close ' , $ this ->expectCallableOnce ());
5043 $ watcher ->handleData ();
44+ $ watcher ->close ();
5145 fclose ($ fd );
5246 }
5347
54- /**
55- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
56- * @covers \Flowcontrol\React\Inotify\InotifyStream::resume
57- * @covers \Flowcontrol\React\Inotify\InotifyStream::handleData
58- */
5948 public function testValidStreamWithEvent (): void
6049 {
6150 /** @var \React\EventLoop\LoopInterface */
@@ -70,36 +59,45 @@ public function testValidStreamWithEvent(): void
7059 fclose ($ fd );
7160 }
7261
73- /**
74- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
75- * @covers \Flowcontrol\React\Inotify\InotifyStream::pause
76- * @covers \Flowcontrol\React\Inotify\InotifyStream::resume
77- * @covers \Flowcontrol\React\Inotify\InotifyStream::handleData
78- * @covers \Flowcontrol\React\Inotify\InotifyStream::close
79- */
62+ public function testNoResumeAfterClose (): void
63+ {
64+ /** @var \React\EventLoop\LoopInterface */
65+ $ loop = $ this ->getMockBuilder (\React \EventLoop \LoopInterface::class)->getMock ();
66+ $ fd = inotify_init ();
67+ inotify_add_watch ($ fd , __DIR__ , IN_CLOSE_WRITE );
68+ $ watcher = new InotifyStream ($ fd , $ loop );
69+ $ watcher ->on ('event ' , $ this ->expectCallableOnce ());
70+ touch (__DIR__ . '/testfile ' );
71+ unlink (__DIR__ . '/testfile ' );
72+ $ watcher ->handleData ();
73+ $ watcher ->close ();
74+ fclose ($ fd );
75+ $ watcher ->resume ();
76+ $ this ->assertFalse ($ watcher ->isReadable ());
77+ }
78+
8079 public function testValidStreamPauseWithEvent (): void
8180 {
8281 /** @var \React\EventLoop\LoopInterface */
8382 $ loop = $ this ->getMockBuilder (\React \EventLoop \LoopInterface::class)->getMock ();
8483 $ loop ->expects ($ this ->exactly (2 ))->method ('removeReadStream ' );
8584 $ loop ->expects ($ this ->exactly (2 ))->method ('addReadStream ' );
86- $ fd = inotify_init ();
85+ $ fd = inotify_init ();
8786 inotify_add_watch ($ fd , __DIR__ , IN_CLOSE_WRITE );
8887 $ watcher = new InotifyStream ($ fd , $ loop );
88+ $ watcher ->on ('event ' , $ this ->expectCallableTimes (2 ));
89+ touch (__DIR__ . '/testfile ' );
90+ unlink (__DIR__ . '/testfile ' );
91+ $ watcher ->handleData ();
8992 $ watcher ->pause ();
9093 $ watcher ->resume ();
94+ touch (__DIR__ . '/testfile ' );
95+ unlink (__DIR__ . '/testfile ' );
9196 $ watcher ->handleData ();
9297 $ watcher ->close ();
98+ fclose ($ fd );
9399 }
94100
95- /**
96- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
97- * @covers \Flowcontrol\React\Inotify\InotifyStream::resume
98- * @covers \Flowcontrol\React\Inotify\InotifyStream::handleData
99- * @covers \Flowcontrol\React\Inotify\InotifyStream::pause
100- * @covers \Flowcontrol\React\Inotify\InotifyStream::close
101- * @covers \Flowcontrol\React\Inotify\InotifyStream::isReadable
102- */
103101 public function testValidStreamWithoutEventAndClose (): void
104102 {
105103 /** @var \React\EventLoop\LoopInterface */
@@ -116,17 +114,10 @@ public function testValidStreamWithoutEventAndClose(): void
116114 $ this ->assertFalse ($ watcher ->isReadable ());
117115 // test close call on closed stream
118116 $ watcher ->close ();
117+ fclose ($ fd );
119118 $ this ->assertFalse ($ watcher ->isReadable ());
120119 }
121120
122- /**
123- * @covers \Flowcontrol\React\Inotify\InotifyStream::__construct
124- * @covers \Flowcontrol\React\Inotify\InotifyStream::resume
125- * @covers \Flowcontrol\React\Inotify\InotifyStream::handleData
126- * @covers \Flowcontrol\React\Inotify\InotifyStream::pause
127- * @covers \Flowcontrol\React\Inotify\InotifyStream::close
128- * @covers \Flowcontrol\React\Inotify\InotifyStream::isReadable
129- */
130121 public function testCloseStreamWhileHandling (): void
131122 {
132123 /** @var \React\EventLoop\LoopInterface */
@@ -157,4 +148,12 @@ private function expectCallableOnce()
157148
158149 return $ mock ;
159150 }
151+
152+ private function expectCallableTimes (int $ times )
153+ {
154+ $ mock = $ this ->getMockBuilder (stdClass::class)->setMethods (['__invoke ' ])->getMock ();
155+ $ mock ->expects ($ this ->exactly ($ times ))->method ('__invoke ' );
156+
157+ return $ mock ;
158+ }
160159}
0 commit comments