6
6
use Burgov \Bundle \KeyValueFormBundle \Form \Type \KeyValueType ;
7
7
use Symfony \Component \Form \AbstractExtension ;
8
8
use Symfony \Component \Form \Extension \Core \ChoiceList \ObjectChoiceList ;
9
+ use Symfony \Component \Form \ChoiceList \ArrayChoiceList ;
9
10
use Symfony \Component \Form \Test \TypeTestCase ;
10
11
11
12
class KeyValueTypeTest extends TypeTestCase
12
13
{
14
+ private static $ isSf28 ;
15
+ private static $ typeNames = array ();
16
+
17
+ public function setUp ()
18
+ {
19
+ parent ::setUp ();
20
+
21
+ if (null === self ::$ isSf28 ) {
22
+ self ::$ isSf28 = method_exists ('Symfony\Component\Form\AbstractType ' , 'getBlockPrefix ' );
23
+ if (self ::$ isSf28 ) {
24
+ self ::$ typeNames = array (
25
+ 'choice ' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType ' ,
26
+ 'text ' => 'Symfony\Component\Form\Extension\Core\Type\TextType ' ,
27
+ 'burgov_key_value ' => 'Burgov\Bundle\KeyValueFormBundle\Form\Type\KeyValueType ' ,
28
+ );
29
+ } else {
30
+ self ::$ typeNames = array (
31
+ 'choice ' => 'choice ' ,
32
+ 'text ' => 'text ' ,
33
+ 'burgov_key_value ' => 'burgov_key_value ' ,
34
+ );
35
+ }
36
+ }
37
+ }
38
+
13
39
public function getExtensions ()
14
40
{
15
41
return array (new ConcreteExtension ());
@@ -43,20 +69,23 @@ public function testSubmitValidData()
43
69
'key3 ' => '1 ' ,
44
70
);
45
71
46
- $ builder = $ this ->factory ->createBuilder ('burgov_key_value ' , $ originalData , array (
47
- 'value_type ' => 'text ' ,
48
- 'key_options ' => array ('label ' => 'label_key ' ),
49
- 'value_options ' => array ('label ' => 'label_value ' )));
50
-
51
- $ form = $ builder ->getForm ();
72
+ $ form = $ this ->factory ->create (
73
+ self ::$ typeNames ['burgov_key_value ' ],
74
+ $ originalData ,
75
+ array (
76
+ 'value_type ' => self ::$ typeNames ['text ' ],
77
+ 'key_options ' => array ('label ' => 'label_key ' ),
78
+ 'value_options ' => array ('label ' => 'label_value ' )
79
+ )
80
+ );
52
81
53
- $ this ->assertFormTypes (array ('text ' , 'text ' ), array ('text ' , 'text ' ), $ form );
82
+ $ this ->assertFormTypes (array (self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ] ), array (self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ] ), $ form );
54
83
$ this ->assertFormOptions (array (array ('label ' => 'label_key ' ), array ('label ' => 'label_value ' )), $ form );
55
84
56
85
$ form ->submit ($ submitData );
57
- $ this ->assertTrue ($ form ->isValid (), $ form ->getErrorsAsString ());
86
+ $ this ->assertTrue ($ form ->isValid (), $ form ->getErrors ());
58
87
59
- $ this ->assertFormTypes (array ('text ' , 'text ' , 'text ' ), array ('text ' , 'text ' , 'text ' ), $ form );
88
+ $ this ->assertFormTypes (array (self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ] ), array (self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ] ), $ form );
60
89
$ this ->assertFormOptions (array (array ('label ' => 'label_key ' ), array ('label ' => 'label_value ' )), $ form );
61
90
62
91
$ this ->assertSame ($ expectedData , $ form ->getData ());
@@ -72,15 +101,26 @@ public function testWithChoiceType()
72
101
$ obj2 ->id = 2 ;
73
102
$ obj2 ->name = 'choice2 ' ;
74
103
75
- $ builder = $ this ->factory ->createBuilder ('burgov_key_value ' , null , array (
76
- 'value_type ' => 'choice ' ,
77
- 'key_options ' => array ('label ' => 'label_key ' ),
78
- 'value_options ' => array (
79
- 'choice_list ' => new ObjectChoiceList (array ($ obj1 , $ obj2 ), 'name ' , array (), null , 'id ' ),
80
- 'label ' => 'label_value '
81
- )));
104
+ $ valueOptions = array ('label ' => 'label_value ' );
105
+ if (self ::$ isSf28 ) {
106
+ // Symfony 2.8+
107
+ $ valueOptions ['choices ' ] = array ($ obj1 , $ obj2 );
108
+ $ valueOptions ['choice_name ' ] = 'id ' ;
109
+ $ valueOptions ['choice_value ' ] = 'name ' ;
110
+ } else {
111
+ // Symfony <2.8
112
+ $ valueOptions ['choice_list ' ] = new ObjectChoiceList (array ($ obj1 , $ obj2 ), 'name ' , array (), null , 'id ' );
113
+ }
82
114
83
- $ form = $ builder ->getForm ();
115
+ $ form = $ this ->factory ->create (
116
+ self ::$ typeNames ['burgov_key_value ' ],
117
+ null ,
118
+ array (
119
+ 'value_type ' => self ::$ typeNames ['choice ' ],
120
+ 'key_options ' => array ('label ' => 'label_key ' ),
121
+ 'value_options ' => $ valueOptions ,
122
+ )
123
+ );
84
124
85
125
$ this ->assertFormTypes (array (), array (), $ form );
86
126
$ this ->assertFormOptions (array (array ('label ' => 'label_key ' ), array ('label ' => 'label_value ' )), $ form );
@@ -96,7 +136,7 @@ public function testWithChoiceType()
96
136
)
97
137
));
98
138
99
- $ this ->assertFormTypes (array ('text ' , 'text ' ), array ('choice ' , 'choice ' ), $ form );
139
+ $ this ->assertFormTypes (array (self :: $ typeNames [ 'text ' ], self :: $ typeNames [ 'text ' ] ), array (self :: $ typeNames [ 'choice ' ], self :: $ typeNames [ 'choice ' ] ), $ form );
100
140
$ this ->assertFormOptions (array (array ('label ' => 'label_key ' ), array ('label ' => 'label_value ' )), $ form );
101
141
102
142
$ this ->assertTrue ($ form ->isValid ());
@@ -106,14 +146,12 @@ public function testWithChoiceType()
106
146
107
147
public function testWithCustomKeyType ()
108
148
{
109
- $ builder = $ this ->factory ->createBuilder ( 'burgov_key_value ' , null , array (
149
+ $ form = $ this ->factory ->create ( self :: $ typeNames [ 'burgov_key_value ' ] , null , array (
110
150
'key_type ' => 'country ' ,
111
151
'value_type ' => 'integer ' ,
112
152
'key_options ' => array ('label ' => 'label_key ' ),
113
153
));
114
154
115
- $ form = $ builder ->getForm ();
116
-
117
155
$ this ->assertFormTypes (array (), array (), $ form );
118
156
$ this ->assertFormOptions (array (array ('label ' => 'label_key ' ), array ()), $ form );
119
157
@@ -141,10 +179,11 @@ private function assertFormTypes(array $keys, array $values, $form)
141
179
$ this ->assertCount (count ($ values ), $ form );
142
180
for ($ i = 0 ; $ i < count ($ form ); $ i ++) {
143
181
if (isset ($ keys [$ i ])) {
144
- $ this ->assertEquals ($ keys [$ i ], $ form ->get ($ i )->get ('key ' )->getConfig ()->getType ()->getInnerType ()->getName ());
182
+
183
+ $ this ->assertInstanceOf ($ keys [$ i ], $ form ->get ($ i )->get ('key ' )->getConfig ()->getType ()->getInnerType ());
145
184
}
146
185
if (isset ($ values [$ i ])) {
147
- $ this ->assertEquals ($ values [$ i ], $ form ->get ($ i )->get ('value ' )->getConfig ()->getType ()->getInnerType ()-> getName ());
186
+ $ this ->assertInstanceOf ($ values [$ i ], $ form ->get ($ i )->get ('value ' )->getConfig ()->getType ()->getInnerType ());
148
187
}
149
188
}
150
189
}
0 commit comments