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