ExecutionContextTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Tests\Component\Validator;
  11. use Symfony\Component\Validator\ExecutionContext;
  12. class ExecutionContextTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $walker;
  15. protected $metadataFactory;
  16. protected $context;
  17. protected function setUp()
  18. {
  19. $this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
  20. $this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  21. $this->context = new ExecutionContext('Root', $this->walker, $this->metadataFactory);
  22. }
  23. protected function tearDown()
  24. {
  25. $this->walker = null;
  26. $this->metadataFactory = null;
  27. $this->context = null;
  28. }
  29. public function testClone()
  30. {
  31. $clone = clone $this->context;
  32. $this->assertNotSame($this->context, $clone);
  33. }
  34. public function testAddViolation()
  35. {
  36. $this->assertEquals(0, count($this->context->getViolations()));
  37. $this->context->addViolation('', array(), '');
  38. $this->assertEquals(1, count($this->context->getViolations()));
  39. }
  40. public function testGetViolations()
  41. {
  42. $this->context->addViolation('', array(), '');
  43. $violations = $this->context->getViolations();
  44. $this->assertEquals(1, count($violations));
  45. $this->assertInstanceOf('Symfony\Component\Validator\ConstraintViolationList', $violations);
  46. $this->assertInstanceOf('ArrayIterator', $violations->getIterator());
  47. $this->assertTrue(isset($violations[0]));
  48. $this->assertFalse(isset($violations[1]));
  49. $violations[] = 'fake';
  50. $this->assertEquals('fake', $violations[1]);
  51. $this->assertTrue(isset($violations[1]));
  52. unset($violations[1]);
  53. $this->assertFalse(isset($violations[1]));
  54. $violations[0] = 'fake';
  55. $this->assertEquals('fake', $violations[0]);
  56. }
  57. public function testViolationsMerge()
  58. {
  59. $this->context->addViolation('Message 1', array(), '');
  60. $this->context->addViolation('Message 2', array(), '');
  61. $violations1 = $this->context->getViolations();
  62. $this->context->addViolation('', array(), '');
  63. $violations2 = $this->context->getViolations();
  64. unset($violations2[1]);
  65. $violations1->addAll($violations2);
  66. $this->assertEmpty($violations1[2]->getMessage());
  67. }
  68. public function testViolationsAsString()
  69. {
  70. $this->context->addViolation('Message 1', array(), '');
  71. $this->context->addViolation('Message 2', array(), '');
  72. $violations = $this->context->getViolations();
  73. $expected = <<<EOF
  74. Root.:
  75. Message 1
  76. Root.:
  77. Message 2
  78. EOF;
  79. $this->assertEquals($expected, $violations->__toString());
  80. }
  81. public function testGetRoot()
  82. {
  83. $this->assertEquals('Root', $this->context->getRoot());
  84. }
  85. public function testSetGetPropertyPath()
  86. {
  87. $this->context->setPropertyPath('property_path');
  88. $this->assertEquals('property_path', $this->context->getPropertyPath());
  89. }
  90. public function testSetGetCurrentClass()
  91. {
  92. $this->context->setCurrentClass('current_class');
  93. $this->assertEquals('current_class', $this->context->getCurrentClass());
  94. }
  95. public function testSetGetCurrentProperty()
  96. {
  97. $this->context->setCurrentProperty('current_property');
  98. $this->assertEquals('current_property', $this->context->getCurrentProperty());
  99. }
  100. public function testSetGetGroup()
  101. {
  102. $this->context->setGroup('group');
  103. $this->assertEquals('group', $this->context->getGroup());
  104. }
  105. public function testGetGraphWalker()
  106. {
  107. $this->assertSame($this->walker, $this->context->getGraphWalker());
  108. $this->assertInstanceOf(
  109. 'Symfony\Component\Validator\GraphWalker',
  110. $this->context->getGraphWalker()
  111. );
  112. }
  113. public function testGetMetadataFactory()
  114. {
  115. $this->assertSame($this->metadataFactory, $this->context->getMetadataFactory());
  116. $this->assertInstanceOf(
  117. 'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
  118. $this->context->getMetadataFactory()
  119. );
  120. }
  121. }