ShowMapperTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. class TestAdmin extends Admin
  19. {
  20. }
  21. /**
  22. * Test for ShowMapper
  23. *
  24. * @author Andrej Hudec <pulzarraider@gmail.com>
  25. */
  26. class ShowMapperTest extends \PHPUnit_Framework_TestCase
  27. {
  28. /**
  29. * @var ShowMapper
  30. */
  31. private $showMapper;
  32. /**
  33. * @var AdminInterface
  34. */
  35. private $admin;
  36. /**
  37. * @var Sonata\AdminBundle\Builder\ShowBuilderInterface
  38. */
  39. private $showBuilder;
  40. /**
  41. * @var FieldDescriptionCollection
  42. */
  43. private $fieldDescriptionCollection;
  44. /**
  45. * @var array
  46. */
  47. private $groups;
  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. // php 5.3 BC
  61. $groups = & $this->groups;
  62. $this->admin->expects($this->any())
  63. ->method('getShowGroups')
  64. ->will($this->returnCallback(function() use (&$groups) {
  65. return $groups;
  66. }));
  67. $this->admin->expects($this->any())
  68. ->method('setShowGroups')
  69. ->will($this->returnCallback(function($showGroups) use (&$groups) {
  70. $groups = $showGroups;
  71. }));
  72. $this->admin->expects($this->any())
  73. ->method('reorderShowGroup')
  74. ->will($this->returnCallback(function($group, $keys) use (&$groups) {
  75. $showGroups = $groups;
  76. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  77. $groups = $showGroups;
  78. }));
  79. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  80. // php 5.3 BC
  81. $fieldDescription = $this->getFieldDescriptionMock();
  82. $modelManager->expects($this->any())
  83. ->method('getNewFieldDescriptionInstance')
  84. ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
  85. $fieldDescriptionClone = clone $fieldDescription;
  86. $fieldDescriptionClone->setName($name);
  87. $fieldDescriptionClone->setOptions($options);
  88. return $fieldDescriptionClone;
  89. }));
  90. $this->admin->expects($this->any())
  91. ->method('getModelManager')
  92. ->will($this->returnValue($modelManager));
  93. $labelTranslatorStrategy = new NoopLabelTranslatorStrategy;
  94. $this->admin->expects($this->any())
  95. ->method('getLabelTranslatorStrategy')
  96. ->will($this->returnValue($labelTranslatorStrategy));
  97. $this->showBuilder->expects($this->any())
  98. ->method('addField')
  99. ->will($this->returnCallback(function($list, $type, $fieldDescription, $admin) {
  100. $list->add($fieldDescription);
  101. }));
  102. $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
  103. }
  104. public function testFluidInterface()
  105. {
  106. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  107. $this->assertEquals($this->showMapper, $this->showMapper->add($fieldDescription));
  108. $this->assertEquals($this->showMapper, $this->showMapper->remove('fooName'));
  109. $this->assertEquals($this->showMapper, $this->showMapper->reorder(array()));
  110. }
  111. public function testGet()
  112. {
  113. $this->assertFalse($this->showMapper->has('fooName'));
  114. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  115. $this->showMapper->add($fieldDescription);
  116. $this->assertEquals($fieldDescription, $this->showMapper->get('fooName'));
  117. }
  118. public function testAdd()
  119. {
  120. $this->showMapper->add('fooName');
  121. $this->assertTrue($this->showMapper->has('fooName'));
  122. $fieldDescription = $this->showMapper->get('fooName');
  123. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  124. $this->assertEquals('fooName', $fieldDescription->getName());
  125. $this->assertEquals('fooName', $fieldDescription->getOption('label'));
  126. }
  127. public function testAddRemove()
  128. {
  129. $this->assertFalse($this->showMapper->has('fooName'));
  130. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  131. $this->showMapper->add($fieldDescription);
  132. $this->assertTrue($this->showMapper->has('fooName'));
  133. $this->showMapper->remove('fooName');
  134. $this->assertFalse($this->showMapper->has('fooName'));
  135. }
  136. public function testAddException()
  137. {
  138. try {
  139. $this->showMapper->add(12345);
  140. } catch (\RuntimeException $e) {
  141. $this->assertContains('invalid state', $e->getMessage());
  142. return;
  143. }
  144. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  145. }
  146. public function testReorder()
  147. {
  148. $this->assertEquals(array(), $this->admin->getShowGroups());
  149. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  150. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  151. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  152. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  153. $this->showMapper->with('Group1');
  154. $this->showMapper->add($fieldDescription1);
  155. $this->showMapper->add($fieldDescription2);
  156. $this->showMapper->add($fieldDescription3);
  157. $this->showMapper->add($fieldDescription4);
  158. $this->assertEquals(array(
  159. 'Group1' =>array(
  160. 'collapsed' => false,
  161. 'fields' => array('fooName1'=>'fooName1', 'fooName2'=>'fooName2', 'fooName3'=>'fooName3', 'fooName4'=>'fooName4'),
  162. 'description' => false,
  163. 'translation_domain' => null,
  164. 'class' => false,
  165. 'name' => 'Group1'
  166. )), $this->admin->getShowGroups());
  167. $this->showMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  168. // print_r is used to compare order of items in associative arrays
  169. $this->assertEquals(print_r(array(
  170. 'Group1' =>array(
  171. 'collapsed' => false,
  172. 'class' => false,
  173. 'description' => false,
  174. 'translation_domain' => null,
  175. 'name' => 'Group1',
  176. 'fields' => array('fooName3'=>'fooName3', 'fooName2'=>'fooName2', 'fooName1'=>'fooName1', 'fooName4'=>'fooName4'),
  177. )), true), print_r($this->admin->getShowGroups(), true));
  178. }
  179. private function getFieldDescriptionMock($name = null, $label = null)
  180. {
  181. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  182. if ($name !== null) {
  183. $fieldDescription->setName($name);
  184. }
  185. if ($label !== null) {
  186. $fieldDescription->setOption('label', $label);
  187. }
  188. return $fieldDescription;
  189. }
  190. }