FormMapperTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. )), $this->admin->getFormTabs());
  72. $this->assertEquals(array('foobar' => array(
  73. 'collapsed' => false,
  74. 'class' => false,
  75. 'description' => false,
  76. 'translation_domain' => null,
  77. 'fields' => array (),
  78. 'name' => 'foobar'
  79. )), $this->admin->getFormGroups());
  80. }
  81. public function testWithOptions()
  82. {
  83. $this->formMapper->with('foobar', array(
  84. 'translation_domain' => 'Foobar',
  85. ));
  86. $this->assertEquals(array('foobar' => array(
  87. 'collapsed' => false,
  88. 'class' => false,
  89. 'description' => false,
  90. 'translation_domain' => 'Foobar',
  91. 'fields' => array (),
  92. 'name' => 'foobar'
  93. )), $this->admin->getFormGroups());
  94. $this->assertEquals(array('default' => array (
  95. 'collapsed' => false,
  96. 'class' => false,
  97. 'description' => false,
  98. 'translation_domain' => 'Foobar',
  99. 'auto_created' => true,
  100. 'groups' => array('foobar'),
  101. 'tab' => true,
  102. 'name' => 'default'
  103. )), $this->admin->getFormTabs());
  104. }
  105. public function testWithFieldsCascadeTranslationDomain()
  106. {
  107. $this->contractor->expects($this->once())
  108. ->method('getDefaultOptions')
  109. ->will($this->returnValue(array()));
  110. $this->formMapper->with('foobar', array(
  111. 'translation_domain' => 'Foobar'
  112. ))
  113. ->add('foo', 'bar')
  114. ->end();
  115. $fieldDescription = $this->admin->getFormFieldDescription('foo');
  116. $this->assertEquals('foo', $fieldDescription->getName());
  117. $this->assertEquals('bar', $fieldDescription->getType());
  118. $this->assertEquals('Foobar', $fieldDescription->getTranslationDomain());
  119. $this->assertTrue($this->formMapper->has('foo'));
  120. $this->assertEquals(array('default' => array (
  121. 'collapsed' => false,
  122. 'class' => false,
  123. 'description' => false,
  124. 'translation_domain' => 'Foobar',
  125. 'auto_created' => true,
  126. 'groups' => array ('foobar'),
  127. 'tab' => true,
  128. 'name' => 'default'
  129. )), $this->admin->getFormTabs());
  130. $this->assertEquals(array('foobar' => array(
  131. 'collapsed' => false,
  132. 'class' => false,
  133. 'description' => false,
  134. 'translation_domain' => 'Foobar',
  135. 'fields' => array(
  136. 'foo' => 'foo'
  137. ),
  138. 'name' => 'foobar'
  139. )), $this->admin->getFormGroups());
  140. }
  141. public function testRemoveCascadeRemoveFieldFromFormGroup()
  142. {
  143. $this->formMapper->with('foo');
  144. $this->formMapper->remove('foo');
  145. }
  146. public function testIfTrueApply()
  147. {
  148. $this->contractor->expects($this->once())
  149. ->method('getDefaultOptions')
  150. ->will($this->returnValue(array()));
  151. $this->formMapper
  152. ->ifTrue(true)
  153. ->add('foo', 'bar')
  154. ->ifEnd()
  155. ;
  156. $this->assertTrue($this->formMapper->has('foo'));
  157. }
  158. public function testIfTrueNotApply()
  159. {
  160. $this->formMapper
  161. ->ifTrue(false)
  162. ->add('foo', 'bar')
  163. ->ifEnd()
  164. ;
  165. $this->assertFalse($this->formMapper->has('foo'));
  166. }
  167. public function testIfTrueCombination()
  168. {
  169. $this->contractor->expects($this->once())
  170. ->method('getDefaultOptions')
  171. ->will($this->returnValue(array()));
  172. $this->formMapper
  173. ->ifTrue(false)
  174. ->add('foo', 'bar')
  175. ->ifEnd()
  176. ->add('baz', 'foobaz')
  177. ;
  178. $this->assertFalse($this->formMapper->has('foo'));
  179. $this->assertTrue($this->formMapper->has('baz'));
  180. }
  181. public function testIfFalseApply()
  182. {
  183. $this->contractor->expects($this->once())
  184. ->method('getDefaultOptions')
  185. ->will($this->returnValue(array()));
  186. $this->formMapper
  187. ->ifFalse(false)
  188. ->add('foo', 'bar')
  189. ->ifEnd()
  190. ;
  191. $this->assertTrue($this->formMapper->has('foo'));
  192. }
  193. public function testIfFalseNotApply()
  194. {
  195. $this->formMapper
  196. ->ifFalse(true)
  197. ->add('foo', 'bar')
  198. ->ifEnd()
  199. ;
  200. $this->assertFalse($this->formMapper->has('foo'));
  201. }
  202. public function testIfFalseCombination()
  203. {
  204. $this->contractor->expects($this->once())
  205. ->method('getDefaultOptions')
  206. ->will($this->returnValue(array()));
  207. $this->formMapper
  208. ->ifFalse(true)
  209. ->add('foo', 'bar')
  210. ->ifEnd()
  211. ->add('baz', 'foobaz')
  212. ;
  213. $this->assertFalse($this->formMapper->has('foo'));
  214. $this->assertTrue($this->formMapper->has('baz'));
  215. }
  216. /**
  217. * @expectedException RuntimeException
  218. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  219. */
  220. public function testIfTrueNested()
  221. {
  222. $this->formMapper->ifTrue(true);
  223. $this->formMapper->ifTrue(true);
  224. }
  225. /**
  226. * @expectedException RuntimeException
  227. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  228. */
  229. public function testIfFalseNested()
  230. {
  231. $this->formMapper->ifFalse(false);
  232. $this->formMapper->ifFalse(false);
  233. }
  234. /**
  235. * @expectedException RuntimeException
  236. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  237. */
  238. public function testIfCombinationNested()
  239. {
  240. $this->formMapper->ifTrue(true);
  241. $this->formMapper->ifFalse(false);
  242. }
  243. /**
  244. * @expectedException RuntimeException
  245. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  246. */
  247. public function testIfFalseCombinationNested2()
  248. {
  249. $this->formMapper->ifFalse(false);
  250. $this->formMapper->ifTrue(true);
  251. }
  252. /**
  253. * @expectedException RuntimeException
  254. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  255. */
  256. public function testIfFalseCombinationNested3()
  257. {
  258. $this->formMapper->ifFalse(true);
  259. $this->formMapper->ifTrue(false);
  260. }
  261. /**
  262. * @expectedException RuntimeException
  263. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  264. */
  265. public function testIfFalseCombinationNested4()
  266. {
  267. $this->formMapper->ifTrue(false);
  268. $this->formMapper->ifFalse(true);
  269. }
  270. private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
  271. {
  272. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  273. if ($name !== null) {
  274. $fieldDescription->setName($name);
  275. }
  276. if ($label !== null) {
  277. $fieldDescription->setOption('label', $label);
  278. }
  279. if ($translationDomain !== null) {
  280. $fieldDescription->setOption('translation_domain', $translationDomain);
  281. }
  282. return $fieldDescription;
  283. }
  284. }