|
19 | 19 | namespace FastForward\DevTools\Tests\Console\Command; |
20 | 20 |
|
21 | 21 | use FastForward\DevTools\Console\Command\RefactorCommand; |
| 22 | +use FastForward\DevTools\Process\ProcessBuilderInterface; |
| 23 | +use FastForward\DevTools\Process\ProcessQueueInterface; |
22 | 24 | use PHPUnit\Framework\Attributes\CoversClass; |
23 | 25 | use PHPUnit\Framework\Attributes\Test; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Prophecy\Argument; |
24 | 28 | use Prophecy\PhpUnit\ProphecyTrait; |
| 29 | +use Prophecy\Prophecy\ObjectProphecy; |
| 30 | +use ReflectionMethod; |
| 31 | +use Symfony\Component\Config\FileLocatorInterface; |
| 32 | +use Symfony\Component\Console\Input\InputInterface; |
| 33 | +use Symfony\Component\Console\Output\OutputInterface; |
25 | 34 | use Symfony\Component\Process\Process; |
26 | 35 |
|
27 | | -use function Safe\getcwd; |
28 | | - |
29 | 36 | #[CoversClass(RefactorCommand::class)] |
30 | | -final class RefactorCommandTest extends AbstractCommandTestCase |
| 37 | +final class RefactorCommandTest extends TestCase |
31 | 38 | { |
32 | 39 | use ProphecyTrait; |
33 | 40 |
|
34 | | - /** |
35 | | - * @return string |
36 | | - */ |
37 | | - protected function getCommandClass(): string |
38 | | - { |
39 | | - return RefactorCommand::class; |
40 | | - } |
| 41 | + /** @var ObjectProphecy<FileLocatorInterface> */ |
| 42 | + private ObjectProphecy $fileLocator; |
41 | 43 |
|
42 | | - /** |
43 | | - * @return string |
44 | | - */ |
45 | | - protected function getCommandName(): string |
46 | | - { |
47 | | - return 'refactor'; |
48 | | - } |
| 44 | + /** @var ObjectProphecy<ProcessBuilderInterface> */ |
| 45 | + private ObjectProphecy $processBuilder; |
49 | 46 |
|
50 | | - /** |
51 | | - * @return string |
52 | | - */ |
53 | | - protected function getCommandDescription(): string |
54 | | - { |
55 | | - return 'Runs Rector for code refactoring.'; |
56 | | - } |
| 47 | + /** @var ObjectProphecy<ProcessQueueInterface> */ |
| 48 | + private ObjectProphecy $processQueue; |
57 | 49 |
|
58 | | - /** |
59 | | - * @return string |
60 | | - */ |
61 | | - protected function getCommandHelp(): string |
62 | | - { |
63 | | - return 'This command runs Rector to refactor your code.'; |
64 | | - } |
| 50 | + /** @var ObjectProphecy<InputInterface> */ |
| 51 | + private ObjectProphecy $input; |
| 52 | + |
| 53 | + /** @var ObjectProphecy<OutputInterface> */ |
| 54 | + private ObjectProphecy $output; |
| 55 | + |
| 56 | + /** @var ObjectProphecy<Process> */ |
| 57 | + private ObjectProphecy $process; |
| 58 | + |
| 59 | + private RefactorCommand $command; |
| 60 | + |
| 61 | + private const string CONFIG_PATH = '/path/to/rector.php'; |
65 | 62 |
|
66 | | - /** |
67 | | - * @return void |
68 | | - */ |
69 | 63 | protected function setUp(): void |
70 | 64 | { |
71 | | - parent::setUp(); |
| 65 | + $this->fileLocator = $this->prophesize(FileLocatorInterface::class); |
| 66 | + $this->processBuilder = $this->prophesize(ProcessBuilderInterface::class); |
| 67 | + $this->processQueue = $this->prophesize(ProcessQueueInterface::class); |
| 68 | + $this->input = $this->prophesize(InputInterface::class); |
| 69 | + $this->output = $this->prophesize(OutputInterface::class); |
| 70 | + $this->process = $this->prophesize(Process::class); |
| 71 | + |
| 72 | + $this->fileLocator->locate(RefactorCommand::CONFIG) |
| 73 | + ->willReturn(self::CONFIG_PATH); |
| 74 | + |
| 75 | + $this->input->getOption('fix')->willReturn(false); |
72 | 76 |
|
73 | | - $this->withConfigFile(RefactorCommand::CONFIG); |
| 77 | + $this->processBuilder->withArgument(Argument::cetera()) |
| 78 | + ->willReturn($this->processBuilder->reveal()); |
| 79 | + |
| 80 | + $this->processBuilder->build('vendor/bin/rector') |
| 81 | + ->willReturn($this->process->reveal()); |
| 82 | + |
| 83 | + $this->processQueue->run($this->output->reveal()) |
| 84 | + ->willReturn(RefactorCommand::SUCCESS); |
| 85 | + |
| 86 | + $this->command = new RefactorCommand( |
| 87 | + $this->fileLocator->reveal(), |
| 88 | + $this->processBuilder->reveal(), |
| 89 | + $this->processQueue->reveal() |
| 90 | + ); |
74 | 91 | } |
75 | 92 |
|
76 | | - /** |
77 | | - * @return void |
78 | | - */ |
79 | 93 | #[Test] |
80 | | - public function executeWithLocalConfigWillRunRectorProcessWithDevToolsConfigFile(): void |
| 94 | + public function commandWillSetExpectedNameDescriptionAndHelp(): void |
81 | 95 | { |
82 | | - $this->withConfigFile(RefactorCommand::CONFIG, true); |
83 | | - |
84 | | - $this->willRunProcessWithCallback(function (Process $process): bool { |
85 | | - $commandLine = $process->getCommandLine(); |
86 | | - |
87 | | - $path = getcwd() . '/' . RefactorCommand::CONFIG; |
| 96 | + self::assertSame('refactor', $this->command->getName()); |
| 97 | + self::assertSame('Runs Rector for code refactoring.', $this->command->getDescription()); |
| 98 | + self::assertSame('This command runs Rector to refactor your code.', $this->command->getHelp()); |
| 99 | + self::assertSame(['rector'], $this->command->getAliases()); |
| 100 | + } |
88 | 101 |
|
89 | | - return str_contains($commandLine, 'vendor/bin/rector') |
90 | | - && str_contains($commandLine, 'process') |
91 | | - && str_contains($commandLine, '--config') |
92 | | - && str_contains($commandLine, $path) |
93 | | - && str_contains($commandLine, '--dry-run'); |
94 | | - }); |
| 102 | + #[Test] |
| 103 | + public function commandWillHaveExpectedOptions(): void |
| 104 | + { |
| 105 | + $definition = $this->command->getDefinition(); |
95 | 106 |
|
96 | | - $this->invokeExecute(); |
| 107 | + self::assertTrue($definition->hasOption('fix')); |
| 108 | + self::assertTrue($definition->hasOption('config')); |
97 | 109 | } |
98 | 110 |
|
99 | | - /** |
100 | | - * @return void |
101 | | - */ |
102 | 111 | #[Test] |
103 | | - public function executeWithoutLocalConfigWillRunRectorProcessWithDevToolsConfigFile(): void |
| 112 | + public function executeWillRunRectorProcessWithDryRunWhenFixIsFalse(): void |
104 | 113 | { |
105 | | - $this->withConfigFile(RefactorCommand::CONFIG); |
| 114 | + $this->processBuilder->withArgument('process') |
| 115 | + ->shouldBeCalledOnce() |
| 116 | + ->willReturn($this->processBuilder->reveal()); |
106 | 117 |
|
107 | | - $this->willRunProcessWithCallback(function (Process $process): bool { |
108 | | - $commandLine = $process->getCommandLine(); |
109 | | - $path = getcwd() . '/' . RefactorCommand::CONFIG; |
| 118 | + $this->processBuilder->withArgument('--config') |
| 119 | + ->shouldBeCalledOnce() |
| 120 | + ->willReturn($this->processBuilder->reveal()); |
110 | 121 |
|
111 | | - return str_contains($commandLine, 'vendor/bin/rector') |
112 | | - && str_contains($commandLine, 'process') |
113 | | - && str_contains($commandLine, '--config') |
114 | | - && str_contains($commandLine, $path) |
115 | | - && str_contains($commandLine, '--dry-run'); |
116 | | - }); |
| 122 | + $this->processBuilder->withArgument(self::CONFIG_PATH) |
| 123 | + ->shouldBeCalledOnce() |
| 124 | + ->willReturn($this->processBuilder->reveal()); |
117 | 125 |
|
118 | | - $this->invokeExecute(); |
| 126 | + $this->processBuilder->withArgument('--dry-run') |
| 127 | + ->shouldBeCalledOnce() |
| 128 | + ->willReturn($this->processBuilder->reveal()); |
| 129 | + |
| 130 | + $this->processQueue->add($this->process->reveal()) |
| 131 | + ->shouldBeCalledOnce(); |
| 132 | + |
| 133 | + $result = $this->executeCommand(); |
| 134 | + |
| 135 | + self::assertSame(RefactorCommand::SUCCESS, $result); |
119 | 136 | } |
120 | 137 |
|
121 | | - /** |
122 | | - * @return void |
123 | | - */ |
124 | 138 | #[Test] |
125 | | - public function executeWithFixOptionWillRunRectorProcessWithoutDryRunOption(): void |
| 139 | + public function executeWillRunRectorProcessWithoutDryRunWhenFixIsTrue(): void |
126 | 140 | { |
127 | 141 | $this->input->getOption('fix') |
128 | | - ->willReturn(true) |
129 | | - ->shouldBeCalledOnce(); |
| 142 | + ->willReturn(true); |
130 | 143 |
|
131 | | - $this->withConfigFile(RefactorCommand::CONFIG, true); |
| 144 | + $this->processBuilder->withArgument('--dry-run') |
| 145 | + ->shouldNotBeCalled(); |
132 | 146 |
|
133 | | - $this->willRunProcessWithCallback(function (Process $process): bool { |
134 | | - $commandLine = $process->getCommandLine(); |
| 147 | + $this->processQueue->add($this->process->reveal()) |
| 148 | + ->shouldBeCalledOnce(); |
135 | 149 |
|
136 | | - return str_contains($commandLine, 'vendor/bin/rector') |
137 | | - && str_contains($commandLine, 'process') |
138 | | - && str_contains($commandLine, '--config') |
139 | | - && str_contains($commandLine, getcwd() . '/' . RefactorCommand::CONFIG) |
140 | | - && ! str_contains($commandLine, '--dry-run'); |
141 | | - }); |
| 150 | + $result = $this->executeCommand(); |
142 | 151 |
|
143 | | - $this->invokeExecute(); |
| 152 | + self::assertSame(RefactorCommand::SUCCESS, $result); |
144 | 153 | } |
145 | 154 |
|
146 | | - /** |
147 | | - * @return void |
148 | | - */ |
149 | | - #[Test] |
150 | | - public function executeWillReturnFailureIfProcessFails(): void |
| 155 | + private function executeCommand(): int |
151 | 156 | { |
152 | | - $this->willRunProcessWithCallback(static fn(): true => true, false); |
| 157 | + $reflectionMethod = new ReflectionMethod($this->command, 'execute'); |
153 | 158 |
|
154 | | - self::assertSame(RefactorCommand::FAILURE, $this->invokeExecute()); |
| 159 | + return $reflectionMethod->invoke($this->command, $this->input->reveal(), $this->output->reveal()); |
155 | 160 | } |
156 | 161 | } |
0 commit comments