ConstraintViolationListTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\ConstraintViolation;
  13. use Symfony\Component\Validator\ConstraintViolationList;
  14. class ConstraintViolationListTest extends TestCase
  15. {
  16. protected $list;
  17. protected function setUp()
  18. {
  19. $this->list = new ConstraintViolationList();
  20. }
  21. protected function tearDown()
  22. {
  23. $this->list = null;
  24. }
  25. public function testInit()
  26. {
  27. $this->assertCount(0, $this->list);
  28. }
  29. public function testInitWithViolations()
  30. {
  31. $violation = $this->getViolation('Error');
  32. $this->list = new ConstraintViolationList(array($violation));
  33. $this->assertCount(1, $this->list);
  34. $this->assertSame($violation, $this->list[0]);
  35. }
  36. public function testAdd()
  37. {
  38. $violation = $this->getViolation('Error');
  39. $this->list->add($violation);
  40. $this->assertCount(1, $this->list);
  41. $this->assertSame($violation, $this->list[0]);
  42. }
  43. public function testAddAll()
  44. {
  45. $violations = array(
  46. 10 => $this->getViolation('Error 1'),
  47. 20 => $this->getViolation('Error 2'),
  48. 30 => $this->getViolation('Error 3'),
  49. );
  50. $otherList = new ConstraintViolationList($violations);
  51. $this->list->addAll($otherList);
  52. $this->assertCount(3, $this->list);
  53. $this->assertSame($violations[10], $this->list[0]);
  54. $this->assertSame($violations[20], $this->list[1]);
  55. $this->assertSame($violations[30], $this->list[2]);
  56. }
  57. public function testIterator()
  58. {
  59. $violations = array(
  60. 10 => $this->getViolation('Error 1'),
  61. 20 => $this->getViolation('Error 2'),
  62. 30 => $this->getViolation('Error 3'),
  63. );
  64. $this->list = new ConstraintViolationList($violations);
  65. // indices are reset upon adding -> array_values()
  66. $this->assertSame(array_values($violations), iterator_to_array($this->list));
  67. }
  68. public function testArrayAccess()
  69. {
  70. $violation = $this->getViolation('Error');
  71. $this->list[] = $violation;
  72. $this->assertSame($violation, $this->list[0]);
  73. $this->assertArrayHasKey(0, $this->list);
  74. unset($this->list[0]);
  75. $this->assertArrayNotHasKey(0, $this->list);
  76. $this->list[10] = $violation;
  77. $this->assertSame($violation, $this->list[10]);
  78. $this->assertArrayHasKey(10, $this->list);
  79. }
  80. public function testToString()
  81. {
  82. $this->list = new ConstraintViolationList(array(
  83. $this->getViolation('Error 1', 'Root'),
  84. $this->getViolation('Error 2', 'Root', 'foo.bar'),
  85. $this->getViolation('Error 3', 'Root', '[baz]'),
  86. $this->getViolation('Error 4', '', 'foo.bar'),
  87. $this->getViolation('Error 5', '', '[baz]'),
  88. ));
  89. $expected = <<<'EOF'
  90. Root:
  91. Error 1
  92. Root.foo.bar:
  93. Error 2
  94. Root[baz]:
  95. Error 3
  96. foo.bar:
  97. Error 4
  98. [baz]:
  99. Error 5
  100. EOF;
  101. $this->assertEquals($expected, (string) $this->list);
  102. }
  103. protected function getViolation($message, $root = null, $propertyPath = null)
  104. {
  105. return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
  106. }
  107. }