GenerateCommandTest::testExecuteWrongDest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Tests\Command;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AcmeBundle\SonataAcmeBundle;
18
use Sonata\EasyExtendsBundle\Command\GenerateCommand;
19
use Sonata\EasyExtendsBundle\Generator\GeneratorInterface;
20
use Symfony\Component\Console\Tester\CommandTester;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\HttpKernel\KernelInterface;
23
24
/**
25
 * @author Marko Kunic <[email protected]>
26
 */
27
final class GenerateCommandTest extends TestCase
28
{
29
    /**
30
     * @dataProvider executeData
31
     */
32
    public function testExecute($args): void
33
    {
34
        $commandTester = $this->buildCommand($this->mockContainer());
35
        $commandTester->execute($args);
36
37
        $this->assertStringContainsString(
38
            'done!',
39
            $commandTester->getDisplay()
40
        );
41
    }
42
43
    public function executeData()
44
    {
45
        return [
46
            [
47
                [
48
                    '--dest' => 'src',
49
                    'bundle' => ['SonataAcmeBundle'],
50
                ],
51
            ],
52
            [
53
                [
54
                    '--dest' => 'src',
55
                    'bundle' => ['SonataAcmeBundle'],
56
                    '--namespace' => 'Application\\Sonata',
57
                ],
58
            ],
59
            [
60
                [
61
                    '--dest' => 'src',
62
                    'bundle' => ['SonataAcmeBundle'],
63
                    '--namespace_prefix' => 'App',
64
                ],
65
            ],
66
            [
67
                [
68
                    '--dest' => 'src',
69
                    'bundle' => ['SonataAcmeBundle'],
70
                    '--namespace' => 'Application\\Sonata',
71
                    '--namespace_prefix' => 'App',
72
                ],
73
            ],
74
        ];
75
    }
76
77
    public function testExecuteWrongDest(): void
78
    {
79
        $commandTester = $this->buildCommand($this->createMock(ContainerInterface::class));
80
81
        $this->expectException(\RuntimeException::class);
82
        $this->expectExceptionMessage("The provided destination folder 'fakedest' does not exist!");
83
84
        $commandTester->execute([
85
            '--dest' => 'fakedest',
86
        ]);
87
    }
88
89
    public function testNoArgument(): void
90
    {
91
        $commandTester = $this->buildCommand($this->mockContainerWithKernel());
92
93
        $commandTester->execute([
94
            '--dest' => 'src',
95
        ]);
96
97
        $this->assertStringContainsString(
98
            'You must provide a bundle name!',
99
            $commandTester->getDisplay()
100
        );
101
    }
102
103
    public function testFakeBundleName(): void
104
    {
105
        $commandTester = $this->buildCommand($this->mockContainerWithKernel());
106
107
        $this->expectException(\RuntimeException::class);
108
        $this->expectExceptionMessage("The bundle 'FakeBundle' does not exist or is not registered in the kernel!");
109
110
        $commandTester->execute([
111
            '--dest' => 'src',
112
            'bundle' => ['FakeBundle'],
113
        ]);
114
115
        $this->assertStringContainsString(
116
            'You must provide a bundle name!',
117
            $commandTester->getDisplay()
118
        );
119
    }
120
121
    public function testNotExtendableBundle(): void
122
    {
123
        $commandTester = $this->buildCommand($this->mockContainerWithKernel(new \Symfony\Bundle\NotExtendableBundle()));
124
125
        $commandTester->execute([
126
            '--dest' => 'src',
127
            'bundle' => ['NotExtendableBundle'],
128
        ]);
129
130
        $this->assertStringContainsString(
131
            sprintf('Ignoring bundle : "Symfony\Bundle\NotExtendableBundle"'),
132
            $commandTester->getDisplay()
133
        );
134
    }
135
136
    public function testInvalidFolderStructure(): void
137
    {
138
        $commandTester = $this->buildCommand(
139
            $this->mockContainerWithKernel(new \Application\Sonata\NotExtendableBundle())
140
        );
141
142
        $commandTester->execute([
143
            '--dest' => 'src',
144
            'bundle' => ['NotExtendableBundle'],
145
        ]);
146
147
        $this->assertStringContainsString(
148
            sprintf('Application\Sonata\NotExtendableBundle : wrong directory structure'),
149
            $commandTester->getDisplay()
150
        );
151
    }
152
153
    private function buildCommand($container)
154
    {
155
        $command = new GenerateCommand();
156
        $command->setContainer($container);
157
158
        return new CommandTester($command);
159
    }
160
161
    private function mockContainer()
162
    {
163
        $containerMock = $this->mockContainerWithKernel();
164
165
        $containerMock->expects($this->exactly(6))
166
            ->method('get')
167
            ->willReturn($this->mockGenerator());
168
169
        return $containerMock;
170
    }
171
172
    private function mockContainerWithKernel($kernelReturnValue = null)
173
    {
174
        $containerMock = $this->createMock(ContainerInterface::class);
175
176
        $containerMock->expects($this->at(0))
177
            ->method('get')
178
            ->with('kernel')
179
            ->willReturn($this->mockKernel($kernelReturnValue));
180
181
        return $containerMock;
182
    }
183
184
    private function mockKernel($returnValue)
185
    {
186
        $kernelMock = $this->createMock(KernelInterface::class);
187
188
        $kernelMock->expects($this->once())
189
            ->method('getBundles')
190
            ->willReturn([
191
                $returnValue ?: new SonataAcmeBundle(),
192
            ]);
193
194
        return $kernelMock;
195
    }
196
197
    private function mockGenerator()
198
    {
199
        $generatorMock = $this->createMock(GeneratorInterface::class);
200
201
        $generatorMock->expects($this->exactly(5))
202
            ->method('generate');
203
204
        return $generatorMock;
205
    }
206
}
207