PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Oro/Bundle/EmbeddedFormBundle/Tests/Unit/Manager/EmbeddedFormManagerTest.php

https://github.com/diimpp/platform
PHP | 239 lines | 150 code | 36 blank | 53 comment | 1 complexity | bbbff37a4910129bc3845a843aefece7 MD5 | raw file
  1. <?php
  2. namespace Oro\Bundle\EmbeddedFormBundle\Tests\Unit\Manager;
  3. use Oro\Bundle\EmbeddedFormBundle\Manager\EmbeddedFormManager;
  4. class EmbeddedFormManagerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @test
  8. */
  9. public function shouldBeConstructedWithContainerAndFormFactory()
  10. {
  11. new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  12. }
  13. /**
  14. * @test
  15. */
  16. public function shouldCreateForm()
  17. {
  18. $type = 'type';
  19. $container = $this->createContainerMock();
  20. $formFactory = $this->createFormFactoryMock();
  21. $manager = new EmbeddedFormManager($container, $formFactory);
  22. $formInstance = new \stdClass();
  23. $formFactory->expects($this->once())
  24. ->method('create')
  25. ->with($type, null, ['channel_form_type' => 'oro_entity_identifier'])
  26. ->will($this->returnValue($formInstance))
  27. ;
  28. $this->assertSame($formInstance, $manager->createForm($type));
  29. }
  30. /**
  31. * @test
  32. */
  33. public function shouldAllowToAddFormType()
  34. {
  35. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  36. $type = uniqid();
  37. $manager->addFormType($type);
  38. }
  39. /**
  40. * @test
  41. */
  42. public function shouldAllowToAddFormTypeWithLabel()
  43. {
  44. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  45. $type = uniqid();
  46. $label = uniqid('label');
  47. $manager->addFormType($type, $label);
  48. }
  49. /**
  50. * @test
  51. */
  52. public function shouldReturnEmptyLabelForNotAddedType()
  53. {
  54. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  55. $type = uniqid();
  56. $this->assertNull($manager->getLabelByType($type));
  57. }
  58. /**
  59. * @test
  60. */
  61. public function shouldReturnLabelForAddedType()
  62. {
  63. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  64. $type = uniqid();
  65. $label = uniqid('label');
  66. $manager->addFormType($type, $label);
  67. $this->assertEquals($label, $manager->getLabelByType($type));
  68. }
  69. /**
  70. * @test
  71. */
  72. public function shouldReturnTypeAsLabelForAddedTypeWithoutLabel()
  73. {
  74. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  75. $type = uniqid();
  76. $manager->addFormType($type);
  77. $this->assertEquals($type, $manager->getLabelByType($type));
  78. }
  79. /**
  80. * @test
  81. */
  82. public function shouldReturnAllAddedTypes()
  83. {
  84. $types = [
  85. $type1 = uniqid('type') => uniqid('label'),
  86. $type2 = uniqid('type') => uniqid('label'),
  87. ];
  88. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  89. $manager->addFormType($type1, $types[$type1]);
  90. $manager->addFormType($type2, $types[$type2]);
  91. $this->assertEquals($types, $manager->getAll());
  92. }
  93. /**
  94. * @test
  95. */
  96. public function shouldReturnEmptyDefaultCss()
  97. {
  98. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  99. $this->assertEquals('', $manager->getDefaultCssByType(uniqid('type')));
  100. }
  101. /**
  102. * @test
  103. */
  104. public function shouldReturnDefaultCss()
  105. {
  106. $type = 'type';
  107. $typeInstance = $this->getMock('Oro\Bundle\EmbeddedFormBundle\Form\Type\EmbeddedFormInterface');
  108. $container = $this->createContainerMock($typeInstance);
  109. $formFactory = $this->createFormFactoryMock();
  110. $defaultCss = 'my default css';
  111. $typeInstance->expects($this->once())
  112. ->method('getDefaultCss')
  113. ->will($this->returnValue($defaultCss));
  114. $manager = new EmbeddedFormManager($container, $formFactory);
  115. $this->assertEquals($defaultCss, $manager->getDefaultCssByType($type));
  116. }
  117. /**
  118. * @test
  119. */
  120. public function shouldReturnDefaultSuccessMessage()
  121. {
  122. $type = 'type';
  123. $typeInstance = $this->getMock('Oro\Bundle\EmbeddedFormBundle\Form\Type\EmbeddedFormInterface');
  124. $container = $this->createContainerMock($typeInstance);
  125. $formFactory = $this->createFormFactoryMock();
  126. $defaultMessage = 'my default message';
  127. $typeInstance->expects($this->once())
  128. ->method('getDefaultSuccessMessage')
  129. ->will($this->returnValue($defaultMessage));
  130. $manager = new EmbeddedFormManager($container, $formFactory);
  131. $this->assertEquals($defaultMessage, $manager->getDefaultSuccessMessageByType($type));
  132. }
  133. /**
  134. * @test
  135. */
  136. public function shouldReturnEmptyDefaultSuccessMessage()
  137. {
  138. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  139. $this->assertEquals('', $manager->getDefaultSuccessMessageByType(uniqid('type')));
  140. }
  141. /**
  142. * @test
  143. */
  144. public function shouldReturnEmptyCustomFormLayoutByFormType()
  145. {
  146. $manager = new EmbeddedFormManager($this->createContainerMock(), $this->createFormFactoryMock());
  147. $this->assertEquals('', $manager->getCustomFormLayoutByFormType(uniqid('type')));
  148. }
  149. /**
  150. * @test
  151. * @dataProvider customLayoutTypesProvider
  152. */
  153. public function shouldReturnCustomFormLayoutByFormType($typeInstance, $expectedLayout)
  154. {
  155. $container = $this->createContainerMock($typeInstance);
  156. $formFactory = $this->createFormFactoryMock();
  157. $type = 'type';
  158. $manager = new EmbeddedFormManager($container, $formFactory);
  159. $this->assertEquals($expectedLayout, $manager->getCustomFormLayoutByFormType($type));
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function customLayoutTypesProvider()
  165. {
  166. $customLayout = 'layout.html.twig';
  167. $typeInstance = $this->getMock('Oro\Bundle\EmbeddedFormBundle\Form\Type\CustomLayoutFormTypeInterface');
  168. $newInterfaceTypeInstance = $this->getMock('Oro\Bundle\EmbeddedFormBundle\Form\Type\CustomLayoutFormInterface');
  169. $typeInstance->expects($this->any())->method('geFormLayout')
  170. ->will($this->returnValue($customLayout));
  171. $newInterfaceTypeInstance->expects($this->any())->method('getFormLayout')
  172. ->will($this->returnValue($customLayout));
  173. return [
  174. 'not custom layout aware form' => [null, ''],
  175. 'deprecated interface, should be used' => [$typeInstance, $customLayout],
  176. 'new interface, should be used' => [$newInterfaceTypeInstance, $customLayout],
  177. ];
  178. }
  179. /**
  180. * @return \PHPUnit_Framework_MockObject_MockObject
  181. */
  182. protected function createFormFactoryMock()
  183. {
  184. return $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  185. }
  186. /**
  187. * @param null|object $typeInstance
  188. * @return \PHPUnit_Framework_MockObject_MockObject
  189. */
  190. protected function createContainerMock($typeInstance = null)
  191. {
  192. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  193. if ($typeInstance) {
  194. $container->expects($this->once())
  195. ->method('has')
  196. ->will($this->returnValue(true));
  197. $container->expects($this->once())
  198. ->method('get')
  199. ->will($this->returnValue($typeInstance));
  200. }
  201. return $container;
  202. }
  203. }