FormErrorHandlerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace JMS\Serializer\Tests\Handler;
  3. use JMS\Serializer\Handler\FormErrorHandler;
  4. use JMS\Serializer\JsonSerializationVisitor;
  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\Form\Forms;
  12. use Symfony\Component\Translation\Translator;
  13. class FormErrorHandlerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var \JMS\Serializer\Handler\FormErrorHandler
  17. */
  18. protected $handler;
  19. /**
  20. * @var \JMS\Serializer\VisitorInterface
  21. */
  22. protected $visitor;
  23. /**
  24. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  25. */
  26. protected $dispatcher;
  27. /**
  28. * @var \Symfony\Component\Form\FormFactoryInterface
  29. */
  30. protected $factory;
  31. public function setUp()
  32. {
  33. $this->handler = new FormErrorHandler(new Translator('en'));
  34. $this->visitor = new JsonSerializationVisitor(new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()));
  35. $this->dispatcher = new EventDispatcher();
  36. $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  37. }
  38. protected function tearDown()
  39. {
  40. $this->handler = null;
  41. $this->visitor = null;
  42. $this->dispatcher = null;
  43. $this->factory = null;
  44. }
  45. public function testSerializeEmptyFormError()
  46. {
  47. $form = $this->createForm();
  48. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  49. $this->assertSame('{}', $json);
  50. }
  51. public function testErrorHandlerWithoutTranslator()
  52. {
  53. $this->handler = new FormErrorHandler();
  54. $form = $this->createForm();
  55. $form->addError(new FormError('error!'));
  56. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  57. $this->assertSame(json_encode(array(
  58. 'errors' => array(
  59. 'error!',
  60. ),
  61. )), $json);
  62. }
  63. public function testSerializeHasFormError()
  64. {
  65. $form = $this->createForm();
  66. $form->addError(new FormError('error!'));
  67. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  68. $this->assertSame(json_encode(array(
  69. 'errors' => array(
  70. 'error!',
  71. ),
  72. )), $json);
  73. }
  74. public function testSerializeChildElements()
  75. {
  76. $formFactory = Forms::createFormFactory();
  77. $form = $formFactory->createBuilder()
  78. ->add('child')
  79. ->add('date')
  80. ->getForm();
  81. $form->addError(new FormError('error!'));
  82. $form->get('date')->addError(new FormError('child-error'));
  83. $json = json_encode($this->handler->serializeFormToJson($this->visitor, $form, array()));
  84. $this->assertSame(json_encode(array(
  85. 'errors' => array(
  86. 'error!',
  87. ),
  88. 'children' => [
  89. 'child' => new \stdClass(),
  90. 'date' => ['errors' => ['child-error']]
  91. ]
  92. )), $json);
  93. }
  94. public function testDefaultTranslationDomain()
  95. {
  96. /** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
  97. $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
  98. $handler = new FormErrorHandler($translator);
  99. $translator->expects($this->once())
  100. ->method('trans')
  101. ->with(
  102. $this->equalTo('error!'),
  103. $this->equalTo([]),
  104. $this->equalTo('validators')
  105. );
  106. $formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
  107. $formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
  108. $formError->expects($this->once())->method('getMessagePluralization')->willReturn(null);
  109. $formError->expects($this->once())->method('getMessageParameters')->willReturn([]);
  110. $this->invokeMethod($handler, 'getErrorMessage', [$formError,]);
  111. }
  112. public function testDefaultTranslationDomainWithPluralTranslation()
  113. {
  114. /** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
  115. $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
  116. $handler = new FormErrorHandler($translator);
  117. $translator->expects($this->once())
  118. ->method('transChoice')
  119. ->with(
  120. $this->equalTo('error!'),
  121. $this->equalTo(0),
  122. $this->equalTo([]),
  123. $this->equalTo('validators')
  124. );
  125. $formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
  126. $formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
  127. $formError->expects($this->exactly(2))->method('getMessagePluralization')->willReturn(0);
  128. $formError->expects($this->once())->method('getMessageParameters')->willReturn([]);
  129. $this->invokeMethod($handler, 'getErrorMessage', [$formError,]);
  130. }
  131. public function testCustomTranslationDomain()
  132. {
  133. /** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
  134. $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
  135. $handler = new FormErrorHandler($translator, 'custom_domain');
  136. $translator->expects($this->once())
  137. ->method('trans')
  138. ->with(
  139. $this->equalTo('error!'),
  140. $this->equalTo([]),
  141. $this->equalTo('custom_domain')
  142. );
  143. $formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
  144. $formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
  145. $formError->expects($this->once())->method('getMessagePluralization')->willReturn(null);
  146. $formError->expects($this->once())->method('getMessageParameters')->willReturn([]);
  147. $this->invokeMethod($handler, 'getErrorMessage', [$formError,]);
  148. }
  149. public function testCustomTranslationDomainWithPluralTranslation()
  150. {
  151. /** @var Translator|\PHPUnit_Framework_MockObject_MockObject $translator */
  152. $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
  153. $handler = new FormErrorHandler($translator, 'custom_domain');
  154. $translator->expects($this->once())
  155. ->method('transChoice')
  156. ->with(
  157. $this->equalTo('error!'),
  158. $this->equalTo(0),
  159. $this->equalTo([]),
  160. $this->equalTo('custom_domain')
  161. );
  162. $formError = $this->getMockBuilder('Symfony\Component\Form\FormError')->disableOriginalConstructor()->getMock();
  163. $formError->expects($this->once())->method('getMessageTemplate')->willReturn('error!');
  164. $formError->expects($this->exactly(2))->method('getMessagePluralization')->willReturn(0);
  165. $formError->expects($this->once())->method('getMessageParameters')->willReturn([]);
  166. $this->invokeMethod($handler, 'getErrorMessage', [$formError,]);
  167. }
  168. /**
  169. * @param string $name
  170. * @param EventDispatcherInterface $dispatcher
  171. * @param string $dataClass
  172. *
  173. * @return FormBuilder
  174. */
  175. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null, $dataClass = null)
  176. {
  177. return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory);
  178. }
  179. /**
  180. * @param string $name
  181. *
  182. * @return \PHPUnit_Framework_MockObject_MockObject
  183. */
  184. protected function getMockForm($name = 'name')
  185. {
  186. $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
  187. $config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
  188. $form->expects($this->any())
  189. ->method('getName')
  190. ->will($this->returnValue($name));
  191. $form->expects($this->any())
  192. ->method('getConfig')
  193. ->will($this->returnValue($config));
  194. return $form;
  195. }
  196. protected function createForm()
  197. {
  198. return $this->getBuilder()->getForm();
  199. }
  200. protected function invokeMethod($object, $method, array $args = [])
  201. {
  202. $reflectionMethod = new \ReflectionMethod($object, $method);
  203. $reflectionMethod->setAccessible(true);
  204. return $reflectionMethod->invokeArgs($object, $args);
  205. }
  206. }