ShowMapperTest.php 16 KB

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