-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathContainerDebugCommandTest.php
348 lines (266 loc) · 13.1 KB
/
ContainerDebugCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ContainerExcluded;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* @group functional
*/
class ContainerDebugCommandTest extends AbstractWebTestCase
{
public function testDumpContainerIfNotExists()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container']);
$this->assertFileExists(static::getContainer()->getParameter('debug.container.dump'));
}
public function testNoDebug()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => false]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container']);
$this->assertStringContainsString('public', $tester->getDisplay());
}
public function testNoDumpedXML()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true, 'debug.container.dump' => false]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container']);
$this->assertStringContainsString('public', $tester->getDisplay());
}
public function testPrivateAlias()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--show-hidden' => true]);
$this->assertStringNotContainsString('public', $tester->getDisplay());
$this->assertStringNotContainsString('private_alias', $tester->getDisplay());
$tester->run(['command' => 'debug:container']);
$this->assertStringContainsString('public', $tester->getDisplay());
$this->assertStringContainsString('private_alias', $tester->getDisplay());
$tester->run(['command' => 'debug:container', 'name' => 'private_alias']);
$this->assertStringContainsString('The "private_alias" service or alias has been removed', $tester->getDisplay());
}
public function testDeprecatedServiceAndAlias()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', 'name' => 'deprecated', '--format' => 'txt']);
$this->assertStringContainsString('[WARNING] The "deprecated" service is deprecated since foo/bar 1.9 and will be removed in 2.0', $tester->getDisplay());
$tester->run(['command' => 'debug:container', 'name' => 'deprecated_alias', '--format' => 'txt']);
$this->assertStringContainsString('[WARNING] The "deprecated_alias" alias is deprecated since foo/bar 1.9 and will be removed in 2.0', $tester->getDisplay());
}
public function testExcludedService()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container']);
$this->assertStringNotContainsString(ContainerExcluded::class, $tester->getDisplay());
}
/**
* @dataProvider provideIgnoreBackslashWhenFindingService
*/
public function testIgnoreBackslashWhenFindingService(string $validServiceId)
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', 'name' => $validServiceId]);
$this->assertStringNotContainsString('No services found', $tester->getDisplay());
}
public function testTagsPartialSearch()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
$tester = new ApplicationTester($application);
$tester->setInputs(['0']);
$tester->run(['command' => 'debug:container', '--tag' => 'kernel.'], ['decorated' => false]);
$this->assertStringMatchesFormat(<<<EOTXT
Select one of the following tags to display its information:
%A
[%d] kernel.reset
%A
Symfony Container Services Tagged with "kernel.%a" Tag
%A
EOTXT,
$tester->getDisplay()
);
}
public function testDescribeEnvVars()
{
putenv('REAL=value');
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--env-vars' => true], ['decorated' => false]);
$this->assertStringMatchesFormat(<<<'TXT'
Symfony Container Environment Variables
=======================================
--------- ----------------- ------------%w
Name Default value Real value%w
--------- ----------------- ------------%w
JSON "[1, "2.5", 3]" n/a%w
REAL n/a "value"%w
UNKNOWN n/a n/a%w
--------- ----------------- ------------%w
// Note real values might be different between web and CLI.%w
[WARNING] The following variables are missing:%w
* UNKNOWN
TXT
, $tester->getDisplay(true));
putenv('REAL');
}
public function testDescribeEnvVar()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);
$this->assertStringContainsString(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
}
public function testGetDeprecation()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
touch($path);
file_put_contents($path, serialize([[
'type' => 16384,
'message' => 'The "Symfony\Bundle\FrameworkBundle\Controller\Controller" class is deprecated since Symfony 4.2, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.',
'file' => '/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php',
'line' => 17,
'trace' => [[
'file' => '/home/hamza/projet/contrib/sf/src/Controller/DefaultController.php',
'line' => 9,
'function' => 'spl_autoload_call',
]],
'count' => 1,
]]));
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
$this->assertStringContainsString('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
}
public function testGetDeprecationNone()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
touch($path);
file_put_contents($path, serialize([]));
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('[OK] There are no deprecations in the logs!', $tester->getDisplay());
}
public function testGetDeprecationNoFile()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.build_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
@unlink($path);
$application = new Application(static::$kernel);
$application->setAutoExit(false);
@unlink(static::getContainer()->getParameter('debug.container.dump'));
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('[WARNING] The deprecation file does not exist', $tester->getDisplay());
}
public static function provideIgnoreBackslashWhenFindingService(): array
{
return [
[BackslashClass::class],
['FixturesBackslashClass'],
['\\'.BackslashClass::class],
];
}
/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions, array $notExpectedSuggestions = [])
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$tester = new CommandCompletionTester($application->find('debug:container'));
$suggestions = $tester->complete($input);
foreach ($expectedSuggestions as $expectedSuggestion) {
$this->assertContains($expectedSuggestion, $suggestions);
}
foreach ($notExpectedSuggestions as $notExpectedSuggestion) {
$this->assertNotContains($notExpectedSuggestion, $suggestions);
}
}
public static function provideCompletionSuggestions(): iterable
{
$serviceId = 'console.command.container_debug';
$hiddenServiceId = '.console.command.container_debug.lazy';
$interfaceServiceId = HttpKernelInterface::class;
yield 'name' => [
[''],
[$serviceId, $interfaceServiceId],
[$hiddenServiceId],
];
yield 'name (with hidden)' => [
['--show-hidden', ''],
[$serviceId, $interfaceServiceId, $hiddenServiceId],
];
yield 'name (with current value)' => [
['--show-hidden', 'console'],
[$serviceId, $hiddenServiceId],
[$interfaceServiceId],
];
yield 'name (no suggestion with --tags)' => [
['--tags', ''],
[],
[$serviceId, $interfaceServiceId, $hiddenServiceId],
];
yield 'option --tag' => [
['--tag', ''],
['console.command'],
];
yield 'option --parameter' => [
['--parameter', ''],
['kernel.debug'],
];
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
];
}
}