Skip to content

Commit 6cbb7b2

Browse files
committed
refactor(tests): Refactor CodeStyleCommandTest and DevToolsTest for improved dependency handling and add new tests for ProcessQueue
Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent d6518fe commit 6cbb7b2

3 files changed

Lines changed: 223 additions & 90 deletions

File tree

tests/Console/Command/CodeStyleCommandTest.php

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,71 @@
1919
namespace FastForward\DevTools\Tests\Console\Command;
2020

2121
use 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;
2424
use PHPUnit\Framework\Attributes\CoversClass;
2525
use PHPUnit\Framework\Attributes\Test;
2626
use PHPUnit\Framework\TestCase;
27+
use Prophecy\Argument;
2728
use Prophecy\PhpUnit\ProphecyTrait;
29+
use Prophecy\Prophecy\ObjectProphecy;
30+
use ReflectionMethod;
2831
use 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)]
3337
final 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
}

tests/Console/DevToolsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818

1919
namespace FastForward\DevTools\Tests\Console;
2020

21+
use FastForward\DevTools\Console\CommandLoader\DevToolsCommandLoader;
2122
use FastForward\DevTools\Console\DevTools;
23+
use FastForward\DevTools\ServiceProvider\DevToolsServiceProvider;
2224
use Override;
2325
use PHPUnit\Framework\Attributes\CoversClass;
2426
use PHPUnit\Framework\Attributes\Test;
27+
use PHPUnit\Framework\Attributes\UsesClass;
2528
use PHPUnit\Framework\TestCase;
2629
use Prophecy\PhpUnit\ProphecyTrait;
2730
use Prophecy\Prophecy\ObjectProphecy;
2831
use ReflectionMethod;
32+
use ReflectionProperty;
2933
use Symfony\Component\Console\Command\Command;
3034
use Symfony\Component\Console\Command\CompleteCommand;
3135
use Symfony\Component\Console\Command\DumpCompletionCommand;
@@ -34,6 +38,8 @@
3438
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
3539

3640
#[CoversClass(DevTools::class)]
41+
#[UsesClass(DevToolsCommandLoader::class)]
42+
#[UsesClass(DevToolsServiceProvider::class)]
3743
final class DevToolsTest extends TestCase
3844
{
3945
use ProphecyTrait;
@@ -97,6 +103,21 @@ public function __construct()
97103
self::assertSame($customCommand, $this->devTools->get('custom'));
98104
}
99105

106+
/**
107+
* @return void
108+
*/
109+
#[Test]
110+
public function createWillReturnInstanceOfDevTools(): void
111+
{
112+
$reflectionProperty = new ReflectionProperty(DevTools::class, 'container');
113+
$reflectionProperty->setValue(null, null);
114+
115+
$devTools = DevTools::create();
116+
117+
self::assertInstanceOf(DevTools::class, $devTools);
118+
self::assertSame($devTools, DevTools::create());
119+
}
120+
100121
/**
101122
* @param DevTools $devTools
102123
*

0 commit comments

Comments
 (0)