FormMapperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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->assertSame(array('default' => array(
  63. 'collapsed' => false,
  64. 'class' => false,
  65. 'description' => false,
  66. 'translation_domain' => null,
  67. 'name' => 'default',
  68. 'box_class' => 'box box-primary',
  69. 'auto_created' => true,
  70. 'groups' => array('foobar'),
  71. 'tab' => true,
  72. )), $this->admin->getFormTabs());
  73. $this->assertSame(array('foobar' => array(
  74. 'collapsed' => false,
  75. 'class' => false,
  76. 'description' => false,
  77. 'translation_domain' => null,
  78. 'name' => 'foobar',
  79. 'box_class' => 'box box-primary',
  80. 'fields' => array(),
  81. )), $this->admin->getFormGroups());
  82. }
  83. public function testWithOptions()
  84. {
  85. $this->formMapper->with('foobar', array(
  86. 'translation_domain' => 'Foobar',
  87. ));
  88. $this->assertSame(array('foobar' => array(
  89. 'collapsed' => false,
  90. 'class' => false,
  91. 'description' => false,
  92. 'translation_domain' => 'Foobar',
  93. 'name' => 'foobar',
  94. 'box_class' => 'box box-primary',
  95. 'fields' => array(),
  96. )), $this->admin->getFormGroups());
  97. $this->assertSame(array('default' => array(
  98. 'collapsed' => false,
  99. 'class' => false,
  100. 'description' => false,
  101. 'translation_domain' => 'Foobar',
  102. 'name' => 'default',
  103. 'box_class' => 'box box-primary',
  104. 'auto_created' => true,
  105. 'groups' => array('foobar'),
  106. 'tab' => true,
  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->assertSame('foo', $fieldDescription->getName());
  121. $this->assertSame('bar', $fieldDescription->getType());
  122. $this->assertSame('Foobar', $fieldDescription->getTranslationDomain());
  123. $this->assertTrue($this->formMapper->has('foo'));
  124. $this->assertSame(array('default' => array(
  125. 'collapsed' => false,
  126. 'class' => false,
  127. 'description' => false,
  128. 'translation_domain' => 'Foobar',
  129. 'name' => 'default',
  130. 'box_class' => 'box box-primary',
  131. 'auto_created' => true,
  132. 'groups' => array('foobar'),
  133. 'tab' => true,
  134. )), $this->admin->getFormTabs());
  135. $this->assertSame(array('foobar' => array(
  136. 'collapsed' => false,
  137. 'class' => false,
  138. 'description' => false,
  139. 'translation_domain' => 'Foobar',
  140. 'name' => 'foobar',
  141. 'box_class' => 'box box-primary',
  142. 'fields' => array(
  143. 'foo' => 'foo',
  144. ),
  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. public function testAddAcceptFormBuilder()
  277. {
  278. $formBuilder = $this
  279. ->getMockBuilder('Symfony\Component\Form\FormBuilder')
  280. ->disableOriginalConstructor()
  281. ->getMock();
  282. $formBuilder->expects($this->any())
  283. ->method('getName')
  284. ->will($this->returnValue('foo'));
  285. $this->formMapper->add($formBuilder);
  286. $this->assertSame($this->formMapper->get('foo'), $formBuilder);
  287. }
  288. private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
  289. {
  290. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  291. if ($name !== null) {
  292. $fieldDescription->setName($name);
  293. }
  294. if ($label !== null) {
  295. $fieldDescription->setOption('label', $label);
  296. }
  297. if ($translationDomain !== null) {
  298. $fieldDescription->setOption('translation_domain', $translationDomain);
  299. }
  300. return $fieldDescription;
  301. }
  302. public function testGroupRemovingWithoutTab()
  303. {
  304. $this->formMapper->with('foobar');
  305. $this->formMapper->removeGroup('foobar');
  306. $this->assertSame(array(), $this->admin->getFormGroups());
  307. }
  308. public function testGroupRemovingWithTab()
  309. {
  310. $this->formMapper->tab('mytab')->with('foobar');
  311. $this->formMapper->removeGroup('foobar', 'mytab');
  312. $this->assertSame(array(), $this->admin->getFormGroups());
  313. }
  314. public function testGroupRemovingWithoutTabAndWithTabRemoving()
  315. {
  316. $this->formMapper->with('foobar');
  317. $this->formMapper->removeGroup('foobar', 'default', true);
  318. $this->assertSame(array(), $this->admin->getFormGroups());
  319. $this->assertSame(array(), $this->admin->getFormTabs());
  320. }
  321. public function testGroupRemovingWithTabAndWithTabRemoving()
  322. {
  323. $this->formMapper->tab('mytab')->with('foobar');
  324. $this->formMapper->removeGroup('foobar', 'mytab', true);
  325. $this->assertSame(array(), $this->admin->getFormGroups());
  326. $this->assertSame(array(), $this->admin->getFormTabs());
  327. }
  328. }