FormMapperTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  14. use Symfony\Component\Form\FormBuilder;
  15. class FormMapperTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var \Sonata\AdminBundle\Builder\FormContractorInterface
  19. */
  20. protected $contractor;
  21. /**
  22. * @var \Sonata\AdminBundle\Admin\AdminInterface
  23. */
  24. protected $admin;
  25. /**
  26. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  27. */
  28. protected $modelManager;
  29. /**
  30. * @var FormMapper
  31. */
  32. protected $formMapper;
  33. public function setUp()
  34. {
  35. $this->contractor = $this->getMockForAbstractClass('Sonata\AdminBundle\Builder\FormContractorInterface');
  36. $formFactory = $this->getMockForAbstractClass('Symfony\Component\Form\FormFactoryInterface');
  37. $eventDispatcher = $this->getMockForAbstractClass('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  38. $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
  39. $this->admin = new CleanAdmin('code', 'class', 'controller');
  40. $this->modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  41. // php 5.3 BC
  42. $fieldDescription = $this->getFieldDescriptionMock();
  43. $this->modelManager->expects($this->any())
  44. ->method('getNewFieldDescriptionInstance')
  45. ->will($this->returnCallback(function ($class, $name, array $options = array()) use ($fieldDescription) {
  46. $fieldDescriptionClone = clone $fieldDescription;
  47. $fieldDescriptionClone->setName($name);
  48. $fieldDescriptionClone->setOptions($options);
  49. return $fieldDescriptionClone;
  50. }));
  51. $this->admin->setModelManager($this->modelManager);
  52. $labelTranslatorStrategy = $this->getMockForAbstractClass('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  53. $this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
  54. $this->formMapper = new FormMapper(
  55. $this->contractor,
  56. $formBuilder,
  57. $this->admin
  58. );
  59. }
  60. public function testWithNoOptions()
  61. {
  62. $this->formMapper->with('foobar');
  63. $this->assertSame(array('default' => array(
  64. 'collapsed' => false,
  65. 'class' => false,
  66. 'description' => false,
  67. 'translation_domain' => null,
  68. 'name' => 'default',
  69. 'box_class' => 'box box-primary',
  70. 'auto_created' => true,
  71. 'groups' => array('foobar'),
  72. 'tab' => true,
  73. )), $this->admin->getFormTabs());
  74. $this->assertSame(array('foobar' => array(
  75. 'collapsed' => false,
  76. 'class' => false,
  77. 'description' => false,
  78. 'translation_domain' => null,
  79. 'name' => 'foobar',
  80. 'box_class' => 'box box-primary',
  81. 'fields' => array(),
  82. )), $this->admin->getFormGroups());
  83. }
  84. public function testWithOptions()
  85. {
  86. $this->formMapper->with('foobar', array(
  87. 'translation_domain' => 'Foobar',
  88. ));
  89. $this->assertSame(array('foobar' => array(
  90. 'collapsed' => false,
  91. 'class' => false,
  92. 'description' => false,
  93. 'translation_domain' => 'Foobar',
  94. 'name' => 'foobar',
  95. 'box_class' => 'box box-primary',
  96. 'fields' => array(),
  97. )), $this->admin->getFormGroups());
  98. $this->assertSame(array('default' => array(
  99. 'collapsed' => false,
  100. 'class' => false,
  101. 'description' => false,
  102. 'translation_domain' => 'Foobar',
  103. 'name' => 'default',
  104. 'box_class' => 'box box-primary',
  105. 'auto_created' => true,
  106. 'groups' => array('foobar'),
  107. 'tab' => true,
  108. )), $this->admin->getFormTabs());
  109. }
  110. public function testWithFieldsCascadeTranslationDomain()
  111. {
  112. $this->contractor->expects($this->once())
  113. ->method('getDefaultOptions')
  114. ->will($this->returnValue(array()));
  115. $this->formMapper->with('foobar', array(
  116. 'translation_domain' => 'Foobar',
  117. ))
  118. ->add('foo', 'bar')
  119. ->end();
  120. $fieldDescription = $this->admin->getFormFieldDescription('foo');
  121. $this->assertSame('foo', $fieldDescription->getName());
  122. $this->assertSame('bar', $fieldDescription->getType());
  123. $this->assertSame('Foobar', $fieldDescription->getTranslationDomain());
  124. $this->assertTrue($this->formMapper->has('foo'));
  125. $this->assertSame(array('default' => array(
  126. 'collapsed' => false,
  127. 'class' => false,
  128. 'description' => false,
  129. 'translation_domain' => 'Foobar',
  130. 'name' => 'default',
  131. 'box_class' => 'box box-primary',
  132. 'auto_created' => true,
  133. 'groups' => array('foobar'),
  134. 'tab' => true,
  135. )), $this->admin->getFormTabs());
  136. $this->assertSame(array('foobar' => array(
  137. 'collapsed' => false,
  138. 'class' => false,
  139. 'description' => false,
  140. 'translation_domain' => 'Foobar',
  141. 'name' => 'foobar',
  142. 'box_class' => 'box box-primary',
  143. 'fields' => array(
  144. 'foo' => 'foo',
  145. ),
  146. )), $this->admin->getFormGroups());
  147. }
  148. public function testRemoveCascadeRemoveFieldFromFormGroup()
  149. {
  150. $this->formMapper->with('foo');
  151. $this->formMapper->remove('foo');
  152. }
  153. public function testIfTrueApply()
  154. {
  155. $this->contractor->expects($this->once())
  156. ->method('getDefaultOptions')
  157. ->will($this->returnValue(array()));
  158. $this->formMapper
  159. ->ifTrue(true)
  160. ->add('foo', 'bar')
  161. ->ifEnd()
  162. ;
  163. $this->assertTrue($this->formMapper->has('foo'));
  164. }
  165. public function testIfTrueNotApply()
  166. {
  167. $this->formMapper
  168. ->ifTrue(false)
  169. ->add('foo', 'bar')
  170. ->ifEnd()
  171. ;
  172. $this->assertFalse($this->formMapper->has('foo'));
  173. }
  174. public function testIfTrueCombination()
  175. {
  176. $this->contractor->expects($this->once())
  177. ->method('getDefaultOptions')
  178. ->will($this->returnValue(array()));
  179. $this->formMapper
  180. ->ifTrue(false)
  181. ->add('foo', 'bar')
  182. ->ifEnd()
  183. ->add('baz', 'foobaz')
  184. ;
  185. $this->assertFalse($this->formMapper->has('foo'));
  186. $this->assertTrue($this->formMapper->has('baz'));
  187. }
  188. public function testIfFalseApply()
  189. {
  190. $this->contractor->expects($this->once())
  191. ->method('getDefaultOptions')
  192. ->will($this->returnValue(array()));
  193. $this->formMapper
  194. ->ifFalse(false)
  195. ->add('foo', 'bar')
  196. ->ifEnd()
  197. ;
  198. $this->assertTrue($this->formMapper->has('foo'));
  199. }
  200. public function testIfFalseNotApply()
  201. {
  202. $this->formMapper
  203. ->ifFalse(true)
  204. ->add('foo', 'bar')
  205. ->ifEnd()
  206. ;
  207. $this->assertFalse($this->formMapper->has('foo'));
  208. }
  209. public function testIfFalseCombination()
  210. {
  211. $this->contractor->expects($this->once())
  212. ->method('getDefaultOptions')
  213. ->will($this->returnValue(array()));
  214. $this->formMapper
  215. ->ifFalse(true)
  216. ->add('foo', 'bar')
  217. ->ifEnd()
  218. ->add('baz', 'foobaz')
  219. ;
  220. $this->assertFalse($this->formMapper->has('foo'));
  221. $this->assertTrue($this->formMapper->has('baz'));
  222. }
  223. /**
  224. * @expectedException \RuntimeException
  225. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  226. */
  227. public function testIfTrueNested()
  228. {
  229. $this->formMapper->ifTrue(true);
  230. $this->formMapper->ifTrue(true);
  231. }
  232. /**
  233. * @expectedException \RuntimeException
  234. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  235. */
  236. public function testIfFalseNested()
  237. {
  238. $this->formMapper->ifFalse(false);
  239. $this->formMapper->ifFalse(false);
  240. }
  241. /**
  242. * @expectedException \RuntimeException
  243. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  244. */
  245. public function testIfCombinationNested()
  246. {
  247. $this->formMapper->ifTrue(true);
  248. $this->formMapper->ifFalse(false);
  249. }
  250. /**
  251. * @expectedException \RuntimeException
  252. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  253. */
  254. public function testIfFalseCombinationNested2()
  255. {
  256. $this->formMapper->ifFalse(false);
  257. $this->formMapper->ifTrue(true);
  258. }
  259. /**
  260. * @expectedException \RuntimeException
  261. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  262. */
  263. public function testIfFalseCombinationNested3()
  264. {
  265. $this->formMapper->ifFalse(true);
  266. $this->formMapper->ifTrue(false);
  267. }
  268. /**
  269. * @expectedException \RuntimeException
  270. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  271. */
  272. public function testIfFalseCombinationNested4()
  273. {
  274. $this->formMapper->ifTrue(false);
  275. $this->formMapper->ifFalse(true);
  276. }
  277. public function testAddAcceptFormBuilder()
  278. {
  279. $formBuilder = $this
  280. ->getMockBuilder('Symfony\Component\Form\FormBuilder')
  281. ->disableOriginalConstructor()
  282. ->getMock();
  283. $formBuilder->expects($this->any())
  284. ->method('getName')
  285. ->will($this->returnValue('foo'));
  286. $formType = $this
  287. ->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')
  288. ->getMock();
  289. $innerType = $this
  290. ->getMockBuilder('Symfony\Component\Form\Extension\Core\Type\FormType')
  291. ->getMock();
  292. $formType->expects($this->once())
  293. ->method('getInnerType')
  294. ->will($this->returnValue($innerType));
  295. $formBuilder->expects($this->once())
  296. ->method('getType')
  297. ->will($this->returnValue($formType));
  298. $this->formMapper->add($formBuilder);
  299. $this->assertSame($this->formMapper->get('foo'), $formBuilder);
  300. }
  301. public function testAddFormBuilderWithType()
  302. {
  303. $formBuilder = $this
  304. ->getMockBuilder('Symfony\Component\Form\FormBuilder')
  305. ->disableOriginalConstructor()
  306. ->getMock();
  307. $formBuilder->expects($this->any())
  308. ->method('getName')
  309. ->will($this->returnValue('foo'));
  310. $formBuilder->expects($this->never())
  311. ->method('getType');
  312. $this->formMapper->add($formBuilder, 'Symfony\Component\Form\Extension\Core\Type\FormType');
  313. $this->assertSame($this->formMapper->get('foo'), $formBuilder);
  314. }
  315. public function testGroupRemovingWithoutTab()
  316. {
  317. $this->formMapper->with('foobar');
  318. $this->formMapper->removeGroup('foobar');
  319. $this->assertSame(array(), $this->admin->getFormGroups());
  320. }
  321. public function testGroupRemovingWithTab()
  322. {
  323. $this->formMapper->tab('mytab')->with('foobar');
  324. $this->formMapper->removeGroup('foobar', 'mytab');
  325. $this->assertSame(array(), $this->admin->getFormGroups());
  326. }
  327. public function testGroupRemovingWithoutTabAndWithTabRemoving()
  328. {
  329. $this->formMapper->with('foobar');
  330. $this->formMapper->removeGroup('foobar', 'default', true);
  331. $this->assertSame(array(), $this->admin->getFormGroups());
  332. $this->assertSame(array(), $this->admin->getFormTabs());
  333. }
  334. public function testGroupRemovingWithTabAndWithTabRemoving()
  335. {
  336. $this->formMapper->tab('mytab')->with('foobar');
  337. $this->formMapper->removeGroup('foobar', 'mytab', true);
  338. $this->assertSame(array(), $this->admin->getFormGroups());
  339. $this->assertSame(array(), $this->admin->getFormTabs());
  340. }
  341. public function testKeys()
  342. {
  343. $this->contractor->expects($this->any())
  344. ->method('getDefaultOptions')
  345. ->will($this->returnValue(array()));
  346. $this->formMapper
  347. ->add('foo', 'bar')
  348. ->add('baz', 'foobaz')
  349. ;
  350. $this->assertSame(array('foo', 'baz'), $this->formMapper->keys());
  351. }
  352. public function testFieldNameIsSanitized()
  353. {
  354. $this->contractor->expects($this->any())
  355. ->method('getDefaultOptions')
  356. ->will($this->returnValue(array()));
  357. $this->formMapper
  358. ->add('fo.o', 'bar')
  359. ->add('ba__z', 'foobaz')
  360. ;
  361. $this->assertSame(array('fo__o', 'ba____z'), $this->formMapper->keys());
  362. }
  363. private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
  364. {
  365. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  366. if ($name !== null) {
  367. $fieldDescription->setName($name);
  368. }
  369. if ($label !== null) {
  370. $fieldDescription->setOption('label', $label);
  371. }
  372. if ($translationDomain !== null) {
  373. $fieldDescription->setOption('translation_domain', $translationDomain);
  374. }
  375. return $fieldDescription;
  376. }
  377. }