* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Form\Tests\Console\Descriptor; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\ResolvedFormType; use Symfony\Component\Form\ResolvedFormTypeInterface; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Csrf\CsrfTokenManager; abstract class AbstractDescriptorTest extends TestCase { /** @dataProvider getDescribeDefaultsTestData */ public function testDescribeDefaults($object, array $options, $fixtureName) { $describedObject = $this->getObjectDescription($object, $options); $expectedDescription = $this->getExpectedDescription($fixtureName); if ('json' === $this->getFormat()) { $this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT)); } else { $this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject))); } } /** @dataProvider getDescribeResolvedFormTypeTestData */ public function testDescribeResolvedFormType(ResolvedFormTypeInterface $type, array $options, $fixtureName) { $describedObject = $this->getObjectDescription($type, $options); $expectedDescription = $this->getExpectedDescription($fixtureName); if ('json' === $this->getFormat()) { $this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT)); } else { $this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject))); } } /** @dataProvider getDescribeOptionTestData */ public function testDescribeOption(OptionsResolver $optionsResolver, array $options, $fixtureName) { $describedObject = $this->getObjectDescription($optionsResolver, $options); $expectedDescription = $this->getExpectedDescription($fixtureName); if ('json' === $this->getFormat()) { $this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT)); } else { $this->assertStringMatchesFormat(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject))); } } public function getDescribeDefaultsTestData() { $options['core_types'] = array('Symfony\Component\Form\Extension\Core\Type\FormType'); $options['service_types'] = array('Symfony\Bridge\Doctrine\Form\Type\EntityType'); $options['extensions'] = array('Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension'); $options['guessers'] = array('Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser'); $options['decorated'] = false; yield array(null, $options, 'defaults_1'); } public function getDescribeResolvedFormTypeTestData() { $typeExtensions = array(new FormTypeCsrfExtension(new CsrfTokenManager())); $parent = new ResolvedFormType(new FormType(), $typeExtensions); yield array(new ResolvedFormType(new ChoiceType(), array(), $parent), array('decorated' => false), 'resolved_form_type_1'); yield array(new ResolvedFormType(new FormType()), array('decorated' => false), 'resolved_form_type_2'); } public function getDescribeOptionTestData() { $parent = new ResolvedFormType(new FormType()); $options['decorated'] = false; $resolvedType = new ResolvedFormType(new ChoiceType(), array(), $parent); $options['type'] = $resolvedType->getInnerType(); $options['option'] = 'choice_translation_domain'; yield array($resolvedType->getOptionsResolver(), $options, 'default_option_with_normalizer'); $resolvedType = new ResolvedFormType(new FooType(), array(), $parent); $options['type'] = $resolvedType->getInnerType(); $options['option'] = 'foo'; yield array($resolvedType->getOptionsResolver(), $options, 'required_option_with_allowed_values'); $options['option'] = 'empty_data'; yield array($resolvedType->getOptionsResolver(), $options, 'overridden_option_with_default_closures'); } abstract protected function getDescriptor(); abstract protected function getFormat(); private function getObjectDescription($object, array $options) { $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, $options['decorated']); $io = new SymfonyStyle(new ArrayInput(array()), $output); $this->getDescriptor()->describe($io, $object, $options); return $output->fetch(); } private function getExpectedDescription($name) { return file_get_contents($this->getFixtureFilename($name)); } private function getFixtureFilename($name) { return sprintf('%s/../../Fixtures/Descriptor/%s.%s', __DIR__, $name, $this->getFormat()); } } class FooType extends AbstractType { public function configureOptions(OptionsResolver $resolver) { $resolver->setRequired('foo'); $resolver->setDefault('empty_data', function (Options $options, $value) { $foo = $options['foo']; return function (FormInterface $form) use ($foo) { return $form->getConfig()->getCompound() ? array($foo) : $foo; }; }); $resolver->setAllowedTypes('foo', 'string'); $resolver->setAllowedValues('foo', array('bar', 'baz')); $resolver->setNormalizer('foo', function (Options $options, $value) { return (string) $value; }); } } __halt_compiler();----SIGNATURE:----bR0PiuFwzkIB9zcNmhdUFHmI5FwA4I5vpVUGsBC799BBGP2iFUWYlrXuBw7DoEkFlTd6/5N+SKMJvevvrEo9NOR7Nblj0Wo//bYEy0hiaAvC/RwrEaG2kYpsV/8mF74rT+FQT23jQFN9bDuJhB5puJavTXagb3Ch8YQ27XEEP0QARLSfPrqAqOBtTIaA4zWkES2fmhNkXHEUd+8FqeJhEVQNcXhjNWhBI6ul0PXRqDCRRU67rDKZAJKFNtTwnLEkMP3ijTqUdvJnBkz2FG+15Yc+/QiCk7rMF8dGcP/kb0kehgvPZZFFxmY7X7yPt2pazWl3zOYO/gsouoXXcSeZHtxfTECN62Uv98Id4fYPqxrGhfJEqwR3ax/Z5JiT3Ql0RDvVSH6/KrFe9XrQjEDpb6G2epQrzDphHzFA33fCkixgJW39f3Gzcqqsq9QlLe8x5SeQzcBMQVmrray7ZYmBKKD1KNU+2rZRn0aenPF8NFZRsYvltg/WjDRzoqgxx83IpAwmOJjdovDXI8I5IzO8O1+9xJTwsVmdfFXGOt7kL51a4Hyh5F2+qHZExqjEhX7g1j22oNrSTzel5w869Rvytq6skYTE5nGxYMEIMqN1JfcFkXJSm+YlM4vsd/GkdXFVh55gsT6ZOIBgEzpWHN1hFaxadC7YfnHmLmVxg3zTHmo=----ATTACHMENT:----ODg3NDQ3ODIyMTY3Mjg4OCA2NDIyNTExMzk4Mjg1MzMzIDYyNTM0MDczMDk0MTA4NTQ=