1919namespace FastForward \DevTools \Tests \Console \Command ;
2020
2121use FastForward \DevTools \Console \Command \CodeStyleCommand ;
22- use FastForward \DevTools \Process \ProcessBuilder ;
23- use FastForward \DevTools \Process \ProcessQueue ;
22+ use FastForward \DevTools \Process \ProcessBuilderInterface ;
23+ use FastForward \DevTools \Process \ProcessQueueInterface ;
2424use PHPUnit \Framework \Attributes \CoversClass ;
2525use PHPUnit \Framework \Attributes \Test ;
2626use PHPUnit \Framework \TestCase ;
27+ use Prophecy \Argument ;
2728use Prophecy \PhpUnit \ProphecyTrait ;
29+ use Prophecy \Prophecy \ObjectProphecy ;
30+ use ReflectionMethod ;
2831use Symfony \Component \Config \FileLocatorInterface ;
32+ use Symfony \Component \Console \Input \InputInterface ;
33+ use Symfony \Component \Console \Output \OutputInterface ;
34+ use Symfony \Component \Process \Process ;
2935
3036#[CoversClass(CodeStyleCommand::class)]
31- #[CoversClass(ProcessBuilder::class)]
32- #[CoversClass(ProcessQueue::class)]
3337final class CodeStyleCommandTest extends TestCase
3438{
3539 use ProphecyTrait;
3640
41+ private ObjectProphecy $ fileLocator ;
42+
43+ private ObjectProphecy $ processQueue ;
44+
45+ private ObjectProphecy $ process ;
46+
47+ private ObjectProphecy $ input ;
48+
49+ private ObjectProphecy $ output ;
50+
51+ private ObjectProphecy $ processBuilder ;
52+
3753 private CodeStyleCommand $ command ;
3854
3955 /**
4056 * @return void
4157 */
4258 protected function setUp (): void
4359 {
44- $ fileLocator = $ this ->createMock (FileLocatorInterface::class);
45- $ fileLocator ->method ('locate ' )
46- ->willReturn ('/path/to/ecs.php ' );
60+ $ this ->fileLocator = $ this ->prophesize (FileLocatorInterface::class);
61+ $ this ->processBuilder = $ this ->prophesize (ProcessBuilderInterface::class);
62+ $ this ->processQueue = $ this ->prophesize (ProcessQueueInterface::class);
63+ $ this ->process = $ this ->prophesize (Process::class);
64+ $ this ->input = $ this ->prophesize (InputInterface::class);
65+ $ this ->output = $ this ->prophesize (OutputInterface::class);
4766
48- $ processBuilder = new ProcessBuilder ();
49- $ processQueue = new ProcessQueue ();
67+ $ this ->processBuilder ->withArgument (Argument::any ())
68+ ->willReturn ($ this ->processBuilder ->reveal ());
69+ $ this ->processBuilder ->withArgument (Argument::any (), Argument::any ())
70+ ->willReturn ($ this ->processBuilder ->reveal ());
5071
51- $ this ->command = new CodeStyleCommand ($ fileLocator , $ processBuilder , $ processQueue );
72+ $ this ->processBuilder ->build (Argument::any ())
73+ ->willReturn ($ this ->process ->reveal ());
74+
75+ $ this ->command = new CodeStyleCommand (
76+ $ this ->fileLocator ->reveal (),
77+ $ this ->processBuilder ->reveal (),
78+ $ this ->processQueue ->reveal (),
79+ );
5280 }
5381
5482 /**
5583 * @return void
5684 */
5785 #[Test]
58- public function configureWillSetExpectedNameDescriptionAndHelp (): void
86+ public function commandWillSetExpectedNameDescriptionAndHelp (): void
5987 {
6088 self ::assertSame ('code-style ' , $ this ->command ->getName ());
6189 self ::assertSame (
@@ -76,4 +104,80 @@ public function commandCanBeConstructedWithDependencies(): void
76104 {
77105 self ::assertInstanceOf (CodeStyleCommand::class, $ this ->command );
78106 }
107+
108+ /**
109+ * @return void
110+ */
111+ #[Test]
112+ public function executeWillReturnSuccessWhenProcessQueueSucceeds (): void
113+ {
114+ $ this ->input ->getOption ('fix ' )
115+ ->willReturn (false );
116+
117+ $ this ->fileLocator ->locate (CodeStyleCommand::CONFIG )
118+ ->willReturn ('/path/to/ecs.php ' );
119+
120+ $ this ->processQueue ->run ($ this ->output ->reveal ())
121+ ->willReturn (CodeStyleCommand::SUCCESS )
122+ ->shouldBeCalled ();
123+
124+ $ result = $ this ->executeCommand ();
125+
126+ self ::assertSame (CodeStyleCommand::SUCCESS , $ result );
127+ }
128+
129+ /**
130+ * @return void
131+ */
132+ #[Test]
133+ public function executeWillReturnFailureWhenProcessQueueFails (): void
134+ {
135+ $ this ->input ->getOption ('fix ' )
136+ ->willReturn (false );
137+
138+ $ this ->fileLocator ->locate (CodeStyleCommand::CONFIG )
139+ ->willReturn ('/path/to/ecs.php ' );
140+
141+ $ this ->processQueue ->run ($ this ->output ->reveal ())
142+ ->willReturn (CodeStyleCommand::FAILURE )
143+ ->shouldBeCalled ();
144+
145+ $ result = $ this ->executeCommand ();
146+
147+ self ::assertSame (CodeStyleCommand::FAILURE , $ result );
148+ }
149+
150+ /**
151+ * @return void
152+ */
153+ #[Test]
154+ public function executeWillRunWithFixOptionWhenFixIsEnabled (): void
155+ {
156+ $ this ->input ->getOption ('fix ' )
157+ ->willReturn (true );
158+
159+ $ this ->fileLocator ->locate (CodeStyleCommand::CONFIG )
160+ ->willReturn ('/path/to/ecs.php ' );
161+
162+ $ this ->processQueue ->run ($ this ->output ->reveal ())
163+ ->willReturn (CodeStyleCommand::SUCCESS )
164+ ->shouldBeCalled ();
165+
166+ $ result = $ this ->executeCommand ();
167+
168+ self ::assertSame (CodeStyleCommand::SUCCESS , $ result );
169+ }
170+
171+ /**
172+ * @return int
173+ */
174+ private function executeCommand (): int
175+ {
176+ $ this ->processQueue ->add ($ this ->process ->reveal ())
177+ ->shouldBeCalledTimes (3 );
178+
179+ $ reflectionMethod = new ReflectionMethod ($ this ->command , 'execute ' );
180+
181+ return $ reflectionMethod ->invoke ($ this ->command , $ this ->input ->reveal (), $ this ->output ->reveal ());
182+ }
79183}
0 commit comments