Skip to content

Commit 204a83e

Browse files
committed
Merge pull request #99 from 66Ton99/eko
Fixed validation_groups when option is a Closure #93
2 parents 54dd7fe + 5a18c08 commit 204a83e

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

Factory/JsFormValidatorFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ protected function getValidationGroups(Form $form)
428428
// If groups is an array - return groups as is
429429
$result = $groups;
430430
} elseif ($groups instanceof \Closure) {
431-
// If groups is a Closure - return the form class name to look for javascript
432-
$result = $this->getElementId($form);
431+
// If groups is a Closure - return the closure response
432+
$result = $groups($form);
433433
}
434434

435435
return $result;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)