Completed
Pull Request — master (#169)
by
unknown
09:21
created

GenerateCommandTest::buildCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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