ErrorElementTest.php 4.8 KB

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