ShowMapperTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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\Show;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  13. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  14. use Sonata\AdminBundle\Show\ShowMapper;
  15. use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
  16. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  17. use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
  18. /**
  19. * Test for ShowMapper.
  20. *
  21. * @author Andrej Hudec <pulzarraider@gmail.com>
  22. */
  23. class ShowMapperTest extends PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * @var ShowMapper
  27. */
  28. private $showMapper;
  29. /**
  30. * @var AdminInterface
  31. */
  32. private $admin;
  33. /**
  34. * @var ShowBuilderInterface
  35. */
  36. private $showBuilder;
  37. /**
  38. * @var FieldDescriptionCollection
  39. */
  40. private $fieldDescriptionCollection;
  41. /**
  42. * @var array
  43. */
  44. private $groups;
  45. /**
  46. * @var array
  47. */
  48. private $listShowFields;
  49. public function setUp()
  50. {
  51. $this->showBuilder = $this->getMockForAbstractClass('Sonata\AdminBundle\Builder\ShowBuilderInterface');
  52. $this->fieldDescriptionCollection = new FieldDescriptionCollection();
  53. $this->admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
  54. $this->admin->expects($this->any())
  55. ->method('getLabel')
  56. ->will($this->returnValue('AdminLabel'));
  57. $this->admin->expects($this->any())
  58. ->method('getShowTabs')
  59. ->will($this->returnValue(array()));
  60. $this->groups = array();
  61. $this->listShowFields = array();
  62. // php 5.3 BC
  63. $groups = &$this->groups;
  64. $listShowFields = &$this->listShowFields;
  65. $this->admin->expects($this->any())
  66. ->method('getShowGroups')
  67. ->will($this->returnCallback(function () use (&$groups) {
  68. return $groups;
  69. }));
  70. $this->admin->expects($this->any())
  71. ->method('setShowGroups')
  72. ->will($this->returnCallback(function ($showGroups) use (&$groups) {
  73. $groups = $showGroups;
  74. }));
  75. $this->admin->expects($this->any())
  76. ->method('reorderShowGroup')
  77. ->will($this->returnCallback(function ($group, $keys) use (&$groups) {
  78. $showGroups = $groups;
  79. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  80. $groups = $showGroups;
  81. }));
  82. $modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  83. // php 5.3 BC
  84. $fieldDescription = $this->getFieldDescriptionMock();
  85. $modelManager->expects($this->any())
  86. ->method('getNewFieldDescriptionInstance')
  87. ->will($this->returnCallback(function ($class, $name, array $options = array()) use ($fieldDescription) {
  88. $fieldDescriptionClone = clone $fieldDescription;
  89. $fieldDescriptionClone->setName($name);
  90. $fieldDescriptionClone->setOptions($options);
  91. return $fieldDescriptionClone;
  92. }));
  93. $this->admin->expects($this->any())
  94. ->method('getModelManager')
  95. ->will($this->returnValue($modelManager));
  96. $labelTranslatorStrategy = new NoopLabelTranslatorStrategy();
  97. $this->admin->expects($this->any())
  98. ->method('getLabelTranslatorStrategy')
  99. ->will($this->returnValue($labelTranslatorStrategy));
  100. $this->admin->expects($this->any())
  101. ->method('hasShowFieldDescription')
  102. ->will($this->returnCallback(function ($name) use (&$listShowFields) {
  103. if (isset($listShowFields[$name])) {
  104. return true;
  105. }
  106. $listShowFields[$name] = true;
  107. return false;
  108. }));
  109. $this->showBuilder->expects($this->any())
  110. ->method('addField')
  111. ->will($this->returnCallback(function ($list, $type, $fieldDescription, $admin) {
  112. $list->add($fieldDescription);
  113. }));
  114. $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
  115. }
  116. public function testFluidInterface()
  117. {
  118. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  119. $this->assertSame($this->showMapper, $this->showMapper->add($fieldDescription));
  120. $this->assertSame($this->showMapper, $this->showMapper->remove('fooName'));
  121. $this->assertSame($this->showMapper, $this->showMapper->reorder(array()));
  122. }
  123. public function testGet()
  124. {
  125. $this->assertFalse($this->showMapper->has('fooName'));
  126. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  127. $this->showMapper->add($fieldDescription);
  128. $this->assertSame($fieldDescription, $this->showMapper->get('fooName'));
  129. }
  130. public function testAdd()
  131. {
  132. $this->showMapper->add('fooName');
  133. $this->assertTrue($this->showMapper->has('fooName'));
  134. $fieldDescription = $this->showMapper->get('fooName');
  135. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  136. $this->assertSame('fooName', $fieldDescription->getName());
  137. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  138. }
  139. public function testIfTrueApply()
  140. {
  141. $this->showMapper->ifTrue(true);
  142. $this->showMapper->add('fooName');
  143. $this->showMapper->ifEnd();
  144. $this->assertTrue($this->showMapper->has('fooName'));
  145. $fieldDescription = $this->showMapper->get('fooName');
  146. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  147. $this->assertSame('fooName', $fieldDescription->getName());
  148. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  149. }
  150. public function testIfTrueNotApply()
  151. {
  152. $this->showMapper->ifTrue(false);
  153. $this->showMapper->add('fooName');
  154. $this->showMapper->ifEnd();
  155. $this->assertFalse($this->showMapper->has('fooName'));
  156. }
  157. public function testIfTrueCombination()
  158. {
  159. $this->showMapper->ifTrue(false);
  160. $this->showMapper->add('fooName');
  161. $this->showMapper->ifEnd();
  162. $this->showMapper->add('barName');
  163. $this->assertFalse($this->showMapper->has('fooName'));
  164. $this->assertTrue($this->showMapper->has('barName'));
  165. $fieldDescription = $this->showMapper->get('barName');
  166. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  167. $this->assertSame('barName', $fieldDescription->getName());
  168. $this->assertSame('barName', $fieldDescription->getOption('label'));
  169. }
  170. public function testIfFalseApply()
  171. {
  172. $this->showMapper->ifFalse(false);
  173. $this->showMapper->add('fooName');
  174. $this->showMapper->ifEnd();
  175. $this->assertTrue($this->showMapper->has('fooName'));
  176. $fieldDescription = $this->showMapper->get('fooName');
  177. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  178. $this->assertSame('fooName', $fieldDescription->getName());
  179. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  180. }
  181. public function testIfFalseNotApply()
  182. {
  183. $this->showMapper->ifFalse(true);
  184. $this->showMapper->add('fooName');
  185. $this->showMapper->ifEnd();
  186. $this->assertFalse($this->showMapper->has('fooName'));
  187. }
  188. public function testIfFalseCombination()
  189. {
  190. $this->showMapper->ifFalse(true);
  191. $this->showMapper->add('fooName');
  192. $this->showMapper->ifEnd();
  193. $this->showMapper->add('barName');
  194. $this->assertFalse($this->showMapper->has('fooName'));
  195. $this->assertTrue($this->showMapper->has('barName'));
  196. $fieldDescription = $this->showMapper->get('barName');
  197. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  198. $this->assertSame('barName', $fieldDescription->getName());
  199. $this->assertSame('barName', $fieldDescription->getOption('label'));
  200. }
  201. /**
  202. * @expectedException \RuntimeException
  203. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  204. */
  205. public function testIfTrueNested()
  206. {
  207. $this->showMapper->ifTrue(true);
  208. $this->showMapper->ifTrue(true);
  209. }
  210. /**
  211. * @expectedException \RuntimeException
  212. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  213. */
  214. public function testIfFalseNested()
  215. {
  216. $this->showMapper->ifFalse(false);
  217. $this->showMapper->ifFalse(false);
  218. }
  219. /**
  220. * @expectedException \RuntimeException
  221. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  222. */
  223. public function testIfCombinationNested()
  224. {
  225. $this->showMapper->ifTrue(true);
  226. $this->showMapper->ifFalse(false);
  227. }
  228. /**
  229. * @expectedException \RuntimeException
  230. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  231. */
  232. public function testIfFalseCombinationNested2()
  233. {
  234. $this->showMapper->ifFalse(false);
  235. $this->showMapper->ifTrue(true);
  236. }
  237. /**
  238. * @expectedException \RuntimeException
  239. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  240. */
  241. public function testIfFalseCombinationNested3()
  242. {
  243. $this->showMapper->ifFalse(true);
  244. $this->showMapper->ifTrue(false);
  245. }
  246. /**
  247. * @expectedException \RuntimeException
  248. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  249. */
  250. public function testIfFalseCombinationNested4()
  251. {
  252. $this->showMapper->ifTrue(false);
  253. $this->showMapper->ifFalse(true);
  254. }
  255. public function testAddRemove()
  256. {
  257. $this->assertFalse($this->showMapper->has('fooName'));
  258. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  259. $this->showMapper->add($fieldDescription);
  260. $this->assertTrue($this->showMapper->has('fooName'));
  261. $this->showMapper->remove('fooName');
  262. $this->assertFalse($this->showMapper->has('fooName'));
  263. }
  264. public function testAddException()
  265. {
  266. $this->expectException('\RuntimeException', 'invalid state');
  267. $this->showMapper->add(12345);
  268. }
  269. public function testAddDuplicateFieldNameException()
  270. {
  271. $name = 'name';
  272. $this->expectException('\RuntimeException', sprintf('Duplicate field %s "name" in show mapper. Names should be unique.', $name));
  273. $this->showMapper->add($name);
  274. $this->showMapper->add($name);
  275. }
  276. public function testKeys()
  277. {
  278. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  279. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  280. $this->showMapper->add($fieldDescription1);
  281. $this->showMapper->add($fieldDescription2);
  282. $this->assertSame(array('fooName1', 'fooName2'), $this->showMapper->keys());
  283. }
  284. public function testReorder()
  285. {
  286. $this->assertSame(array(), $this->admin->getShowGroups());
  287. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  288. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  289. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  290. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  291. $this->showMapper->with('Group1');
  292. $this->showMapper->add($fieldDescription1);
  293. $this->showMapper->add($fieldDescription2);
  294. $this->showMapper->add($fieldDescription3);
  295. $this->showMapper->add($fieldDescription4);
  296. $this->assertSame(array(
  297. 'Group1' => array(
  298. 'collapsed' => false,
  299. 'class' => false,
  300. 'description' => false,
  301. 'label' => 'Group1',
  302. 'translation_domain' => null,
  303. 'name' => 'Group1',
  304. 'box_class' => 'box box-primary',
  305. 'fields' => array('fooName1' => 'fooName1', 'fooName2' => 'fooName2', 'fooName3' => 'fooName3', 'fooName4' => 'fooName4'),
  306. ), ), $this->admin->getShowGroups());
  307. $this->showMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  308. // print_r is used to compare order of items in associative arrays
  309. $this->assertSame(print_r(array(
  310. 'Group1' => array(
  311. 'collapsed' => false,
  312. 'class' => false,
  313. 'description' => false,
  314. 'label' => 'Group1',
  315. 'translation_domain' => null,
  316. 'name' => 'Group1',
  317. 'box_class' => 'box box-primary',
  318. 'fields' => array('fooName3' => 'fooName3', 'fooName2' => 'fooName2', 'fooName1' => 'fooName1', 'fooName4' => 'fooName4'),
  319. ), ), true), print_r($this->admin->getShowGroups(), true));
  320. }
  321. public function testGroupRemovingWithoutTab()
  322. {
  323. $this->cleanShowMapper();
  324. $this->showMapper->with('groupfoo1');
  325. $this->showMapper->removeGroup('groupfoo1');
  326. $this->assertSame(array(), $this->admin->getShowGroups());
  327. }
  328. public function testGroupRemovingWithTab()
  329. {
  330. $this->cleanShowMapper();
  331. $this->showMapper->tab('mytab')->with('groupfoo2');
  332. $this->showMapper->removeGroup('groupfoo2', 'mytab');
  333. $this->assertSame(array(), $this->admin->getShowGroups());
  334. }
  335. public function testGroupRemovingWithoutTabAndWithTabRemoving()
  336. {
  337. $this->cleanShowMapper();
  338. $this->showMapper->with('groupfoo3');
  339. $this->showMapper->removeGroup('groupfoo3', 'default', true);
  340. $this->assertSame(array(), $this->admin->getShowGroups());
  341. $this->assertSame(array(), $this->admin->getShowTabs());
  342. }
  343. public function testGroupRemovingWithTabAndWithTabRemoving()
  344. {
  345. $this->cleanShowMapper();
  346. $this->showMapper->tab('mytab2')->with('groupfoo4');
  347. $this->showMapper->removeGroup('groupfoo4', 'mytab2', true);
  348. $this->assertSame(array(), $this->admin->getShowGroups());
  349. $this->assertSame(array(), $this->admin->getShowTabs());
  350. }
  351. public function testEmptyFieldLabel()
  352. {
  353. $this->showMapper->add('foo', null, array('label' => false));
  354. $this->assertFalse($this->showMapper->get('foo')->getOption('label'));
  355. }
  356. private function cleanShowMapper()
  357. {
  358. $this->showBuilder = $this->getMockForAbstractClass('Sonata\AdminBundle\Builder\ShowBuilderInterface');
  359. $this->fieldDescriptionCollection = new FieldDescriptionCollection();
  360. $this->admin = new CleanAdmin('code', 'class', 'controller');
  361. $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
  362. }
  363. private function getFieldDescriptionMock($name = null, $label = 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. return $fieldDescription;
  373. }
  374. }