ShowMapperTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. } else {
  104. $listShowFields[$name] = true;
  105. return false;
  106. }
  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. private function getFieldDescriptionMock($name = null, $label = null)
  329. {
  330. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  331. if ($name !== null) {
  332. $fieldDescription->setName($name);
  333. }
  334. if ($label !== null) {
  335. $fieldDescription->setOption('label', $label);
  336. }
  337. return $fieldDescription;
  338. }
  339. }