1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\Tests\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\ResourceBundle\Form\DataTransformer\BooleanTransformer; |
15
|
|
|
use Lug\Bundle\ResourceBundle\Form\Extension\BooleanExtension; |
16
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
19
|
|
|
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
21
|
|
|
use Symfony\Component\Form\Forms; |
22
|
|
|
use Symfony\Component\Validator\Validation; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class BooleanExtensionTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FormFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $formFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
36
|
|
|
*/ |
37
|
|
|
private $parameterResolver; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var BooleanTransformer |
41
|
|
|
*/ |
42
|
|
|
private $booleanTransformer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function setUp() |
48
|
|
|
{ |
49
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
|
|
|
|
50
|
|
|
$this->booleanTransformer = new BooleanTransformer(); |
51
|
|
|
|
52
|
|
|
$this->formFactory = Forms::createFormFactoryBuilder() |
53
|
|
|
->addExtension(new ValidatorExtension(Validation::createValidator())) |
54
|
|
|
->addTypeExtension(new BooleanExtension($this->parameterResolver, $this->booleanTransformer)) |
55
|
|
|
->getFormFactory(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testCheckboxType() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$viewTransformers = $this->formFactory |
61
|
|
|
->create(CheckboxType::class, null, ['api' => false]) |
62
|
|
|
->getConfig() |
63
|
|
|
->getViewTransformers(); |
64
|
|
|
|
65
|
|
|
$this->assertCount(1, $viewTransformers); |
66
|
|
|
$this->assertInstanceOf(BooleanToStringTransformer::class, $viewTransformers[0]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testApiType() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$viewTransformers = $this->formFactory |
72
|
|
|
->create(CheckboxType::class, null, ['api' => true]) |
73
|
|
|
->getConfig() |
74
|
|
|
->getViewTransformers(); |
75
|
|
|
|
76
|
|
|
$this->assertCount(1, $viewTransformers); |
77
|
|
|
$this->assertInstanceOf(BooleanTransformer::class, $viewTransformers[0]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @dataProvider validInitialProvider |
82
|
|
|
*/ |
83
|
|
|
public function testValidInitialData($expected, $data) |
84
|
|
|
{ |
85
|
|
|
$form = $this->formFactory->create(CheckboxType::class, $data, ['api' => true]); |
86
|
|
|
|
87
|
|
|
$this->assertSame($expected, $form->getData()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @dataProvider invalidProvider |
92
|
|
|
* |
93
|
|
|
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
94
|
|
|
* @expectedExceptionMessage The boolean type expects a boolean or null value |
95
|
|
|
*/ |
96
|
|
|
public function testInvalidInitialData($data) |
97
|
|
|
{ |
98
|
|
|
$this->formFactory->create(CheckboxType::class, $data, ['api' => true]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @dataProvider validSubmitProvider |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function testValidSubmittedData($expected, $data) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$form = $this->formFactory |
107
|
|
|
->create(CheckboxType::class, null, ['api' => true]) |
108
|
|
|
->submit($data); |
109
|
|
|
|
110
|
|
|
$this->assertTrue($form->isValid()); |
111
|
|
|
$this->assertSame($expected, $form->getData()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @dataProvider invalidProvider |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function testInvalidSubmittedData($data) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$form = $this->formFactory |
120
|
|
|
->create(CheckboxType::class, null, ['api' => true]) |
121
|
|
|
->submit($data); |
122
|
|
|
|
123
|
|
|
$this->assertFalse($form->isValid()); |
124
|
|
|
$this->assertNull($form->getData()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
129
|
|
|
* @expectedExceptionMessage The option "api" with value "foo" is expected to be of type "bool", but is of type "string". |
130
|
|
|
*/ |
131
|
|
|
public function testInvalidApiOption() |
132
|
|
|
{ |
133
|
|
|
$this->formFactory->create(CheckboxType::class, null, ['api' => 'foo']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return mixed[] |
138
|
|
|
*/ |
139
|
|
|
public function validInitialProvider() |
140
|
|
|
{ |
141
|
|
|
return [ |
142
|
|
|
[true, true], |
143
|
|
|
[false, false], |
144
|
|
|
[false, null], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return mixed[] |
150
|
|
|
*/ |
151
|
|
|
public function validSubmitProvider() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
[true, true], |
155
|
|
|
[true, 1], |
156
|
|
|
[true, '1'], |
157
|
|
|
[true, 'true'], |
158
|
|
|
[true, 'yes'], |
159
|
|
|
[true, 'on'], |
160
|
|
|
[false, false], |
161
|
|
|
[false, 0], |
162
|
|
|
[false, '0'], |
163
|
|
|
[false, 'false'], |
164
|
|
|
[false, 'no'], |
165
|
|
|
[false, 'off'], |
166
|
|
|
[false, ''], |
167
|
|
|
[false, null], |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return mixed[] |
173
|
|
|
*/ |
174
|
|
|
public function invalidProvider() |
175
|
|
|
{ |
176
|
|
|
return [ |
177
|
|
|
['foo'], |
178
|
|
|
[1.2], |
179
|
|
|
[new \stdClass()], |
180
|
|
|
[['foo' => 'bar']], |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
186
|
|
|
*/ |
187
|
|
|
private function createParameterResolverMock() |
188
|
|
|
{ |
189
|
|
|
return $this->getMock(ParameterResolverInterface::class); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.