These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * This file is part of the FreshDoctrineEnumBundle |
||
4 | * |
||
5 | * (c) Artem Genvald <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | namespace Fresh\DoctrineEnumBundle\Tests\DForm; |
||
12 | |||
13 | use Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser; |
||
14 | use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType; |
||
15 | use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\NotAChildType; |
||
16 | use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper; |
||
17 | use Symfony\Component\Form\Guess\Guess; |
||
18 | use Symfony\Component\Form\Guess\TypeGuess; |
||
19 | |||
20 | /** |
||
21 | * EnumTypeGuesserTest. |
||
22 | * |
||
23 | * @author Artem Genvald <[email protected]> |
||
24 | */ |
||
25 | class EnumTypeGuesserTest extends \PHPUnit_Framework_TestCase |
||
26 | { |
||
27 | public function testNullResultWhenClassMetadataNotFound() |
||
28 | { |
||
29 | /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */ |
||
30 | $enumTypeGuesser = $this->getMockBuilder('\Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser') |
||
31 | ->disableOriginalConstructor() |
||
32 | ->setMethods(['getMetadata']) |
||
33 | ->getMock(); |
||
34 | |||
35 | $enumTypeGuesser->expects($this->once()) |
||
36 | ->method('getMetadata') |
||
37 | ->willReturn(null); |
||
38 | |||
39 | $this->assertNull($enumTypeGuesser->guessType('\stdClass', 'position')); |
||
40 | } |
||
41 | |||
42 | public function testNullResultWhenEnumTypeNotRegistered() |
||
43 | { |
||
44 | /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */ |
||
45 | $enumTypeGuesser = $this->getMockBuilder('\Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser') |
||
46 | ->disableOriginalConstructor() |
||
47 | ->setMethods(['getMetadata']) |
||
48 | ->getMock(); |
||
49 | |||
50 | $metadata = $this->getMockBuilder('\Doctrine\ORM\Mapping\ClassMetadataInfo') |
||
51 | ->disableOriginalConstructor() |
||
52 | ->setMethods(['getTypeOfField']) |
||
53 | ->getMock(); |
||
54 | |||
55 | $metadata->expects($this->once()) |
||
56 | ->method('getTypeOfField') |
||
57 | ->willReturn('unregistered_enum_type'); |
||
58 | |||
59 | $enumTypeGuesser->expects($this->once()) |
||
60 | ->method('getMetadata') |
||
61 | ->willReturn([$metadata]); |
||
62 | |||
63 | $this->assertNull($enumTypeGuesser->guessType('\stdClass', 'position')); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException |
||
68 | */ |
||
69 | View Code Duplication | public function testExceptionWhenClassDoesNotExist() |
|
0 ignored issues
–
show
|
|||
70 | { |
||
71 | $managerRegistry = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') |
||
72 | ->disableOriginalConstructor() |
||
73 | ->getMock(); |
||
74 | $registeredTypes = [ |
||
75 | 'stub' => [ |
||
76 | 'class' => '\Acme\Foo\Bar\Baz', |
||
77 | ] |
||
78 | ]; |
||
79 | |||
80 | /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */ |
||
81 | $enumTypeGuesser = $this->getMockBuilder('\Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser') |
||
82 | ->setConstructorArgs([$managerRegistry, $registeredTypes]) |
||
83 | ->setMethods(['getMetadata']) |
||
84 | ->getMock(); |
||
85 | |||
86 | $metadata = $this->getMockBuilder('\Doctrine\ORM\Mapping\ClassMetadataInfo') |
||
87 | ->disableOriginalConstructor() |
||
88 | ->setMethods(['getTypeOfField']) |
||
89 | ->getMock(); |
||
90 | |||
91 | $metadata->expects($this->once()) |
||
92 | ->method('getTypeOfField') |
||
93 | ->willReturn('stub'); |
||
94 | |||
95 | $enumTypeGuesser->expects($this->once()) |
||
96 | ->method('getMetadata') |
||
97 | ->willReturn([$metadata]); |
||
98 | |||
99 | $this->assertNull($enumTypeGuesser->guessType('\stdClass', 'position')); |
||
100 | } |
||
101 | |||
102 | View Code Duplication | public function testNullResultWhenIsNotChildOfAbstractEnumType() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
103 | { |
||
104 | $managerRegistry = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') |
||
105 | ->disableOriginalConstructor() |
||
106 | ->getMock(); |
||
107 | $registeredTypes = [ |
||
108 | 'NotAChildType' => [ |
||
109 | 'class' => NotAChildType::class, |
||
110 | ] |
||
111 | ]; |
||
112 | |||
113 | /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */ |
||
114 | $enumTypeGuesser = $this->getMockBuilder('\Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser') |
||
115 | ->setConstructorArgs([$managerRegistry, $registeredTypes]) |
||
116 | ->setMethods(['getMetadata']) |
||
117 | ->getMock(); |
||
118 | |||
119 | $metadata = $this->getMockBuilder('\Doctrine\ORM\Mapping\ClassMetadataInfo') |
||
120 | ->disableOriginalConstructor() |
||
121 | ->setMethods(['getTypeOfField']) |
||
122 | ->getMock(); |
||
123 | |||
124 | $metadata->expects($this->once()) |
||
125 | ->method('getTypeOfField') |
||
126 | ->willReturn('NotAChildType'); |
||
127 | |||
128 | $enumTypeGuesser->expects($this->once()) |
||
129 | ->method('getMetadata') |
||
130 | ->willReturn([$metadata]); |
||
131 | |||
132 | $this->assertNull($enumTypeGuesser->guessType('\stdClass', 'position')); |
||
133 | } |
||
134 | |||
135 | public function testSuccessfulTypeGuessing() |
||
136 | { |
||
137 | $managerRegistry = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') |
||
138 | ->disableOriginalConstructor() |
||
139 | ->getMock(); |
||
140 | $registeredTypes = [ |
||
141 | 'BasketballPositionType' => [ |
||
142 | 'class' => BasketballPositionType::class, |
||
143 | ] |
||
144 | ]; |
||
145 | |||
146 | /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */ |
||
147 | $enumTypeGuesser = $this->getMockBuilder('\Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser') |
||
148 | ->setConstructorArgs([$managerRegistry, $registeredTypes]) |
||
149 | ->setMethods(['getMetadata']) |
||
150 | ->getMock(); |
||
151 | |||
152 | $metadata = $this->getMockBuilder('\Doctrine\ORM\Mapping\ClassMetadataInfo') |
||
153 | ->disableOriginalConstructor() |
||
154 | ->setMethods(['getTypeOfField', 'isNullable']) |
||
155 | ->getMock(); |
||
156 | |||
157 | $metadata->expects($this->once()) |
||
158 | ->method('getTypeOfField') |
||
159 | ->willReturn('BasketballPositionType'); |
||
160 | |||
161 | $metadata->expects($this->once()) |
||
162 | ->method('isNullable') |
||
163 | ->willReturn(true); |
||
164 | |||
165 | $enumTypeGuesser->expects($this->once()) |
||
166 | ->method('getMetadata') |
||
167 | ->willReturn([$metadata]); |
||
168 | |||
169 | $typeGuess = new TypeGuess( |
||
170 | LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\ChoiceType'), |
||
171 | [ |
||
172 | 'choices' => BasketballPositionType::getChoices(), |
||
173 | 'required' => false, |
||
174 | ], |
||
175 | Guess::VERY_HIGH_CONFIDENCE |
||
176 | ); |
||
177 | |||
178 | $this->assertEquals($typeGuess, $enumTypeGuesser->guessType('\stdClass', 'position')); |
||
179 | } |
||
180 | } |
||
181 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.