2525
2626use CPSIT \ProjectBuilder as Src ;
2727use CPSIT \ProjectBuilder \Tests ;
28+ use Generator ;
29+ use LogicException ;
2830use PHPUnit \Framework ;
2931use Symfony \Component \Filesystem ;
3032
@@ -38,6 +40,7 @@ final class RunCommandStepTest extends Tests\ContainerAwareTestCase
3840{
3941 private Src \Builder \Generator \Step \RunCommandStep $ subject ;
4042 private Src \Builder \BuildResult $ result ;
43+ private Filesystem \Filesystem $ filesystem ;
4144
4245 protected function setUp (): void
4346 {
@@ -50,6 +53,11 @@ protected function setUp(): void
5053 'foo ' ,
5154 ),
5255 );
56+ $ this ->filesystem = new Filesystem \Filesystem ();
57+
58+ if (!$ this ->filesystem ->exists ($ this ->result ->getWrittenDirectory ())) {
59+ $ this ->filesystem ->mkdir ($ this ->result ->getWrittenDirectory ());
60+ }
5361 }
5462
5563 #[Framework \Attributes \Test]
@@ -64,7 +72,7 @@ public function runThrowsExceptionIfNoCommandIsGiven(): void
6472 #[Framework \Attributes \Test]
6573 public function runThrowsExceptionIfRevertingIsAttempted (): never
6674 {
67- $ this ->expectException (' \ LogicException' );
75+ $ this ->expectException (LogicException::class );
6876 $ this ->expectExceptionCode (1687518806 );
6977 $ this ->expectExceptionMessage ('An already run command cannot be reverted. ' );
7078 $ this ->subject ->revert ($ this ->result );
@@ -79,7 +87,7 @@ public function runCommandIsSupported(): void
7987 }
8088
8189 #[Framework \Attributes \Test]
82- public function runExecutesCommandWithoutConfirmationIfSKipConfirmationIsConfigured (): void
90+ public function runExecutesCommandWithoutConfirmationIfSkipConfirmationIsConfigured (): void
8391 {
8492 $ this ->subject ->setConfig (
8593 new Src \Builder \Config \ValueObject \Step (
@@ -91,18 +99,30 @@ public function runExecutesCommandWithoutConfirmationIfSKipConfirmationIsConfigu
9199 ),
92100 );
93101
94- $ workingDirectory = $ this ->result ->getWrittenDirectory ();
95-
96- $ fileSystem = new Filesystem \Filesystem ();
97- if (!$ fileSystem ->exists ($ workingDirectory )) {
98- $ fileSystem ->mkdir ($ workingDirectory );
99- }
100-
101102 self ::assertTrue ($ this ->subject ->run ($ this ->result ));
102103 self ::assertFalse ($ this ->subject ->isStopped ());
103104 self ::assertStringNotContainsString ('Do you wish to run this command? ' , $ this ->io ->getOutput ());
104105 }
105106
107+ #[Framework \Attributes \Test]
108+ #[Framework \Attributes \DataProvider('runDoesNotExecuteCommandAndRespectsExecutionRequirementDataProvider ' )]
109+ public function runDoesNotExecuteCommandAndRespectsExecutionRequirement (bool $ required , bool $ expected ): void
110+ {
111+ $ this ->subject ->setConfig (
112+ new Src \Builder \Config \ValueObject \Step (
113+ Src \Builder \Generator \Step \RunCommandStep::getType (),
114+ new Src \Builder \Config \ValueObject \StepOptions (
115+ command: 'echo \'foo \'' ,
116+ required: $ required ,
117+ ),
118+ ),
119+ );
120+
121+ $ this ->io ->setUserInputs (['no ' ]);
122+ self ::assertSame ($ expected , $ this ->subject ->run ($ this ->result ));
123+ self ::assertTrue ($ this ->subject ->isStopped ());
124+ }
125+
106126 #[Framework \Attributes \Test]
107127 public function runExecutesCommandAndAllowsExecutionFailures (): void
108128 {
@@ -116,13 +136,6 @@ public function runExecutesCommandAndAllowsExecutionFailures(): void
116136 ),
117137 );
118138
119- $ workingDirectory = $ this ->result ->getWrittenDirectory ();
120-
121- $ fileSystem = new Filesystem \Filesystem ();
122- if (!$ fileSystem ->exists ($ workingDirectory )) {
123- $ fileSystem ->mkdir ($ workingDirectory );
124- }
125-
126139 self ::assertTrue ($ this ->subject ->run ($ this ->result ));
127140 self ::assertFalse ($ this ->subject ->isStopped ());
128141 self ::assertStringContainsString ('not found ' , $ this ->io ->getOutput ());
@@ -157,19 +170,23 @@ public function invalidCommandPrintProcessErrorMessage(): void
157170 ),
158171 );
159172
160- $ workingDirectory = $ this ->result ->getWrittenDirectory ();
161-
162- $ fileSystem = new Filesystem \Filesystem ();
163- if (!$ fileSystem ->exists ($ workingDirectory )) {
164- $ fileSystem ->mkdir ($ workingDirectory );
165- }
166-
167173 $ this ->io ->setUserInputs (['yes ' ]);
168- $ actual = $ this ->subject ->run ($ this ->result );
169- $ this -> io -> getOutput ();
174+ self :: assertFalse ( $ this ->subject ->run ($ this ->result ) );
175+ }
170176
171- self ::assertFalse ($ actual );
177+ /**
178+ * @return Generator<string, array{bool, bool}>
179+ */
180+ public static function runDoesNotExecuteCommandAndRespectsExecutionRequirementDataProvider (): Generator
181+ {
182+ yield 'required ' => [true , false ];
183+ yield 'optional ' => [false , true ];
184+ }
172185
173- $ fileSystem ->remove ($ workingDirectory );
186+ protected function tearDown (): void
187+ {
188+ if ($ this ->filesystem ->exists ($ this ->result ->getWrittenDirectory ())) {
189+ $ this ->filesystem ->remove ($ this ->result ->getWrittenDirectory ());
190+ }
174191 }
175192}
0 commit comments