FormErrorHandlerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace JMS\Serializer\Tests\Handler;
  3. use JMS\Serializer\JsonSerializationVisitor;
  4. use JMS\Serializer\Handler\FormErrorHandler;
  5. use JMS\Serializer\Naming\CamelCaseNamingStrategy;
  6. use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
  7. use Symfony\Component\EventDispatcher\EventDispatcher;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\Form\FormBuilder;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\Translation\Translator;
  12. class FormErrorHandlerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var \JMS\Serializer\Handler\FormErrorHandler
  16. */
  17. protected $handler;
  18. /**
  19. * @var \JMS\Serializer\VisitorInterface
  20. */
  21. protected $visitor;
  22. /**
  23. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  24. */
  25. protected $dispatcher;
  26. /**
  27. * @var \Symfony\Component\Form\FormFactoryInterface
  28. */
  29. protected $factory;
  30. public function setUp()
  31. {
  32. $this->handler = new FormErrorHandler(new Translator('en'));
  33. $this->visitor = new JsonSerializationVisitor(new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()));
  34. $this->dispatcher = new EventDispatcher();
  35. $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  36. }
  37. protected function tearDown()
  38. {
  39. $this->handler = null;
  40. $this->visitor = null;
  41. $this->dispatcher = null;
  42. $this->factory = null;
  43. }
  44. public function testSerializeEmptyFormError()
  45. {
  46. $form = $this->createForm();
  47. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  48. $this->assertSame('{}', $json);
  49. }
  50. public function testSerializeHasFormError()
  51. {
  52. $form = $this->createForm();
  53. $form->addError(new FormError('error!'));
  54. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  55. $this->assertSame(json_encode(array(
  56. 'errors' => array(
  57. 'error!',
  58. ),
  59. )), $json);
  60. }
  61. /**
  62. * @param string $name
  63. * @param EventDispatcherInterface $dispatcher
  64. * @param string $dataClass
  65. *
  66. * @return FormBuilder
  67. */
  68. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null, $dataClass = null)
  69. {
  70. return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory);
  71. }
  72. /**
  73. * @param string $name
  74. *
  75. * @return \PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected function getMockForm($name = 'name')
  78. {
  79. $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
  80. $config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
  81. $form->expects($this->any())
  82. ->method('getName')
  83. ->will($this->returnValue($name));
  84. $form->expects($this->any())
  85. ->method('getConfig')
  86. ->will($this->returnValue($config));
  87. return $form;
  88. }
  89. protected function createForm()
  90. {
  91. return $this->getBuilder()->getForm();
  92. }
  93. }