|
2 | 2 |
|
3 | 3 | namespace Tests\Cases;
|
4 | 4 |
|
| 5 | +use Composer\Config; |
| 6 | +use Helldar\EnvSync\Frameworks\Symfony\Console\Sync; |
| 7 | +use Helldar\EnvSync\Services\Syncer; |
| 8 | +use PHPUnit\Framework\TestCase; |
5 | 9 | use Symfony\Bundle\FrameworkBundle\Console\Application;
|
6 |
| -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
7 | 10 | use Symfony\Component\Console\Command\Command;
|
8 | 11 | use Symfony\Component\Console\Tester\CommandTester;
|
9 |
| -use Symfony\Component\HttpKernel\KernelInterface; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | +use Symfony\Component\HttpKernel\Kernel; |
10 | 14 | use Tests\Concerns\Configurable;
|
11 | 15 | use Tests\Concerns\Files;
|
12 | 16 |
|
13 |
| -abstract class SymfonyTestCase extends KernelTestCase |
| 17 | +abstract class SymfonyTestCase extends TestCase |
14 | 18 | {
|
15 | 19 | use Configurable;
|
16 | 20 | use Files;
|
17 | 21 |
|
18 |
| - protected function call(string $name, array $options = []): string |
| 22 | + /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
| 23 | + protected $container; |
| 24 | + |
| 25 | + /** @var \Symfony\Bundle\FrameworkBundle\Console\Application */ |
| 26 | + protected $application; |
| 27 | + |
| 28 | + protected function setUp(): void |
19 | 29 | {
|
20 |
| - $command = $this->application()->find($name); |
| 30 | + $this->mockContainer(); |
| 31 | + $this->mockApplication(); |
| 32 | + $this->mockCommand(); |
| 33 | + } |
21 | 34 |
|
22 |
| - $tester = $this->tester($command); |
| 35 | + protected function mockContainer(): void |
| 36 | + { |
| 37 | + $this->container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return \Symfony\Component\HttpKernel\Kernel|\PHPUnit\Framework\MockObject\MockBuilder |
| 42 | + */ |
| 43 | + protected function mockKernel() |
| 44 | + { |
| 45 | + $kernel = $this->getMockBuilder(Kernel::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
23 | 48 |
|
24 |
| - $tester->execute($options); |
| 49 | + $kernel->expects($this->once()) |
| 50 | + ->method('getBundles') |
| 51 | + ->will($this->returnValue([])); |
25 | 52 |
|
26 |
| - return $tester->getDisplay(); |
| 53 | + $kernel->expects($this->any()) |
| 54 | + ->method('getContainer') |
| 55 | + ->will($this->returnValue($this->container)); |
| 56 | + |
| 57 | + return $kernel; |
| 58 | + } |
| 59 | + |
| 60 | + protected function mockApplication(): void |
| 61 | + { |
| 62 | + $this->application = new Application($this->mockKernel()); |
| 63 | + } |
| 64 | + |
| 65 | + protected function mockCommand(): void |
| 66 | + { |
| 67 | + $command = $this->getCommand(); |
| 68 | + |
| 69 | + $this->application->add($command); |
27 | 70 | }
|
28 | 71 |
|
29 |
| - protected function application(): Application |
| 72 | + protected function composerConfig(): Config |
30 | 73 | {
|
31 |
| - return new Application($this->kernel()); |
| 74 | + return new Config(); |
32 | 75 | }
|
33 | 76 |
|
34 |
| - protected function kernel(): KernelInterface |
| 77 | + protected function getSyncer(): Syncer |
35 | 78 | {
|
36 |
| - return static::createKernel(); |
| 79 | + return Syncer::make(); |
| 80 | + } |
| 81 | + |
| 82 | + protected function getCommand(): Sync |
| 83 | + { |
| 84 | + return new Sync($this->composerConfig(), $this->getSyncer()); |
37 | 85 | }
|
38 | 86 |
|
39 | 87 | protected function tester(Command $command): CommandTester
|
40 | 88 | {
|
41 | 89 | return new CommandTester($command);
|
42 | 90 | }
|
| 91 | + |
| 92 | + protected function call(string $name, array $options = []) |
| 93 | + { |
| 94 | + $command = $this->application->find($name); |
| 95 | + |
| 96 | + $tester = $this->tester($command); |
| 97 | + |
| 98 | + $tester->execute(array_merge(['command' => $name], $options)); |
| 99 | + |
| 100 | + return $tester->getDisplay(); |
| 101 | + } |
43 | 102 | }
|
0 commit comments