ShowMapperTest.php 12 KB

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