FormMapperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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\Form;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
  13. use Symfony\Component\Form\FormBuilder;
  14. class FormMapperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var \Sonata\AdminBundle\Builder\FormContractorInterface
  18. */
  19. protected $contractor;
  20. /**
  21. * @var \Sonata\AdminBundle\Admin\AdminInterface
  22. */
  23. protected $admin;
  24. /**
  25. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  26. */
  27. protected $modelManager;
  28. /**
  29. * @var FormMapper
  30. */
  31. protected $formMapper;
  32. public function setUp()
  33. {
  34. $this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  35. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  36. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  37. $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
  38. $this->admin = new CleanAdmin('code', 'class', 'controller');
  39. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  40. // php 5.3 BC
  41. $fieldDescription = $this->getFieldDescriptionMock();
  42. $this->modelManager->expects($this->any())
  43. ->method('getNewFieldDescriptionInstance')
  44. ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
  45. $fieldDescriptionClone = clone $fieldDescription;
  46. $fieldDescriptionClone->setName($name);
  47. $fieldDescriptionClone->setOptions($options);
  48. return $fieldDescriptionClone;
  49. }));
  50. $this->admin->setModelManager($this->modelManager);
  51. $labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  52. $this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
  53. $this->formMapper = new FormMapper(
  54. $this->contractor,
  55. $formBuilder,
  56. $this->admin
  57. );
  58. }
  59. public function testWithNoOptions()
  60. {
  61. $this->formMapper->with('foobar');
  62. $this->assertEquals(array('default' => array (
  63. 'collapsed' => false,
  64. 'class' => false,
  65. 'description' => false,
  66. 'translation_domain' => null,
  67. 'auto_created' => true,
  68. 'groups' => array('foobar'),
  69. 'tab' => true,
  70. 'name' => 'default',
  71. 'box_class' => 'box box-primary'
  72. )), $this->admin->getFormTabs());
  73. $this->assertEquals(array('foobar' => array(
  74. 'collapsed' => false,
  75. 'class' => false,
  76. 'description' => false,
  77. 'translation_domain' => null,
  78. 'fields' => array (),
  79. 'name' => 'foobar',
  80. 'box_class' => 'box box-primary'
  81. )), $this->admin->getFormGroups());
  82. }
  83. public function testWithOptions()
  84. {
  85. $this->formMapper->with('foobar', array(
  86. 'translation_domain' => 'Foobar',
  87. ));
  88. $this->assertEquals(array('foobar' => array(
  89. 'collapsed' => false,
  90. 'class' => false,
  91. 'description' => false,
  92. 'translation_domain' => 'Foobar',
  93. 'fields' => array (),
  94. 'name' => 'foobar',
  95. 'box_class' => 'box box-primary'
  96. )), $this->admin->getFormGroups());
  97. $this->assertEquals(array('default' => array (
  98. 'collapsed' => false,
  99. 'class' => false,
  100. 'description' => false,
  101. 'translation_domain' => 'Foobar',
  102. 'auto_created' => true,
  103. 'groups' => array('foobar'),
  104. 'tab' => true,
  105. 'name' => 'default',
  106. 'box_class' => 'box box-primary'
  107. )), $this->admin->getFormTabs());
  108. }
  109. public function testWithFieldsCascadeTranslationDomain()
  110. {
  111. $this->contractor->expects($this->once())
  112. ->method('getDefaultOptions')
  113. ->will($this->returnValue(array()));
  114. $this->formMapper->with('foobar', array(
  115. 'translation_domain' => 'Foobar'
  116. ))
  117. ->add('foo', 'bar')
  118. ->end();
  119. $fieldDescription = $this->admin->getFormFieldDescription('foo');
  120. $this->assertEquals('foo', $fieldDescription->getName());
  121. $this->assertEquals('bar', $fieldDescription->getType());
  122. $this->assertEquals('Foobar', $fieldDescription->getTranslationDomain());
  123. $this->assertTrue($this->formMapper->has('foo'));
  124. $this->assertEquals(array('default' => array (
  125. 'collapsed' => false,
  126. 'class' => false,
  127. 'description' => false,
  128. 'translation_domain' => 'Foobar',
  129. 'auto_created' => true,
  130. 'groups' => array ('foobar'),
  131. 'tab' => true,
  132. 'name' => 'default',
  133. 'box_class' => 'box box-primary'
  134. )), $this->admin->getFormTabs());
  135. $this->assertEquals(array('foobar' => array(
  136. 'collapsed' => false,
  137. 'class' => false,
  138. 'description' => false,
  139. 'translation_domain' => 'Foobar',
  140. 'fields' => array(
  141. 'foo' => 'foo'
  142. ),
  143. 'name' => 'foobar',
  144. 'box_class' => 'box box-primary'
  145. )), $this->admin->getFormGroups());
  146. }
  147. public function testRemoveCascadeRemoveFieldFromFormGroup()
  148. {
  149. $this->formMapper->with('foo');
  150. $this->formMapper->remove('foo');
  151. }
  152. public function testIfTrueApply()
  153. {
  154. $this->contractor->expects($this->once())
  155. ->method('getDefaultOptions')
  156. ->will($this->returnValue(array()));
  157. $this->formMapper
  158. ->ifTrue(true)
  159. ->add('foo', 'bar')
  160. ->ifEnd()
  161. ;
  162. $this->assertTrue($this->formMapper->has('foo'));
  163. }
  164. public function testIfTrueNotApply()
  165. {
  166. $this->formMapper
  167. ->ifTrue(false)
  168. ->add('foo', 'bar')
  169. ->ifEnd()
  170. ;
  171. $this->assertFalse($this->formMapper->has('foo'));
  172. }
  173. public function testIfTrueCombination()
  174. {
  175. $this->contractor->expects($this->once())
  176. ->method('getDefaultOptions')
  177. ->will($this->returnValue(array()));
  178. $this->formMapper
  179. ->ifTrue(false)
  180. ->add('foo', 'bar')
  181. ->ifEnd()
  182. ->add('baz', 'foobaz')
  183. ;
  184. $this->assertFalse($this->formMapper->has('foo'));
  185. $this->assertTrue($this->formMapper->has('baz'));
  186. }
  187. public function testIfFalseApply()
  188. {
  189. $this->contractor->expects($this->once())
  190. ->method('getDefaultOptions')
  191. ->will($this->returnValue(array()));
  192. $this->formMapper
  193. ->ifFalse(false)
  194. ->add('foo', 'bar')
  195. ->ifEnd()
  196. ;
  197. $this->assertTrue($this->formMapper->has('foo'));
  198. }
  199. public function testIfFalseNotApply()
  200. {
  201. $this->formMapper
  202. ->ifFalse(true)
  203. ->add('foo', 'bar')
  204. ->ifEnd()
  205. ;
  206. $this->assertFalse($this->formMapper->has('foo'));
  207. }
  208. public function testIfFalseCombination()
  209. {
  210. $this->contractor->expects($this->once())
  211. ->method('getDefaultOptions')
  212. ->will($this->returnValue(array()));
  213. $this->formMapper
  214. ->ifFalse(true)
  215. ->add('foo', 'bar')
  216. ->ifEnd()
  217. ->add('baz', 'foobaz')
  218. ;
  219. $this->assertFalse($this->formMapper->has('foo'));
  220. $this->assertTrue($this->formMapper->has('baz'));
  221. }
  222. /**
  223. * @expectedException RuntimeException
  224. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  225. */
  226. public function testIfTrueNested()
  227. {
  228. $this->formMapper->ifTrue(true);
  229. $this->formMapper->ifTrue(true);
  230. }
  231. /**
  232. * @expectedException RuntimeException
  233. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  234. */
  235. public function testIfFalseNested()
  236. {
  237. $this->formMapper->ifFalse(false);
  238. $this->formMapper->ifFalse(false);
  239. }
  240. /**
  241. * @expectedException RuntimeException
  242. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  243. */
  244. public function testIfCombinationNested()
  245. {
  246. $this->formMapper->ifTrue(true);
  247. $this->formMapper->ifFalse(false);
  248. }
  249. /**
  250. * @expectedException RuntimeException
  251. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  252. */
  253. public function testIfFalseCombinationNested2()
  254. {
  255. $this->formMapper->ifFalse(false);
  256. $this->formMapper->ifTrue(true);
  257. }
  258. /**
  259. * @expectedException RuntimeException
  260. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  261. */
  262. public function testIfFalseCombinationNested3()
  263. {
  264. $this->formMapper->ifFalse(true);
  265. $this->formMapper->ifTrue(false);
  266. }
  267. /**
  268. * @expectedException RuntimeException
  269. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  270. */
  271. public function testIfFalseCombinationNested4()
  272. {
  273. $this->formMapper->ifTrue(false);
  274. $this->formMapper->ifFalse(true);
  275. }
  276. private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
  277. {
  278. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  279. if ($name !== null) {
  280. $fieldDescription->setName($name);
  281. }
  282. if ($label !== null) {
  283. $fieldDescription->setOption('label', $label);
  284. }
  285. if ($translationDomain !== null) {
  286. $fieldDescription->setOption('translation_domain', $translationDomain);
  287. }
  288. return $fieldDescription;
  289. }
  290. }