ShowMapperTest.php 14 KB

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