Created
June 23, 2013 14:20
-
-
Save cristianoc72/5845175 to your computer and use it in GitHub Desktop.
Unit test for violations_to_array function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'violations_to_array.php'; | |
use Symfony\Component\Validator\ConstraintViolation; | |
use Symfony\Component\Validator\ConstraintViolationList; | |
class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase | |
{ | |
protected $list; | |
protected function setUp() | |
{ | |
$this->list = new ConstraintViolationList(); | |
} | |
protected function tearDown() | |
{ | |
$this->list = null; | |
} | |
public function testToArray() | |
{ | |
$this->list = new ConstraintViolationList(array( | |
$this->getViolation('Error 1', 'Root', 'foo'), | |
$this->getViolation('Error 2', 'Root', 'foo'), | |
$this->getViolation('Error 3', 'Root', 'bar'), | |
$this->getViolation('Error 4', 'Root 2', 'foo'), | |
$this->getViolation('Error 5', 'Root 3', 'baz'), | |
)); | |
$expectedNoFilters = array( | |
'foo' => array('Error 1', 'Error 2', 'Error 4'), | |
'bar' => array('Error 3'), | |
'baz' => array('Error 5') | |
); | |
$this->assertEquals($expectedNoFilters, violations_to_array($this->list)); | |
$expectedFilterPropertyPath = array( | |
'foo' => array('Error 1', 'Error 2', 'Error 4') | |
); | |
$this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo')); | |
//Wrong value causes to return empty array | |
$this->assertEquals(array(), violations_to_array($this->list, 'wrong')); | |
} | |
protected function getViolation($message, $root = null, $propertyPath = null) | |
{ | |
return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment