|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yokai\EnumBundle\Tests\Form\Extension; |
| 4 | + |
| 5 | +use Prophecy\Prophecy\ObjectProphecy; |
| 6 | +use Symfony\Component\Form\AbstractType; |
| 7 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 8 | +use Symfony\Component\Form\Guess\Guess; |
| 9 | +use Symfony\Component\Form\Guess\TypeGuess; |
| 10 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 11 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 12 | +use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
| 13 | +use Yokai\EnumBundle\Form\Extension\EnumTypeGuesser; |
| 14 | +use Yokai\EnumBundle\Form\Type\EnumType; |
| 15 | +use Yokai\EnumBundle\Registry\EnumRegistryInterface; |
| 16 | +use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; |
| 17 | +use Yokai\EnumBundle\Tests\Form\TestExtension; |
| 18 | +use Yokai\EnumBundle\Validator\Constraints\Enum; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Yann Eugoné <eugone.yann@gmail.com> |
| 22 | + */ |
| 23 | +class EnumTypeGuesserTest extends TypeTestCase |
| 24 | +{ |
| 25 | + const TEST_CLASS = 'Yokai\EnumBundle\Tests\Form\Extension\EnumTypeGuesserTest_TestClass'; |
| 26 | + |
| 27 | + const TEST_PROPERTY = 'property'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var EnumTypeGuesser |
| 31 | + */ |
| 32 | + private $guesser; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ObjectProphecy|EnumRegistryInterface |
| 36 | + */ |
| 37 | + private $enumRegistry; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ClassMetadata |
| 41 | + */ |
| 42 | + private $metadata; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ObjectProphecy|MetadataFactoryInterface |
| 46 | + */ |
| 47 | + private $metadataFactory; |
| 48 | + |
| 49 | + protected function setUp() |
| 50 | + { |
| 51 | + $this->enumRegistry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface'); |
| 52 | + $this->enumRegistry->has('state')->willReturn(false); |
| 53 | + $this->enumRegistry->has('gender')->willReturn(true); |
| 54 | + $this->enumRegistry->get('gender')->willReturn(new GenderEnum); |
| 55 | + |
| 56 | + $this->metadata = new ClassMetadata(self::TEST_CLASS); |
| 57 | + $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => 'gender'])); |
| 58 | + $this->metadataFactory = $this->prophesize('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'); |
| 59 | + $this->metadataFactory->getMetadataFor(self::TEST_CLASS) |
| 60 | + ->willReturn($this->metadata); |
| 61 | + |
| 62 | + $this->guesser = new EnumTypeGuesser($this->metadataFactory->reveal(), $this->enumRegistry->reveal()); |
| 63 | + |
| 64 | + parent::setUp(); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGuessType() |
| 68 | + { |
| 69 | + $guess = new TypeGuess( |
| 70 | + $this->getEnumType(), |
| 71 | + [ |
| 72 | + 'enum' => 'gender', |
| 73 | + 'multiple' => false, |
| 74 | + ], |
| 75 | + Guess::HIGH_CONFIDENCE |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testGuessRequired() |
| 82 | + { |
| 83 | + $this->assertNull($this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testGuessMaxLength() |
| 87 | + { |
| 88 | + $this->assertNull($this->guesser->guessMaxLength(self::TEST_CLASS, self::TEST_PROPERTY)); |
| 89 | + } |
| 90 | + |
| 91 | + public function testGuessPattern() |
| 92 | + { |
| 93 | + $this->assertNull($this->guesser->guessPattern(self::TEST_CLASS, self::TEST_PROPERTY)); |
| 94 | + } |
| 95 | + |
| 96 | + public function testCreateForm() |
| 97 | + { |
| 98 | + $class = self::TEST_CLASS; |
| 99 | + $form = $this->factory->create($this->getFormType(), new $class, ['data_class' => $class]) |
| 100 | + ->add(self::TEST_PROPERTY); |
| 101 | + |
| 102 | + $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices')); |
| 103 | + } |
| 104 | + |
| 105 | + protected function getFormType() |
| 106 | + { |
| 107 | + if (method_exists(AbstractType::class, 'getBlockPrefix')) { |
| 108 | + $name = FormType::class; //Symfony 3.x support |
| 109 | + } else { |
| 110 | + $name = 'form'; //Symfony 2.x support |
| 111 | + } |
| 112 | + |
| 113 | + return $name; |
| 114 | + } |
| 115 | + |
| 116 | + protected function getEnumType() |
| 117 | + { |
| 118 | + if (method_exists(AbstractType::class, 'getBlockPrefix')) { |
| 119 | + $name = EnumType::class; //Symfony 3.x support |
| 120 | + } else { |
| 121 | + $name = 'enum'; //Symfony 2.x support |
| 122 | + } |
| 123 | + |
| 124 | + return $name; |
| 125 | + } |
| 126 | + |
| 127 | + protected function getExtensions() |
| 128 | + { |
| 129 | + return [ |
| 130 | + new TestExtension($this->enumRegistry->reveal(), $this->metadataFactory->reveal()), |
| 131 | + ]; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class EnumTypeGuesserTest_TestClass |
| 136 | +{ |
| 137 | + public $property; |
| 138 | +} |
0 commit comments