|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Tests\Form\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
6
|
|
|
use Symfony\Component\Form\FormInterface; |
|
7
|
|
|
use Symfony\Component\Form\Test\TypeTestCase; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; |
|
10
|
|
|
use Yokai\EnumBundle\EnumRegistry; |
|
11
|
|
|
use Yokai\EnumBundle\Form\Type\EnumType; |
|
12
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; |
|
13
|
|
|
use Yokai\EnumBundle\Tests\Form\TestExtension; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Yann Eugoné <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class EnumTypeTest extends TypeTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
use ProphecyTrait; |
|
21
|
|
|
|
|
22
|
|
|
private $enumRegistry; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->enumRegistry = $this->prophesize(EnumRegistry::class); |
|
27
|
|
|
$this->enumRegistry->has('state')->willReturn(false); |
|
28
|
|
|
$this->enumRegistry->has(GenderEnum::class)->willReturn(true); |
|
29
|
|
|
$this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum); |
|
30
|
|
|
|
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testEnumOptionIsRequired(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectException(MissingOptionsException::class); |
|
37
|
|
|
$this->createForm(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testEnumOptionIsInvalid(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->expectException(InvalidOptionsException::class); |
|
43
|
|
|
$this->createForm('state'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testEnumOptionValid(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$form = $this->createForm(GenderEnum::class); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->getConfig()->getOption('choices')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getExtensions(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
new TestExtension($this->enumRegistry->reveal()) |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function createForm($enum = null): FormInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$options = []; |
|
63
|
|
|
if ($enum) { |
|
64
|
|
|
$options['enum'] = $enum; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->factory->create(EnumType::class, null, $options); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|