|
| 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