4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\ConstraintViolation;
16 use Symfony\Component\Validator\ConstraintViolationList;
18 class ConstraintViolationListTest extends TestCase
22 protected function setUp()
24 $this->list = new ConstraintViolationList();
27 protected function tearDown()
32 public function testInit()
34 $this->assertCount(0, $this->list);
37 public function testInitWithViolations()
39 $violation = $this->getViolation('Error');
40 $this->list = new ConstraintViolationList(array($violation));
42 $this->assertCount(1, $this->list);
43 $this->assertSame($violation, $this->list[0]);
46 public function testAdd()
48 $violation = $this->getViolation('Error');
49 $this->list->add($violation);
51 $this->assertCount(1, $this->list);
52 $this->assertSame($violation, $this->list[0]);
55 public function testAddAll()
58 10 => $this->getViolation('Error 1'),
59 20 => $this->getViolation('Error 2'),
60 30 => $this->getViolation('Error 3'),
62 $otherList = new ConstraintViolationList($violations);
63 $this->list->addAll($otherList);
65 $this->assertCount(3, $this->list);
67 $this->assertSame($violations[10], $this->list[0]);
68 $this->assertSame($violations[20], $this->list[1]);
69 $this->assertSame($violations[30], $this->list[2]);
72 public function testIterator()
75 10 => $this->getViolation('Error 1'),
76 20 => $this->getViolation('Error 2'),
77 30 => $this->getViolation('Error 3'),
80 $this->list = new ConstraintViolationList($violations);
82 // indices are reset upon adding -> array_values()
83 $this->assertSame(array_values($violations), iterator_to_array($this->list));
86 public function testArrayAccess()
88 $violation = $this->getViolation('Error');
89 $this->list[] = $violation;
91 $this->assertSame($violation, $this->list[0]);
92 $this->assertTrue(isset($this->list[0]));
94 unset($this->list[0]);
96 $this->assertFalse(isset($this->list[0]));
98 $this->list[10] = $violation;
100 $this->assertSame($violation, $this->list[10]);
101 $this->assertTrue(isset($this->list[10]));
104 public function testToString()
106 $this->list = new ConstraintViolationList(array(
107 $this->getViolation('Error 1', 'Root'),
108 $this->getViolation('Error 2', 'Root', 'foo.bar'),
109 $this->getViolation('Error 3', 'Root', '[baz]'),
110 $this->getViolation('Error 4', '', 'foo.bar'),
111 $this->getViolation('Error 5', '', '[baz]'),
128 $this->assertEquals($expected, (string) $this->list);
131 protected function getViolation($message, $root = null, $propertyPath = null)
133 return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);