1111use DR \CodeSnifferBaseline \Plugin \Plugin ;
1212use Exception ;
1313use org \bovigo \vfs \vfsStream ;
14+ use PHPUnit \Framework \Attributes \CoversClass ;
1415use PHPUnit \Framework \MockObject \MockObject ;
1516use PHPUnit \Framework \TestCase ;
1617use RuntimeException ;
1718
18- /**
19- * @coversDefaultClass \DR\CodeSnifferBaseline\Plugin\Plugin
20- * @covers ::__construct
21- */
19+ #[CoversClass(Plugin::class)]
2220class PluginTest extends TestCase
2321{
24- /** @var Composer|MockObject */
25- private Composer $ composer ;
26- /** @var IOInterface|MockObject */
27- private IOInterface $ stream ;
22+ private Composer &MockObject $ composer ;
23+ private IOInterface &MockObject $ stream ;
2824
2925 protected function setUp (): void
3026 {
3127 $ this ->composer = $ this ->createMock (Composer::class);
3228 $ this ->stream = $ this ->createMock (IOInterface::class);
3329 }
3430
35- /**
36- * @covers ::getSubscribedEvents
37- */
3831 public function testGetSubscribedEvents (): void
3932 {
4033 $ expected = [
@@ -45,10 +38,6 @@ public function testGetSubscribedEvents(): void
4538 static ::assertSame ($ expected , Plugin::getSubscribedEvents ());
4639 }
4740
48- /**
49- * @covers ::activate
50- * @covers ::onPostInstall
51- */
5241 public function testOnPostInstallWithoutExistingFile (): void
5342 {
5443 $ this ->stream ->expects (static ::once ())->method ('error ' )->with (static ::stringContains ('failed to find ' ));
@@ -58,15 +47,18 @@ public function testOnPostInstallWithoutExistingFile(): void
5847 $ plugin ->onPostInstall ();
5948 }
6049
61- /**
62- * @covers ::activate
63- * @covers ::onPostInstall
64- */
6550 public function testOnPostInstallAlreadyContainsInjection (): void
6651 {
67- $ this ->stream ->expects (static ::exactly (2 ))
68- ->method ('info ' )
69- ->withConsecutive ([static ::stringContains ('read ' )], [static ::stringContains ('is already modified ' )]);
52+ $ matcher = static ::exactly (2 );
53+ $ this ->stream ->expects ($ matcher )
54+ ->method ('info ' )->willReturnCallback (function (...$ parameters ) use ($ matcher ) {
55+ if ($ matcher ->numberOfInvocations () === 1 ) {
56+ $ this ->assertSame (static ::stringContains ('read ' ), $ parameters [0 ]);
57+ }
58+ if ($ matcher ->numberOfInvocations () === 2 ) {
59+ $ this ->assertSame (static ::stringContains ('is already modified ' ), $ parameters [0 ]);
60+ }
61+ });
7062
7163 $ file = vfsStream::setup ()->url () . '/File.php ' ;
7264 file_put_contents ($ file , 'foobar \\' . BaselineHandler::class . 'foobar ' );
@@ -77,15 +69,16 @@ public function testOnPostInstallAlreadyContainsInjection(): void
7769 $ plugin ->onPostInstall ();
7870 }
7971
80- /**
81- * @covers ::activate
82- * @covers ::onPostInstall
83- */
8472 public function testOnPostInstallShouldErrorWhenMessageCountCantBeFound (): void
8573 {
86- $ this ->stream ->expects (static ::once ())
74+ $ matcher = static ::once ();
75+ $ this ->stream ->expects ($ matcher )
8776 ->method ('error ' )
88- ->withConsecutive ([static ::stringContains ('unable to find `$messageCount++;` ' )]);
77+ ->willReturnCallback (function (...$ parameters ) use ($ matcher ) {
78+ if ($ matcher ->numberOfInvocations () === 1 ) {
79+ $ this ->assertSame (static ::stringContains ('unable to find `$messageCount++;` ' ), $ parameters [0 ]);
80+ }
81+ });
8982
9083 $ file = vfsStream::setup ()->url () . '/File.php ' ;
9184 file_put_contents ($ file , 'foobar ' );
@@ -95,15 +88,18 @@ public function testOnPostInstallShouldErrorWhenMessageCountCantBeFound(): void
9588 $ plugin ->onPostInstall ();
9689 }
9790
98- /**
99- * @covers ::activate
100- * @covers ::onPostInstall
101- */
10291 public function testOnPostInstallShouldInjectCode (): void
10392 {
104- $ this ->stream ->expects (static ::exactly (2 ))
105- ->method ('info ' )
106- ->withConsecutive ([static ::stringContains ('read ' )], [static ::stringContains ('saved to: ' )]);
93+ $ matcher = static ::exactly (2 );
94+ $ this ->stream ->expects ($ matcher )
95+ ->method ('info ' )->willReturnCallback (function (...$ parameters ) use ($ matcher ) {
96+ if ($ matcher ->numberOfInvocations () === 1 ) {
97+ $ this ->assertSame (static ::stringContains ('read ' ), $ parameters [0 ]);
98+ }
99+ if ($ matcher ->numberOfInvocations () === 2 ) {
100+ $ this ->assertSame (static ::stringContains ('saved to: ' ), $ parameters [0 ]);
101+ }
102+ });
107103
108104 $ file = vfsStream::setup ()->url () . '/File.php ' ;
109105 file_put_contents ($ file , 'foobar $messageCount++; foobar ' );
@@ -116,7 +112,6 @@ public function testOnPostInstallShouldInjectCode(): void
116112 }
117113
118114 /**
119- * @covers ::run
120115 * @throws Exception
121116 */
122117 public function testRun (): void
0 commit comments