Completed
Pull Request — 2.x (#165)
by
unknown
01:32
created

GenerateCommandTest::testExecuteWrongDest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
4
namespace Sonata\EasyExtendsBundle\Tests\Command;
5
6
use PHPUnit\Framework\TestCase;
7
use Sonata\AcmeBundle\SonataAcmeBundle;
8
use Sonata\EasyExtendsBundle\Command\GenerateCommand;
9
use Sonata\EasyExtendsBundle\Generator\GeneratorInterface;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Tester\CommandTester;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpKernel\KernelInterface;
14
15
/**
16
 * @author Marko Kunic <[email protected]>
17
 */
18
class GenerateCommandTest extends TestCase
19
{
20
    public function testExecute()
21
    {
22
        $commandTester = $this->buildCommand($this->mockContainer());
23
        $commandTester->execute([
24
            '--dest' => 'src',
25
            'bundle' => ['SonataAcmeBundle'],
26
        ]);
27
28
        $this->assertContains(
29
            'done!',
30
            $commandTester->getDisplay()
31
        );
32
    }
33
34
    private function buildCommand($conatiner)
35
    {
36
        $application = new Application();
37
        $generateCommand = new GenerateCommand();
38
        $generateCommand->setContainer($conatiner);
39
        $application->add($generateCommand);
40
41
        $command = $application->find('sonata:easy-extends:generate');
42
        return new CommandTester($command);
43
    }
44
45
    private function mockContainer()
46
    {
47
        $containerMock = $this->mockContainerWithKernel();
48
49
        $containerMock->expects($this->exactly(6))
50
            ->method('get')
51
            ->willReturn($this->mockGenerator());
52
53
        return $containerMock;
54
    }
55
56
    private function mockContainerWithKernel($kernelReturnValue = null)
57
    {
58
        $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock();
59
60
        $containerMock->expects($this->at(0))
61
            ->method('get')
62
            ->with('kernel')
63
            ->willReturn($this->mockKernel($kernelReturnValue));
64
65
        return $containerMock;
66
    }
67
68
    private function mockKernel($returnValue)
69
    {
70
        $kernelMock = $this->getMockBuilder(KernelInterface::class)->getMock();
71
72
        $kernelMock->expects($this->once())
73
            ->method('getBundles')
74
            ->willReturn([
75
                $returnValue ?: new SonataAcmeBundle()
76
            ]);
77
78
        return $kernelMock;
79
    }
80
81
    private function mockGenerator()
82
    {
83
        $generatorMock = $this->getMockBuilder(GeneratorInterface::class)->getMock();
84
85
        $generatorMock->expects($this->exactly(5))
86
            ->method('generate');
87
88
        return $generatorMock;
89
    }
90
91
    public function testExecuteWrongDest()
92
    {
93
        $commandTester = $this->buildCommand($this->getMockBuilder(ContainerInterface::class)->getMock());
94
95
        $this->expectException(\RuntimeException::class);
96
        $this->expectExceptionMessage("The provided destination folder 'fakedest' does not exist!");
97
98
        $commandTester->execute([
99
            '--dest' => 'fakedest',
100
        ]);
101
    }
102
103
    public function testNoArgument()
104
    {
105
        $commandTester = $this->buildCommand($this->mockContainerWithKernel());
106
107
        $commandTester->execute([
108
            '--dest' => 'src',
109
        ]);
110
111
        $this->assertContains(
112
            'You must provide a bundle name!',
113
            $commandTester->getDisplay()
114
        );
115
    }
116
117
    public function testFakeBundleName()
118
    {
119
        $commandTester = $this->buildCommand($this->mockContainerWithKernel());
120
121
        $this->expectException(\RuntimeException::class);
122
        $this->expectExceptionMessage("The bundle 'FakeBundle' does not exist or not defined in the kernel file!");
123
124
        $commandTester->execute([
125
            '--dest' => 'src',
126
            'bundle' => ['FakeBundle'],
127
        ]);
128
129
        $this->assertContains(
130
            'You must provide a bundle name!',
131
            $commandTester->getDisplay()
132
        );
133
    }
134
135
    public function testNotExtendableBundle()
136
    {
137
138
        $commandTester = $this->buildCommand($this->mockContainerWithKernel(new \Symfony\Bundle\NotExtendableBundle()));
139
140
        $commandTester->execute([
141
            '--dest' => 'src',
142
            'bundle' => ['NotExtendableBundle'],
143
        ]);
144
145
        $this->assertContains(
146
            sprintf('Ignoring bundle : "Symfony\Bundle\NotExtendableBundle"'),
147
            $commandTester->getDisplay()
148
        );
149
    }
150
151
    public function testInvalidFolderStructure()
152
    {
153
        $commandTester = $this->buildCommand(
154
            $this->mockContainerWithKernel(new \Application\Sonata\NotExtendableBundle())
155
        );
156
157
        $commandTester->execute([
158
            '--dest' => 'src',
159
            'bundle' => ['NotExtendableBundle'],
160
        ]);
161
162
        $this->assertContains(
163
            sprintf('Application\Sonata\NotExtendableBundle : wrong folder structure'),
164
            $commandTester->getDisplay()
165
        );
166
    }
167
}
168