PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/admin-bundle/Tests/Show/ShowMapperTest.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 412 lines | 274 code | 80 blank | 58 comment | 4 complexity | 43c3b8dfdebfc2b1cfa45e18cb6916a1 MD5 | raw file
  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. /**
  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. } else {
  105. $listShowFields[$name] = true;
  106. return false;
  107. }
  108. }));
  109. $this->showBuilder->expects($this->any())
  110. ->method('addField')
  111. ->will($this->returnCallback(function ($list, $type, $fieldDescription, $admin) {
  112. $list->add($fieldDescription);
  113. }));
  114. $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
  115. }
  116. public function testFluidInterface()
  117. {
  118. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  119. $this->assertSame($this->showMapper, $this->showMapper->add($fieldDescription));
  120. $this->assertSame($this->showMapper, $this->showMapper->remove('fooName'));
  121. $this->assertSame($this->showMapper, $this->showMapper->reorder(array()));
  122. }
  123. public function testGet()
  124. {
  125. $this->assertFalse($this->showMapper->has('fooName'));
  126. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  127. $this->showMapper->add($fieldDescription);
  128. $this->assertSame($fieldDescription, $this->showMapper->get('fooName'));
  129. }
  130. public function testAdd()
  131. {
  132. $this->showMapper->add('fooName');
  133. $this->assertTrue($this->showMapper->has('fooName'));
  134. $fieldDescription = $this->showMapper->get('fooName');
  135. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  136. $this->assertSame('fooName', $fieldDescription->getName());
  137. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  138. }
  139. public function testIfTrueApply()
  140. {
  141. $this->showMapper->ifTrue(true);
  142. $this->showMapper->add('fooName');
  143. $this->showMapper->ifEnd();
  144. $this->assertTrue($this->showMapper->has('fooName'));
  145. $fieldDescription = $this->showMapper->get('fooName');
  146. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  147. $this->assertSame('fooName', $fieldDescription->getName());
  148. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  149. }
  150. public function testIfTrueNotApply()
  151. {
  152. $this->showMapper->ifTrue(false);
  153. $this->showMapper->add('fooName');
  154. $this->showMapper->ifEnd();
  155. $this->assertFalse($this->showMapper->has('fooName'));
  156. }
  157. public function testIfTrueCombination()
  158. {
  159. $this->showMapper->ifTrue(false);
  160. $this->showMapper->add('fooName');
  161. $this->showMapper->ifEnd();
  162. $this->showMapper->add('barName');
  163. $this->assertFalse($this->showMapper->has('fooName'));
  164. $this->assertTrue($this->showMapper->has('barName'));
  165. $fieldDescription = $this->showMapper->get('barName');
  166. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  167. $this->assertSame('barName', $fieldDescription->getName());
  168. $this->assertSame('barName', $fieldDescription->getOption('label'));
  169. }
  170. public function testIfFalseApply()
  171. {
  172. $this->showMapper->ifFalse(false);
  173. $this->showMapper->add('fooName');
  174. $this->showMapper->ifEnd();
  175. $this->assertTrue($this->showMapper->has('fooName'));
  176. $fieldDescription = $this->showMapper->get('fooName');
  177. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  178. $this->assertSame('fooName', $fieldDescription->getName());
  179. $this->assertSame('fooName', $fieldDescription->getOption('label'));
  180. }
  181. public function testIfFalseNotApply()
  182. {
  183. $this->showMapper->ifFalse(true);
  184. $this->showMapper->add('fooName');
  185. $this->showMapper->ifEnd();
  186. $this->assertFalse($this->showMapper->has('fooName'));
  187. }
  188. public function testIfFalseCombination()
  189. {
  190. $this->showMapper->ifFalse(true);
  191. $this->showMapper->add('fooName');
  192. $this->showMapper->ifEnd();
  193. $this->showMapper->add('barName');
  194. $this->assertFalse($this->showMapper->has('fooName'));
  195. $this->assertTrue($this->showMapper->has('barName'));
  196. $fieldDescription = $this->showMapper->get('barName');
  197. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  198. $this->assertSame('barName', $fieldDescription->getName());
  199. $this->assertSame('barName', $fieldDescription->getOption('label'));
  200. }
  201. /**
  202. * @expectedException RuntimeException
  203. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  204. */
  205. public function testIfTrueNested()
  206. {
  207. $this->showMapper->ifTrue(true);
  208. $this->showMapper->ifTrue(true);
  209. }
  210. /**
  211. * @expectedException RuntimeException
  212. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  213. */
  214. public function testIfFalseNested()
  215. {
  216. $this->showMapper->ifFalse(false);
  217. $this->showMapper->ifFalse(false);
  218. }
  219. /**
  220. * @expectedException RuntimeException
  221. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  222. */
  223. public function testIfCombinationNested()
  224. {
  225. $this->showMapper->ifTrue(true);
  226. $this->showMapper->ifFalse(false);
  227. }
  228. /**
  229. * @expectedException RuntimeException
  230. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  231. */
  232. public function testIfFalseCombinationNested2()
  233. {
  234. $this->showMapper->ifFalse(false);
  235. $this->showMapper->ifTrue(true);
  236. }
  237. /**
  238. * @expectedException RuntimeException
  239. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  240. */
  241. public function testIfFalseCombinationNested3()
  242. {
  243. $this->showMapper->ifFalse(true);
  244. $this->showMapper->ifTrue(false);
  245. }
  246. /**
  247. * @expectedException RuntimeException
  248. * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
  249. */
  250. public function testIfFalseCombinationNested4()
  251. {
  252. $this->showMapper->ifTrue(false);
  253. $this->showMapper->ifFalse(true);
  254. }
  255. public function testAddRemove()
  256. {
  257. $this->assertFalse($this->showMapper->has('fooName'));
  258. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  259. $this->showMapper->add($fieldDescription);
  260. $this->assertTrue($this->showMapper->has('fooName'));
  261. $this->showMapper->remove('fooName');
  262. $this->assertFalse($this->showMapper->has('fooName'));
  263. }
  264. public function testAddException()
  265. {
  266. try {
  267. $this->showMapper->add(12345);
  268. } catch (\RuntimeException $e) {
  269. $this->assertContains('invalid state', $e->getMessage());
  270. return;
  271. }
  272. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  273. }
  274. public function testAddDuplicateFieldNameException()
  275. {
  276. $name = 'name';
  277. try {
  278. $this->showMapper->add($name);
  279. $this->showMapper->add($name);
  280. } catch (\RuntimeException $e) {
  281. $this->assertContains(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name), $e->getMessage());
  282. return;
  283. }
  284. $this->fail('Failed asserting that duplicate field name exception of type "\RuntimeException" is thrown.');
  285. }
  286. public function testReorder()
  287. {
  288. $this->assertSame(array(), $this->admin->getShowGroups());
  289. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  290. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  291. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  292. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  293. $this->showMapper->with('Group1');
  294. $this->showMapper->add($fieldDescription1);
  295. $this->showMapper->add($fieldDescription2);
  296. $this->showMapper->add($fieldDescription3);
  297. $this->showMapper->add($fieldDescription4);
  298. $this->assertSame(array(
  299. 'Group1' => array(
  300. 'collapsed' => false,
  301. 'class' => false,
  302. 'description' => false,
  303. 'translation_domain' => null,
  304. 'name' => 'Group1',
  305. 'box_class' => 'box box-primary',
  306. 'fields' => array('fooName1' => 'fooName1', 'fooName2' => 'fooName2', 'fooName3' => 'fooName3', 'fooName4' => 'fooName4'),
  307. ), ), $this->admin->getShowGroups());
  308. $this->showMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  309. // print_r is used to compare order of items in associative arrays
  310. $this->assertSame(print_r(array(
  311. 'Group1' => array(
  312. 'collapsed' => false,
  313. 'class' => false,
  314. 'description' => false,
  315. 'translation_domain' => null,
  316. 'name' => 'Group1',
  317. 'box_class' => 'box box-primary',
  318. 'fields' => array('fooName3' => 'fooName3', 'fooName2' => 'fooName2', 'fooName1' => 'fooName1', 'fooName4' => 'fooName4'),
  319. ), ), true), print_r($this->admin->getShowGroups(), true));
  320. }
  321. private function getFieldDescriptionMock($name = null, $label = null)
  322. {
  323. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  324. if ($name !== null) {
  325. $fieldDescription->setName($name);
  326. }
  327. if ($label !== null) {
  328. $fieldDescription->setOption('label', $label);
  329. }
  330. return $fieldDescription;
  331. }
  332. }