55namespace DotTest \Maker ;
66
77use Dot \Maker \ColorEnum ;
8+ use Dot \Maker \IO \Input ;
89use Dot \Maker \IO \Output ;
910use Dot \Maker \Maker ;
1011use org \bovigo \vfs \vfsStream ;
1112use PHPUnit \Framework \TestCase ;
1213
1314use function fclose ;
1415use function fopen ;
16+ use function fwrite ;
1517use function rewind ;
1618use function stream_get_contents ;
1719
1820use const PHP_EOL ;
1921
2022class MakerTest extends TestCase
2123{
24+ /** @var resource $outputStream */
25+ private $ outputStream ;
26+ /** @var resource $inputStream */
27+ private $ inputStream ;
28+ /** @var resource $errorStream */
29+ private $ errorStream ;
30+
31+ protected function setUp (): void
32+ {
33+ $ this ->errorStream = fopen ('php://memory ' , 'w+ ' );
34+ Output::setErrorStream ($ this ->errorStream );
35+
36+ $ this ->inputStream = fopen ('php://memory ' , 'w+ ' );
37+ Input::setStream ($ this ->inputStream );
38+
39+ $ this ->outputStream = fopen ('php://memory ' , 'w+ ' );
40+ Output::setOutputStream ($ this ->outputStream );
41+ }
42+
43+ protected function tearDown (): void
44+ {
45+ fclose ($ this ->outputStream );
46+ fclose ($ this ->inputStream );
47+ fclose ($ this ->errorStream );
48+ }
49+
2250 public function testWillInstantiate (): void
2351 {
2452 $ maker = new Maker ('' );
@@ -27,23 +55,18 @@ public function testWillInstantiate(): void
2755
2856 public function testInvokeWillOutputErrorWhenNotInCliMode (): void
2957 {
30- $ errorStream = fopen ('php://memory ' , 'w+ ' );
31- Output::setErrorStream ($ errorStream );
32-
3358 $ maker = $ this ->getMockBuilder (Maker::class)
3459 ->onlyMethods (['isCli ' ])
3560 ->setConstructorArgs (['' ])
3661 ->getMock ();
3762 $ maker ->method ('isCli ' )->willReturn (false );
3863 $ maker ([]);
3964
40- rewind ($ errorStream );
65+ rewind ($ this -> errorStream );
4166 $ this ->assertSame (
4267 ColorEnum::colorize ('dot-maker must be run in CLI only ' , ColorEnum::ForegroundBrightRed) . PHP_EOL ,
43- stream_get_contents ($ errorStream )
68+ stream_get_contents ($ this -> errorStream )
4469 );
45-
46- fclose ($ errorStream );
4770 }
4871
4972 public function testInvokeWillOutputErrorWhenInvalidComponent (): void
@@ -59,29 +82,20 @@ public function testInvokeWillOutputErrorWhenInvalidComponent(): void
5982 } ' ,
6083 ]);
6184
62- $ errorStream = fopen ('php://memory ' , 'w+ ' );
63- Output::setErrorStream ($ errorStream );
64-
65- $ outputStream = fopen ('php://memory ' , 'w+ ' );
66- Output::setOutputStream ($ outputStream );
67-
6885 $ maker = $ this ->getMockBuilder (Maker::class)
6986 ->onlyMethods (['isCli ' ])
7087 ->setConstructorArgs ([$ root ->url ()])
7188 ->getMock ();
7289 $ maker ->method ('isCli ' )->willReturn (true );
7390 $ maker (['' , 'invalid-component ' ]);
7491
75- rewind ($ errorStream );
76- rewind ($ outputStream );
92+ rewind ($ this -> errorStream );
93+ rewind ($ this -> outputStream );
7794 $ this ->assertSame (
7895 ColorEnum::colorize ('Unknown component: "invalid-component" ' , ColorEnum::ForegroundBrightRed) . PHP_EOL ,
79- stream_get_contents ($ errorStream )
96+ stream_get_contents ($ this -> errorStream )
8097 );
81- $ this ->assertEmpty (stream_get_contents ($ outputStream ));
82-
83- fclose ($ errorStream );
84- fclose ($ outputStream );
98+ $ this ->assertEmpty (stream_get_contents ($ this ->outputStream ));
8599 }
86100
87101 public function testInvokeWithoutArgsWillOutputHelpText (): void
@@ -97,28 +111,54 @@ public function testInvokeWithoutArgsWillOutputHelpText(): void
97111 } ' ,
98112 ]);
99113
100- $ errorStream = fopen ('php://memory ' , 'w+ ' );
101- Output::setErrorStream ($ errorStream );
102-
103- $ outputStream = fopen ('php://memory ' , 'w+ ' );
104- Output::setOutputStream ($ outputStream );
105-
106114 $ maker = $ this ->getMockBuilder (Maker::class)
107115 ->onlyMethods (['isCli ' ])
108116 ->setConstructorArgs ([$ root ->url ()])
109117 ->getMock ();
110118 $ maker ->method ('isCli ' )->willReturn (true );
111119 $ maker (['' , '' ]);
112120
113- rewind ($ errorStream );
114- rewind ($ outputStream );
121+ rewind ($ this -> errorStream );
122+ rewind ($ this -> outputStream );
115123 $ this ->assertSame (
116124 Helper::getHelpText (),
117- stream_get_contents ($ outputStream )
125+ stream_get_contents ($ this -> outputStream )
118126 );
119- $ this ->assertEmpty (stream_get_contents ($ errorStream ));
127+ $ this ->assertEmpty (stream_get_contents ($ this ->errorStream ));
128+ }
129+
130+ public function testCallingInvokeWithArgsModuleWillOutputDebugInfo (): void
131+ {
132+ $ root = vfsStream::setup ('root ' , 0644 , [
133+ 'composer.json ' => '{
134+ "autoload": {
135+ "psr-4": {
136+ "Api \\\\App \\\\": "src/App/src/"
137+ }
138+ }
139+ } ' ,
140+ ]);
141+
142+ fwrite ($ this ->inputStream , PHP_EOL );
143+ rewind ($ this ->inputStream );
120144
121- fclose ($ errorStream );
122- fclose ($ outputStream );
145+ $ maker = $ this ->getMockBuilder (Maker::class)
146+ ->onlyMethods (['isCli ' ])
147+ ->setConstructorArgs ([$ root ->url ()])
148+ ->getMock ();
149+ $ maker ->method ('isCli ' )->willReturn (true );
150+ $ maker (['' , 'module ' ]);
151+
152+ rewind ($ this ->outputStream );
153+ $ this ->assertSame (
154+ <<<BODY
155+ \033[94mDetected project type: Api \033[0m
156+ \033[94mCore architecture: No \033[0m
157+
158+ New module name:
159+ BODY ,
160+ stream_get_contents ($ this ->outputStream )
161+ );
162+ $ this ->assertEmpty (stream_get_contents ($ this ->errorStream ));
123163 }
124164}
0 commit comments