Skip to content

Commit b5b4afa

Browse files
committed
feature #1782 Validate root_namespace configuration (GromNaN)
This PR was merged into the 1.x branch. Discussion ---------- Validate `root_namespace` configuration Fix #1781 Commits ------- cfe91c4 Validate root_namespace configuration
2 parents 155680f + cfe91c4 commit b5b4afa

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

src/MakerBundle.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public function configure(DefinitionConfigurator $definition): void
3232
{
3333
$definition->rootNode()
3434
->children()
35-
->scalarNode('root_namespace')->defaultValue('App')->end()
35+
->scalarNode('root_namespace')
36+
->defaultValue('App')
37+
->validate()
38+
->ifString()
39+
->then(Validator::validateClassName(...))
40+
->end()
41+
->end()
3642
->booleanNode('generate_final_classes')->defaultTrue()->end()
3743
->booleanNode('generate_final_entities')->defaultFalse()->end()
3844
->end()

tests/BundleConfigurationTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Bundle\MakerBundle\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\MakerBundle\MakerBundle;
16+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
18+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
19+
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
20+
use Symfony\Component\Config\Definition\Processor;
21+
22+
class BundleConfigurationTest extends TestCase
23+
{
24+
public function testDefaultConfiguration()
25+
{
26+
$config = $this->processConfiguration([]);
27+
28+
$this->assertSame('App', $config['root_namespace']);
29+
$this->assertTrue($config['generate_final_classes']);
30+
$this->assertFalse($config['generate_final_entities']);
31+
}
32+
33+
public function testAllOptionsConfigured()
34+
{
35+
$config = $this->processConfiguration([
36+
'maker' => [
37+
'root_namespace' => 'Custom\\Name\\Space',
38+
'generate_final_classes' => false,
39+
'generate_final_entities' => true,
40+
],
41+
]);
42+
43+
$this->assertSame('Custom\\Name\\Space', $config['root_namespace']);
44+
$this->assertFalse($config['generate_final_classes']);
45+
$this->assertTrue($config['generate_final_entities']);
46+
}
47+
48+
public function testInvalidRootNamespaceWithReservedKeyword()
49+
{
50+
$this->expectException(InvalidConfigurationException::class);
51+
$this->expectExceptionMessage('"Class" is a reserved keyword');
52+
53+
$this->processConfiguration([
54+
'maker' => [
55+
'root_namespace' => 'App\\Class',
56+
],
57+
]);
58+
}
59+
60+
/**
61+
* Processes the configuration using the MakerBundle's configure method.
62+
*
63+
* @param array<string, mixed> $configs
64+
*
65+
* @return array<string, mixed>
66+
*/
67+
private function processConfiguration(array $configs): array
68+
{
69+
$bundle = new MakerBundle();
70+
$treeBuilder = new TreeBuilder('maker');
71+
$definitionConfigurator = new DefinitionConfigurator(
72+
$treeBuilder,
73+
$this->createStub(DefinitionFileLoader::class),
74+
__DIR__,
75+
__FILE__
76+
);
77+
78+
$bundle->configure($definitionConfigurator);
79+
80+
$processor = new Processor();
81+
82+
return $processor->process($treeBuilder->buildTree(), $configs);
83+
}
84+
}

0 commit comments

Comments
 (0)