Skip to content

Commit

Permalink
Merge pull request #99 from 66Ton99/eko
Browse files Browse the repository at this point in the history
Fixed validation_groups when option is a Closure #93
  • Loading branch information
66Ton99 committed Jan 31, 2016
2 parents 54dd7fe + 5a18c08 commit 204a83e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Factory/JsFormValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
121 changes: 121 additions & 0 deletions Tests/Factory/JsFormValidatorFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Fp\JsFormValidatorBundle\Tests\Factory;

use Fp\JsFormValidatorBundle\Factory\JsFormValidatorFactory;

/**
* Class JsFormValidatorFactoryTest
*/
class JsFormValidatorFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JsFormValidatorFactory
*/
protected $factory;

/**
* Sets up a new test factory instance.
*/
public function setUp()
{
$this->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');
}
}

0 comments on commit 204a83e

Please sign in to comment.