Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dda4e7c

Browse files
bug #50546 [FrameworkBundle] Fix debug:config & config:dump in debug mode (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [FrameworkBundle] Fix `debug:config` & `config:dump` in debug mode | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47623 | License | MIT | Doc PR | - Displaying configs for extensions without a bundle using the `debug:config` & `config:dump-reference` commands works depending on whether debug is `true` or `false`. The reason behind this is the following code: https://github.com/symfony/symfony/blob/b4128fdefea4ff95b27861bf65ba789d24868df1/src/Symfony/Bundle/FrameworkBundle/Command/BuildDebugContainerTrait.php#L42-L60 When debug is `true` the extensions are never loaded in the container, so the commands don't work. There are even tests for these cases but they are all executed with debug `false`. This PR aims to make the commands work with both debug `true` & `false`. Another problem is that these extensions are not visible in the list of available extensions and are not offered by the completion feature, but since those seem more like new features I've created a separate PR for 6.4: #50548 Commits ------- 2824fc5 [FrameworkBundle] Fix `debug:config` & `config:dump` in debug mode
2 parents a5e6d8d + 2824fc5 commit dda4e7c

File tree

4 files changed

+162
-66
lines changed

4 files changed

+162
-66
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/BuildDebugContainerTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ protected function getContainerBuilder(KernelInterface $kernel): ContainerBuilde
5050
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
5151
$container->compile();
5252
} else {
53-
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
53+
$buildContainer = \Closure::bind(function () {
54+
$containerBuilder = $this->getContainerBuilder();
55+
$this->prepareContainer($containerBuilder);
56+
57+
return $containerBuilder;
58+
}, $kernel, \get_class($kernel));
59+
$container = $buildContainer();
60+
(new XmlFileLoader($container, new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
5461
$locatorPass = new ServiceLocatorTagPass();
5562
$locatorPass->process($container);
5663

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ private function getConfigForExtension(ExtensionInterface $extension, ContainerB
176176

177177
// Fall back to default config if the extension has one
178178

179-
if (!$extension instanceof ConfigurationExtensionInterface) {
179+
if (!$extension instanceof ConfigurationExtensionInterface && !$extension instanceof ConfigurationInterface) {
180180
throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
181181
}
182182

183183
$configs = $container->getExtensionConfig($extensionAlias);
184-
$configuration = $extension->getConfiguration($configs, $container);
184+
$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($configs, $container);
185185
$this->validateConfiguration($extension, $configuration);
186186

187187
return (new Processor())->processConfiguration($configuration, $configs);

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,114 +23,159 @@
2323
*/
2424
class ConfigDebugCommandTest extends AbstractWebTestCase
2525
{
26-
private $application;
27-
28-
protected function setUp(): void
26+
/**
27+
* @testWith [true]
28+
* [false]
29+
*/
30+
public function testDumpKernelExtension(bool $debug)
2931
{
30-
$kernel = static::createKernel(['test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
31-
$this->application = new Application($kernel);
32-
$this->application->doRun(new ArrayInput([]), new NullOutput());
32+
$tester = $this->createCommandTester($debug);
33+
$ret = $tester->execute(['name' => 'foo']);
34+
35+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
36+
$this->assertStringContainsString('foo:', $tester->getDisplay());
37+
$this->assertStringContainsString(' foo: bar', $tester->getDisplay());
3338
}
3439

35-
public function testDumpBundleName()
40+
/**
41+
* @testWith [true]
42+
* [false]
43+
*/
44+
public function testDumpBundleName(bool $debug)
3645
{
37-
$tester = $this->createCommandTester();
46+
$tester = $this->createCommandTester($debug);
3847
$ret = $tester->execute(['name' => 'TestBundle']);
3948

4049
$this->assertSame(0, $ret, 'Returns 0 in case of success');
4150
$this->assertStringContainsString('custom: foo', $tester->getDisplay());
4251
}
4352

44-
public function testDumpBundleOption()
53+
/**
54+
* @testWith [true]
55+
* [false]
56+
*/
57+
public function testDumpBundleOption(bool $debug)
4558
{
46-
$tester = $this->createCommandTester();
59+
$tester = $this->createCommandTester($debug);
4760
$ret = $tester->execute(['name' => 'TestBundle', 'path' => 'custom']);
4861

4962
$this->assertSame(0, $ret, 'Returns 0 in case of success');
5063
$this->assertStringContainsString('foo', $tester->getDisplay());
5164
}
5265

53-
public function testParametersValuesAreResolved()
66+
/**
67+
* @testWith [true]
68+
* [false]
69+
*/
70+
public function testParametersValuesAreResolved(bool $debug)
5471
{
55-
$tester = $this->createCommandTester();
72+
$tester = $this->createCommandTester($debug);
5673
$ret = $tester->execute(['name' => 'framework']);
5774

5875
$this->assertSame(0, $ret, 'Returns 0 in case of success');
5976
$this->assertStringContainsString("locale: '%env(LOCALE)%'", $tester->getDisplay());
6077
$this->assertStringContainsString('secret: test', $tester->getDisplay());
6178
}
6279

63-
public function testDefaultParameterValueIsResolvedIfConfigIsExisting()
80+
/**
81+
* @testWith [true]
82+
* [false]
83+
*/
84+
public function testDefaultParameterValueIsResolvedIfConfigIsExisting(bool $debug)
6485
{
65-
$tester = $this->createCommandTester();
86+
$tester = $this->createCommandTester($debug);
6687
$ret = $tester->execute(['name' => 'framework']);
6788

6889
$this->assertSame(0, $ret, 'Returns 0 in case of success');
69-
$kernelCacheDir = $this->application->getKernel()->getContainer()->getParameter('kernel.cache_dir');
90+
$kernelCacheDir = self::$kernel->getContainer()->getParameter('kernel.cache_dir');
7091
$this->assertStringContainsString(sprintf("dsn: 'file:%s/profiler'", $kernelCacheDir), $tester->getDisplay());
7192
}
7293

73-
public function testDumpExtensionConfigWithoutBundle()
94+
/**
95+
* @testWith [true]
96+
* [false]
97+
*/
98+
public function testDumpExtensionConfigWithoutBundle(bool $debug)
7499
{
75-
$tester = $this->createCommandTester();
100+
$tester = $this->createCommandTester($debug);
76101
$ret = $tester->execute(['name' => 'test_dump']);
77102

78103
$this->assertSame(0, $ret, 'Returns 0 in case of success');
79104
$this->assertStringContainsString('enabled: true', $tester->getDisplay());
80105
}
81106

82-
public function testDumpUndefinedBundleOption()
107+
/**
108+
* @testWith [true]
109+
* [false]
110+
*/
111+
public function testDumpUndefinedBundleOption(bool $debug)
83112
{
84-
$tester = $this->createCommandTester();
113+
$tester = $this->createCommandTester($debug);
85114
$tester->execute(['name' => 'TestBundle', 'path' => 'foo']);
86115

87116
$this->assertStringContainsString('Unable to find configuration for "test.foo"', $tester->getDisplay());
88117
}
89118

90-
public function testDumpWithPrefixedEnv()
119+
/**
120+
* @testWith [true]
121+
* [false]
122+
*/
123+
public function testDumpWithPrefixedEnv(bool $debug)
91124
{
92-
$tester = $this->createCommandTester();
125+
$tester = $this->createCommandTester($debug);
93126
$tester->execute(['name' => 'FrameworkBundle']);
94127

95128
$this->assertStringContainsString("cookie_httponly: '%env(bool:COOKIE_HTTPONLY)%'", $tester->getDisplay());
96129
}
97130

98-
public function testDumpFallsBackToDefaultConfigAndResolvesParameterValue()
131+
/**
132+
* @testWith [true]
133+
* [false]
134+
*/
135+
public function testDumpFallsBackToDefaultConfigAndResolvesParameterValue(bool $debug)
99136
{
100-
$tester = $this->createCommandTester();
137+
$tester = $this->createCommandTester($debug);
101138
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);
102139

103140
$this->assertSame(0, $ret, 'Returns 0 in case of success');
104141
$this->assertStringContainsString('foo: bar', $tester->getDisplay());
105142
}
106143

107-
public function testDumpFallsBackToDefaultConfigAndResolvesEnvPlaceholder()
144+
/**
145+
* @testWith [true]
146+
* [false]
147+
*/
148+
public function testDumpFallsBackToDefaultConfigAndResolvesEnvPlaceholder(bool $debug)
108149
{
109-
$tester = $this->createCommandTester();
150+
$tester = $this->createCommandTester($debug);
110151
$ret = $tester->execute(['name' => 'DefaultConfigTestBundle']);
111152

112153
$this->assertSame(0, $ret, 'Returns 0 in case of success');
113154
$this->assertStringContainsString("baz: '%env(BAZ)%'", $tester->getDisplay());
114155
}
115156

116-
public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible()
157+
/**
158+
* @testWith [true]
159+
* [false]
160+
*/
161+
public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible(bool $debug)
117162
{
118163
$this->expectException(\LogicException::class);
119164
$this->expectExceptionMessage('The extension with alias "extension_without_config_test" does not have configuration.');
120165

121-
$tester = $this->createCommandTester();
166+
$tester = $this->createCommandTester($debug);
122167
$tester->execute(['name' => 'ExtensionWithoutConfigTestBundle']);
123168
}
124169

125170
/**
126171
* @dataProvider provideCompletionSuggestions
127172
*/
128-
public function testComplete(array $input, array $expectedSuggestions)
173+
public function testComplete(bool $debug, array $input, array $expectedSuggestions)
129174
{
130-
$this->application->add(new ConfigDebugCommand());
131-
132-
$tester = new CommandCompletionTester($this->application->get('debug:config'));
175+
$application = $this->createApplication($debug);
133176

177+
$application->add(new ConfigDebugCommand());
178+
$tester = new CommandCompletionTester($application->get('debug:config'));
134179
$suggestions = $tester->complete($input);
135180

136181
foreach ($expectedSuggestions as $expectedSuggestion) {
@@ -140,17 +185,32 @@ public function testComplete(array $input, array $expectedSuggestions)
140185

141186
public static function provideCompletionSuggestions(): \Generator
142187
{
143-
yield 'name' => [[''], ['default_config_test', 'extension_without_config_test', 'framework', 'test']];
188+
$name = ['default_config_test', 'extension_without_config_test', 'framework', 'test'];
189+
yield 'name, no debug' => [false, [''], $name];
190+
yield 'name, debug' => [true, [''], $name];
144191

145-
yield 'name (started CamelCase)' => [['Fra'], ['DefaultConfigTestBundle', 'ExtensionWithoutConfigTestBundle', 'FrameworkBundle', 'TestBundle']];
192+
$nameCamelCased = ['DefaultConfigTestBundle', 'ExtensionWithoutConfigTestBundle', 'FrameworkBundle', 'TestBundle'];
193+
yield 'name (started CamelCase), no debug' => [false, ['Fra'], $nameCamelCased];
194+
yield 'name (started CamelCase), debug' => [true, ['Fra'], $nameCamelCased];
146195

147-
yield 'name with existing path' => [['framework', ''], ['secret', 'router.resource', 'router.utf8', 'router.enabled', 'validation.enabled', 'default_locale']];
196+
$nameWithPath = ['secret', 'router.resource', 'router.utf8', 'router.enabled', 'validation.enabled', 'default_locale'];
197+
yield 'name with existing path, no debug' => [false, ['framework', ''], $nameWithPath];
198+
yield 'name with existing path, debug' => [true, ['framework', ''], $nameWithPath];
148199
}
149200

150-
private function createCommandTester(): CommandTester
201+
private function createCommandTester(bool $debug): CommandTester
151202
{
152-
$command = $this->application->find('debug:config');
203+
$command = $this->createApplication($debug)->find('debug:config');
153204

154205
return new CommandTester($command);
155206
}
207+
208+
private function createApplication(bool $debug): Application
209+
{
210+
$kernel = static::bootKernel(['debug' => $debug, 'test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
211+
$application = new Application($kernel);
212+
$application->doRun(new ArrayInput([]), new NullOutput());
213+
214+
return $application;
215+
}
156216
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,54 @@
2323
*/
2424
class ConfigDumpReferenceCommandTest extends AbstractWebTestCase
2525
{
26-
private $application;
27-
28-
protected function setUp(): void
29-
{
30-
$kernel = static::createKernel(['test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
31-
$this->application = new Application($kernel);
32-
$this->application->doRun(new ArrayInput([]), new NullOutput());
33-
}
34-
35-
public function testDumpKernelExtension()
26+
/**
27+
* @testWith [true]
28+
* [false]
29+
*/
30+
public function testDumpKernelExtension(bool $debug)
3631
{
37-
$tester = $this->createCommandTester();
32+
$tester = $this->createCommandTester($debug);
3833
$ret = $tester->execute(['name' => 'foo']);
34+
35+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
3936
$this->assertStringContainsString('foo:', $tester->getDisplay());
4037
$this->assertStringContainsString(' bar', $tester->getDisplay());
4138
}
4239

43-
public function testDumpBundleName()
40+
/**
41+
* @testWith [true]
42+
* [false]
43+
*/
44+
public function testDumpBundleName(bool $debug)
4445
{
45-
$tester = $this->createCommandTester();
46+
$tester = $this->createCommandTester($debug);
4647
$ret = $tester->execute(['name' => 'TestBundle']);
4748

4849
$this->assertSame(0, $ret, 'Returns 0 in case of success');
4950
$this->assertStringContainsString('test:', $tester->getDisplay());
5051
$this->assertStringContainsString(' custom:', $tester->getDisplay());
5152
}
5253

53-
public function testDumpExtensionConfigWithoutBundle()
54+
/**
55+
* @testWith [true]
56+
* [false]
57+
*/
58+
public function testDumpExtensionConfigWithoutBundle(bool $debug)
5459
{
55-
$tester = $this->createCommandTester();
60+
$tester = $this->createCommandTester($debug);
5661
$ret = $tester->execute(['name' => 'test_dump']);
5762

5863
$this->assertSame(0, $ret, 'Returns 0 in case of success');
5964
$this->assertStringContainsString('enabled: true', $tester->getDisplay());
6065
}
6166

62-
public function testDumpAtPath()
67+
/**
68+
* @testWith [true]
69+
* [false]
70+
*/
71+
public function testDumpAtPath(bool $debug)
6372
{
64-
$tester = $this->createCommandTester();
73+
$tester = $this->createCommandTester($debug);
6574
$ret = $tester->execute([
6675
'name' => 'test',
6776
'path' => 'array',
@@ -79,9 +88,13 @@ public function testDumpAtPath()
7988
, $tester->getDisplay(true));
8089
}
8190

82-
public function testDumpAtPathXml()
91+
/**
92+
* @testWith [true]
93+
* [false]
94+
*/
95+
public function testDumpAtPathXml(bool $debug)
8396
{
84-
$tester = $this->createCommandTester();
97+
$tester = $this->createCommandTester($debug);
8598
$ret = $tester->execute([
8699
'name' => 'test',
87100
'path' => 'array',
@@ -95,24 +108,40 @@ public function testDumpAtPathXml()
95108
/**
96109
* @dataProvider provideCompletionSuggestions
97110
*/
98-
public function testComplete(array $input, array $expectedSuggestions)
111+
public function testComplete(bool $debug, array $input, array $expectedSuggestions)
99112
{
100-
$this->application->add(new ConfigDumpReferenceCommand());
101-
$tester = new CommandCompletionTester($this->application->get('config:dump-reference'));
102-
$suggestions = $tester->complete($input, 2);
113+
$application = $this->createApplication($debug);
114+
115+
$application->add(new ConfigDumpReferenceCommand());
116+
$tester = new CommandCompletionTester($application->get('config:dump-reference'));
117+
$suggestions = $tester->complete($input);
103118
$this->assertSame($expectedSuggestions, $suggestions);
104119
}
105120

106121
public static function provideCompletionSuggestions(): iterable
107122
{
108-
yield 'name' => [[''], ['DefaultConfigTestBundle', 'default_config_test', 'ExtensionWithoutConfigTestBundle', 'extension_without_config_test', 'FrameworkBundle', 'framework', 'TestBundle', 'test']];
109-
yield 'option --format' => [['--format', ''], ['yaml', 'xml']];
123+
$name = ['DefaultConfigTestBundle', 'default_config_test', 'ExtensionWithoutConfigTestBundle', 'extension_without_config_test', 'FrameworkBundle', 'framework', 'TestBundle', 'test'];
124+
yield 'name, no debug' => [false, [''], $name];
125+
yield 'name, debug' => [true, [''], $name];
126+
127+
$optionFormat = ['yaml', 'xml'];
128+
yield 'option --format, no debug' => [false, ['--format', ''], $optionFormat];
129+
yield 'option --format, debug' => [true, ['--format', ''], $optionFormat];
110130
}
111131

112-
private function createCommandTester(): CommandTester
132+
private function createCommandTester(bool $debug): CommandTester
113133
{
114-
$command = $this->application->find('config:dump-reference');
134+
$command = $this->createApplication($debug)->find('config:dump-reference');
115135

116136
return new CommandTester($command);
117137
}
138+
139+
private function createApplication(bool $debug): Application
140+
{
141+
$kernel = static::createKernel(['debug' => $debug, 'test_case' => 'ConfigDump', 'root_config' => 'config.yml']);
142+
$application = new Application($kernel);
143+
$application->doRun(new ArrayInput([]), new NullOutput());
144+
145+
return $application;
146+
}
118147
}

0 commit comments

Comments
 (0)