Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},

Expand Down