|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony 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\FrameworkBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | +use Symfony\Component\Form\AbstractType; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 22 | + */ |
| 23 | +class FormPassTest extends \PHPUnit_Framework_TestCase |
| 24 | +{ |
| 25 | + public function testDoNothingIfFormExtensionNotLoaded() |
| 26 | + { |
| 27 | + $container = new ContainerBuilder(); |
| 28 | + $container->addCompilerPass(new FormPass()); |
| 29 | + |
| 30 | + $container->compile(); |
| 31 | + |
| 32 | + $this->assertFalse($container->hasDefinition('form.extension')); |
| 33 | + } |
| 34 | + |
| 35 | + public function testAddTaggedTypes() |
| 36 | + { |
| 37 | + $container = new ContainerBuilder(); |
| 38 | + $container->addCompilerPass(new FormPass()); |
| 39 | + |
| 40 | + $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension'); |
| 41 | + $extDefinition->setArguments(array( |
| 42 | + new Reference('service_container'), |
| 43 | + array(), |
| 44 | + array(), |
| 45 | + array(), |
| 46 | + )); |
| 47 | + |
| 48 | + $definition1 = new Definition(__CLASS__.'_Type1'); |
| 49 | + $definition1->addTag('form.type'); |
| 50 | + $definition2 = new Definition(__CLASS__.'_Type2'); |
| 51 | + $definition2->addTag('form.type', array('alias' => 'mytype2')); |
| 52 | + |
| 53 | + $container->setDefinition('form.extension', $extDefinition); |
| 54 | + $container->setDefinition('my.type1', $definition1); |
| 55 | + $container->setDefinition('my.type2', $definition2); |
| 56 | + |
| 57 | + $container->compile(); |
| 58 | + |
| 59 | + $extDefinition = $container->getDefinition('form.extension'); |
| 60 | + |
| 61 | + $this->assertSame(array( |
| 62 | + __CLASS__.'_Type1' => 'my.type1', |
| 63 | + 'mytype2' => 'my.type2', |
| 64 | + ), $extDefinition->getArgument(1)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testAddTaggedTypeExtensions() |
| 68 | + { |
| 69 | + $container = new ContainerBuilder(); |
| 70 | + $container->addCompilerPass(new FormPass()); |
| 71 | + |
| 72 | + $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension'); |
| 73 | + $extDefinition->setArguments(array( |
| 74 | + new Reference('service_container'), |
| 75 | + array(), |
| 76 | + array(), |
| 77 | + array(), |
| 78 | + )); |
| 79 | + |
| 80 | + $definition1 = new Definition('stdClass'); |
| 81 | + $definition1->addTag('form.type_extension', array('alias' => 'type1')); |
| 82 | + $definition2 = new Definition('stdClass'); |
| 83 | + $definition2->addTag('form.type_extension', array('alias' => 'type1')); |
| 84 | + $definition3 = new Definition('stdClass'); |
| 85 | + $definition3->addTag('form.type_extension', array('alias' => 'type2')); |
| 86 | + |
| 87 | + $container->setDefinition('form.extension', $extDefinition); |
| 88 | + $container->setDefinition('my.type_extension1', $definition1); |
| 89 | + $container->setDefinition('my.type_extension2', $definition2); |
| 90 | + $container->setDefinition('my.type_extension3', $definition3); |
| 91 | + |
| 92 | + $container->compile(); |
| 93 | + |
| 94 | + $extDefinition = $container->getDefinition('form.extension'); |
| 95 | + |
| 96 | + $this->assertSame(array( |
| 97 | + 'type1' => array( |
| 98 | + 'my.type_extension1', |
| 99 | + 'my.type_extension2', |
| 100 | + ), |
| 101 | + 'type2' => array( |
| 102 | + 'my.type_extension3', |
| 103 | + ), |
| 104 | + ), $extDefinition->getArgument(2)); |
| 105 | + } |
| 106 | + |
| 107 | + public function testAddTaggedGuessers() |
| 108 | + { |
| 109 | + $container = new ContainerBuilder(); |
| 110 | + $container->addCompilerPass(new FormPass()); |
| 111 | + |
| 112 | + $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension'); |
| 113 | + $extDefinition->setArguments(array( |
| 114 | + new Reference('service_container'), |
| 115 | + array(), |
| 116 | + array(), |
| 117 | + array(), |
| 118 | + )); |
| 119 | + |
| 120 | + $definition1 = new Definition('stdClass'); |
| 121 | + $definition1->addTag('form.type_guesser'); |
| 122 | + $definition2 = new Definition('stdClass'); |
| 123 | + $definition2->addTag('form.type_guesser'); |
| 124 | + |
| 125 | + $container->setDefinition('form.extension', $extDefinition); |
| 126 | + $container->setDefinition('my.guesser1', $definition1); |
| 127 | + $container->setDefinition('my.guesser2', $definition2); |
| 128 | + |
| 129 | + $container->compile(); |
| 130 | + |
| 131 | + $extDefinition = $container->getDefinition('form.extension'); |
| 132 | + |
| 133 | + $this->assertSame(array( |
| 134 | + 'my.guesser1', |
| 135 | + 'my.guesser2', |
| 136 | + ), $extDefinition->getArgument(3)); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class FormPassTest_Type1 extends AbstractType |
| 141 | +{ |
| 142 | +} |
| 143 | + |
| 144 | +class FormPassTest_Type2 extends AbstractType |
| 145 | +{ |
| 146 | +} |
0 commit comments