ErrorElementTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Validator;
  11. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
  12. use Sonata\AdminBundle\Validator\ErrorElement;
  13. use Symfony\Component\Validator\Constraints\NotNull;
  14. /**
  15. * Test for ErrorElement.
  16. *
  17. * @author Andrej Hudec <pulzarraider@gmail.com>
  18. */
  19. class ErrorElementTest extends \PHPUnit_Framework_TestCase
  20. {
  21. private $errorElement;
  22. private $context;
  23. private $subject;
  24. protected function setUp()
  25. {
  26. $constraintValidatorFactory = $this->getMock('Symfony\Component\Validator\ConstraintValidatorFactoryInterface');
  27. $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
  28. $this->context->expects($this->once())
  29. ->method('getPropertyPath')
  30. ->will($this->returnValue('bar'));
  31. $this->subject = new Foo();
  32. $this->errorElement = new ErrorElement($this->subject, $constraintValidatorFactory, $this->context, 'foo_admin');
  33. }
  34. public function testGetSubject()
  35. {
  36. $this->assertSame($this->subject, $this->errorElement->getSubject());
  37. }
  38. public function testGetErrorsEmpty()
  39. {
  40. $this->assertSame(array(), $this->errorElement->getErrors());
  41. }
  42. public function testGetErrors()
  43. {
  44. $this->errorElement->addViolation('Foo error message', array('bar_param' => 'bar_param_lvalue'), 'BAR');
  45. $this->assertSame(array(array('Foo error message', array('bar_param' => 'bar_param_lvalue'), 'BAR')), $this->errorElement->getErrors());
  46. }
  47. public function testAddViolation()
  48. {
  49. $this->errorElement->addViolation(array('Foo error message', array('bar_param' => 'bar_param_lvalue'), 'BAR'));
  50. $this->assertSame(array(array('Foo error message', array('bar_param' => 'bar_param_lvalue'), 'BAR')), $this->errorElement->getErrors());
  51. }
  52. public function testAddConstraint()
  53. {
  54. $constraint = new NotNull();
  55. $this->context->expects($this->once())
  56. ->method('validateValue')
  57. ->with($this->equalTo($this->subject), $this->equalTo($constraint), $this->equalTo(''), $this->equalTo('foo_admin'))
  58. ->will($this->returnValue(null));
  59. $this->errorElement->addConstraint($constraint);
  60. }
  61. public function testWith()
  62. {
  63. $constraint = new NotNull();
  64. $this->context->expects($this->once())
  65. ->method('validateValue')
  66. ->with($this->equalTo(null), $this->equalTo($constraint), $this->equalTo('bar'), $this->equalTo('foo_admin'))
  67. ->will($this->returnValue(null));
  68. $this->errorElement->with('bar');
  69. $this->errorElement->addConstraint($constraint);
  70. $this->errorElement->end();
  71. }
  72. public function testCall()
  73. {
  74. $constraint = new NotNull();
  75. $this->context->expects($this->once())
  76. ->method('validateValue')
  77. ->with($this->equalTo(null), $this->equalTo($constraint), $this->equalTo('bar'), $this->equalTo('foo_admin'))
  78. ->will($this->returnValue(null));
  79. $this->errorElement->with('bar');
  80. $this->errorElement->assertNotNull();
  81. $this->errorElement->end();
  82. }
  83. public function testCallException()
  84. {
  85. $this->setExpectedException('RuntimeException', 'Unable to recognize the command');
  86. $this->errorElement->with('bar');
  87. $this->errorElement->baz();
  88. }
  89. public function testGetFullPropertyPath()
  90. {
  91. $this->errorElement->with('baz');
  92. $this->assertSame('bar.baz', $this->errorElement->getFullPropertyPath());
  93. $this->errorElement->end();
  94. $this->assertSame('bar', $this->errorElement->getFullPropertyPath());
  95. }
  96. public function testFluidInterface()
  97. {
  98. $constraint = new NotNull();
  99. $this->context->expects($this->any())
  100. ->method('validateValue')
  101. ->with($this->equalTo($this->subject), $this->equalTo($constraint), $this->equalTo(''), $this->equalTo('foo_admin'))
  102. ->will($this->returnValue(null));
  103. $this->assertSame($this->errorElement, $this->errorElement->with('baz'));
  104. $this->assertSame($this->errorElement, $this->errorElement->end());
  105. $this->assertSame($this->errorElement, $this->errorElement->addViolation('Foo error message', array('bar_param' => 'bar_param_lvalue'), 'BAR'));
  106. $this->assertSame($this->errorElement, $this->errorElement->addConstraint($constraint));
  107. $this->assertSame($this->errorElement, $this->errorElement->assertNotNull());
  108. }
  109. }