From c8767d6e72ca1661077a676520ecc53c9406ff17 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Thu, 7 Jan 2016 16:32:02 +0100 Subject: [PATCH] Fixed validation_groups when option is a Closure Update composer.json dependencies Added some unit tests --- Factory/JsFormValidatorFactory.php | 4 +- Tests/Factory/JsFormValidatorFactoryTest.php | 121 +++++++++++++++++++ composer.json | 2 +- 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 Tests/Factory/JsFormValidatorFactoryTest.php diff --git a/Factory/JsFormValidatorFactory.php b/Factory/JsFormValidatorFactory.php index b7cdd8c..a34a665 100644 --- a/Factory/JsFormValidatorFactory.php +++ b/Factory/JsFormValidatorFactory.php @@ -428,8 +428,8 @@ protected function getValidationGroups(Form $form) // If groups is an array - return groups as is $result = $groups; } elseif ($groups instanceof \Closure) { - // If groups is a Closure - return the form class name to look for javascript - $result = $this->getElementId($form); + // If groups is a Closure - return the closure response + $result = $groups($form); } return $result; diff --git a/Tests/Factory/JsFormValidatorFactoryTest.php b/Tests/Factory/JsFormValidatorFactoryTest.php new file mode 100644 index 0000000..64bd672 --- /dev/null +++ b/Tests/Factory/JsFormValidatorFactoryTest.php @@ -0,0 +1,121 @@ +factory = $this->getMockBuilder('Fp\JsFormValidatorBundle\Factory\JsFormValidatorFactory') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Tears down test class properties. + */ + public function tearDown() + { + $this->factory = null; + } + + /** + * Tests the getValidationGroups() method when returning an empty array. + */ + public function testGetValidationGroupsWhenEmpty() + { + // Given + $formConfig = $this->getMock('Symfony\Component\Form\FormConfigInterface'); + $formConfig + ->expects($this->once()) + ->method('getOption') + ->with($this->equalTo('validation_groups')) + ->will($this->returnValue(null)); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form') + ->disableOriginalConstructor() + ->getMock(); + + $form->expects($this->once())->method('getConfig')->will($this->returnValue($formConfig)); + + $factory = new \ReflectionMethod($this->factory, 'getValidationGroups'); + $factory->setAccessible(true); + + // When + $result = $factory->invoke($this->factory, $form); + + // Then + $this->assertEquals(array('Default'), $result, 'Should return Default as validation_groups'); + } + + /** + * Tests the getValidationGroups() method when using a simple array. + */ + public function testGetValidationGroupsWhenArray() + { + // Given + $formConfig = $this->getMock('Symfony\Component\Form\FormConfigInterface'); + $formConfig + ->expects($this->once()) + ->method('getOption') + ->with($this->equalTo('validation_groups')) + ->will($this->returnValue(array('test1', 'test2'))); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form') + ->disableOriginalConstructor() + ->getMock(); + + $form->expects($this->once())->method('getConfig')->will($this->returnValue($formConfig)); + + $factory = new \ReflectionMethod($this->factory, 'getValidationGroups'); + $factory->setAccessible(true); + + // When + $result = $factory->invoke($this->factory, $form); + + // Then + $this->assertEquals(array('test1', 'test2'), $result, 'Should return the validation_groups array'); + } + + /** + * Tests the getValidationGroups() method when using a Closure. + */ + public function testGetValidationGroupsWhenClosure() + { + // Given + $formConfig = $this->getMock('Symfony\Component\Form\FormConfigInterface'); + $formConfig + ->expects($this->once()) + ->method('getOption') + ->with($this->equalTo('validation_groups')) + ->will($this->returnValue(function () { return array('person'); })); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form') + ->disableOriginalConstructor() + ->getMock(); + + $form->expects($this->once())->method('getConfig')->will($this->returnValue($formConfig)); + + $factory = new \ReflectionMethod($this->factory, 'getValidationGroups'); + $factory->setAccessible(true); + + // When + $result = $factory->invoke($this->factory, $form); + + // Then + $this->assertEquals(array('person'), $result, 'Should return the closure response as validation_groups'); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index a860e69..61f69eb 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "phpunit/phpunit": "3.7.*", "behat/mink-bundle": "dev-master", "behat/mink-selenium2-driver": "1.1.0", - "satooshi/php-coveralls": "dev-master", + "satooshi/php-coveralls": "~1.0", "se/selenium-server-standalone": "2.45.0" },