1+ <?php
2+
3+ namespace Fp \JsFormValidatorBundle \Tests \Factory ;
4+
5+ use Fp \JsFormValidatorBundle \Factory \JsFormValidatorFactory ;
6+
7+ /**
8+ * Class JsFormValidatorFactoryTest
9+ */
10+ class JsFormValidatorFactoryTest extends \PHPUnit_Framework_TestCase
11+ {
12+ /**
13+ * @var JsFormValidatorFactory
14+ */
15+ protected $ factory ;
16+
17+ /**
18+ * Sets up a new test factory instance.
19+ */
20+ public function setUp ()
21+ {
22+ $ this ->factory = $ this ->getMockBuilder ('Fp\JsFormValidatorBundle\Factory\JsFormValidatorFactory ' )
23+ ->disableOriginalConstructor ()
24+ ->getMock ();
25+ }
26+
27+ /**
28+ * Tears down test class properties.
29+ */
30+ public function tearDown ()
31+ {
32+ $ this ->factory = null ;
33+ }
34+
35+ /**
36+ * Tests the getValidationGroups() method when returning an empty array.
37+ */
38+ public function testGetValidationGroupsWhenEmpty ()
39+ {
40+ // Given
41+ $ formConfig = $ this ->getMock ('Symfony\Component\Form\FormConfigInterface ' );
42+ $ formConfig
43+ ->expects ($ this ->once ())
44+ ->method ('getOption ' )
45+ ->with ($ this ->equalTo ('validation_groups ' ))
46+ ->will ($ this ->returnValue (null ));
47+
48+ $ form = $ this ->getMockBuilder ('Symfony\Component\Form\Form ' )
49+ ->disableOriginalConstructor ()
50+ ->getMock ();
51+
52+ $ form ->expects ($ this ->once ())->method ('getConfig ' )->will ($ this ->returnValue ($ formConfig ));
53+
54+ $ factory = new \ReflectionMethod ($ this ->factory , 'getValidationGroups ' );
55+ $ factory ->setAccessible (true );
56+
57+ // When
58+ $ result = $ factory ->invoke ($ this ->factory , $ form );
59+
60+ // Then
61+ $ this ->assertEquals (array ('Default ' ), $ result , 'Should return Default as validation_groups ' );
62+ }
63+
64+ /**
65+ * Tests the getValidationGroups() method when using a simple array.
66+ */
67+ public function testGetValidationGroupsWhenArray ()
68+ {
69+ // Given
70+ $ formConfig = $ this ->getMock ('Symfony\Component\Form\FormConfigInterface ' );
71+ $ formConfig
72+ ->expects ($ this ->once ())
73+ ->method ('getOption ' )
74+ ->with ($ this ->equalTo ('validation_groups ' ))
75+ ->will ($ this ->returnValue (array ('test1 ' , 'test2 ' )));
76+
77+ $ form = $ this ->getMockBuilder ('Symfony\Component\Form\Form ' )
78+ ->disableOriginalConstructor ()
79+ ->getMock ();
80+
81+ $ form ->expects ($ this ->once ())->method ('getConfig ' )->will ($ this ->returnValue ($ formConfig ));
82+
83+ $ factory = new \ReflectionMethod ($ this ->factory , 'getValidationGroups ' );
84+ $ factory ->setAccessible (true );
85+
86+ // When
87+ $ result = $ factory ->invoke ($ this ->factory , $ form );
88+
89+ // Then
90+ $ this ->assertEquals (array ('test1 ' , 'test2 ' ), $ result , 'Should return the validation_groups array ' );
91+ }
92+
93+ /**
94+ * Tests the getValidationGroups() method when using a Closure.
95+ */
96+ public function testGetValidationGroupsWhenClosure ()
97+ {
98+ // Given
99+ $ formConfig = $ this ->getMock ('Symfony\Component\Form\FormConfigInterface ' );
100+ $ formConfig
101+ ->expects ($ this ->once ())
102+ ->method ('getOption ' )
103+ ->with ($ this ->equalTo ('validation_groups ' ))
104+ ->will ($ this ->returnValue (function () { return array ('person ' ); }));
105+
106+ $ form = $ this ->getMockBuilder ('Symfony\Component\Form\Form ' )
107+ ->disableOriginalConstructor ()
108+ ->getMock ();
109+
110+ $ form ->expects ($ this ->once ())->method ('getConfig ' )->will ($ this ->returnValue ($ formConfig ));
111+
112+ $ factory = new \ReflectionMethod ($ this ->factory , 'getValidationGroups ' );
113+ $ factory ->setAccessible (true );
114+
115+ // When
116+ $ result = $ factory ->invoke ($ this ->factory , $ form );
117+
118+ // Then
119+ $ this ->assertEquals (array ('person ' ), $ result , 'Should return the closure response as validation_groups ' );
120+ }
121+ }
0 commit comments