FormMapperTest.php 9.9 KB

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