ShowMapperTest.php 13 KB

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