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
|
|
|
class GenerateCommandTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
public function testExecute() |
29
|
|
|
{ |
30
|
|
|
$commandTester = $this->buildCommand($this->mockContainer()); |
31
|
|
|
$commandTester->execute([ |
32
|
|
|
'--dest' => 'src', |
33
|
|
|
'bundle' => ['SonataAcmeBundle'], |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$this->assertContains( |
37
|
|
|
'done!', |
38
|
|
|
$commandTester->getDisplay() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testExecuteWrongDest() |
43
|
|
|
{ |
44
|
|
|
$commandTester = $this->buildCommand($this->getMockBuilder(ContainerInterface::class)->getMock()); |
45
|
|
|
|
46
|
|
|
$this->expectException(\RuntimeException::class); |
47
|
|
|
$this->expectExceptionMessage("The provided destination folder 'fakedest' does not exist!"); |
48
|
|
|
|
49
|
|
|
$commandTester->execute([ |
50
|
|
|
'--dest' => 'fakedest', |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testNoArgument() |
55
|
|
|
{ |
56
|
|
|
$commandTester = $this->buildCommand($this->mockContainerWithKernel()); |
57
|
|
|
|
58
|
|
|
$commandTester->execute([ |
59
|
|
|
'--dest' => 'src', |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$this->assertContains( |
63
|
|
|
'You must provide a bundle name!', |
64
|
|
|
$commandTester->getDisplay() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testFakeBundleName() |
69
|
|
|
{ |
70
|
|
|
$commandTester = $this->buildCommand($this->mockContainerWithKernel()); |
71
|
|
|
|
72
|
|
|
$this->expectException(\RuntimeException::class); |
73
|
|
|
$this->expectExceptionMessage("The bundle 'FakeBundle' does not exist or not registered in the kernel!"); |
74
|
|
|
|
75
|
|
|
$commandTester->execute([ |
76
|
|
|
'--dest' => 'src', |
77
|
|
|
'bundle' => ['FakeBundle'], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$this->assertContains( |
81
|
|
|
'You must provide a bundle name!', |
82
|
|
|
$commandTester->getDisplay() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testNotExtendableBundle() |
87
|
|
|
{ |
88
|
|
|
$commandTester = $this->buildCommand($this->mockContainerWithKernel(new \Symfony\Bundle\NotExtendableBundle())); |
89
|
|
|
|
90
|
|
|
$commandTester->execute([ |
91
|
|
|
'--dest' => 'src', |
92
|
|
|
'bundle' => ['NotExtendableBundle'], |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$this->assertContains( |
96
|
|
|
sprintf('Ignoring bundle : "Symfony\Bundle\NotExtendableBundle"'), |
97
|
|
|
$commandTester->getDisplay() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testInvalidFolderStructure() |
102
|
|
|
{ |
103
|
|
|
$commandTester = $this->buildCommand( |
104
|
|
|
$this->mockContainerWithKernel(new \Application\Sonata\NotExtendableBundle()) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$commandTester->execute([ |
108
|
|
|
'--dest' => 'src', |
109
|
|
|
'bundle' => ['NotExtendableBundle'], |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->assertContains( |
113
|
|
|
sprintf('Application\Sonata\NotExtendableBundle : wrong directory structure'), |
114
|
|
|
$commandTester->getDisplay() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function buildCommand($container) |
119
|
|
|
{ |
120
|
|
|
$application = new Application(); |
121
|
|
|
$generateCommand = new GenerateCommand(); |
122
|
|
|
$generateCommand->setContainer($container); |
123
|
|
|
$application->add($generateCommand); |
124
|
|
|
|
125
|
|
|
$command = $application->find('sonata:easy-extends:generate'); |
126
|
|
|
|
127
|
|
|
return new CommandTester($command); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function mockContainer() |
131
|
|
|
{ |
132
|
|
|
$containerMock = $this->mockContainerWithKernel(); |
133
|
|
|
|
134
|
|
|
$containerMock->expects($this->exactly(6)) |
135
|
|
|
->method('get') |
136
|
|
|
->willReturn($this->mockGenerator()); |
137
|
|
|
|
138
|
|
|
return $containerMock; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function mockContainerWithKernel($kernelReturnValue = null) |
142
|
|
|
{ |
143
|
|
|
$containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
144
|
|
|
|
145
|
|
|
$containerMock->expects($this->at(0)) |
146
|
|
|
->method('get') |
147
|
|
|
->with('kernel') |
148
|
|
|
->willReturn($this->mockKernel($kernelReturnValue)); |
149
|
|
|
|
150
|
|
|
return $containerMock; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function mockKernel($returnValue) |
154
|
|
|
{ |
155
|
|
|
$kernelMock = $this->getMockBuilder(KernelInterface::class)->getMock(); |
156
|
|
|
|
157
|
|
|
$kernelMock->expects($this->once()) |
158
|
|
|
->method('getBundles') |
159
|
|
|
->willReturn([ |
160
|
|
|
$returnValue ?: new SonataAcmeBundle(), |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
return $kernelMock; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function mockGenerator() |
167
|
|
|
{ |
168
|
|
|
$generatorMock = $this->getMockBuilder(GeneratorInterface::class)->getMock(); |
169
|
|
|
|
170
|
|
|
$generatorMock->expects($this->exactly(5)) |
171
|
|
|
->method('generate'); |
172
|
|
|
|
173
|
|
|
return $generatorMock; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|