ShowMapperTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Show\ShowMapper;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  15. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  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 Sonata\AdminBundle\Builder\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->groups = array();
  53. // php 5.3 BC
  54. $groups = & $this->groups;
  55. $this->admin->expects($this->any())
  56. ->method('getShowGroups')
  57. ->will($this->returnCallback(function() use (&$groups) {
  58. return $groups;
  59. }));
  60. $this->admin->expects($this->any())
  61. ->method('setShowGroups')
  62. ->will($this->returnCallback(function($showGroups) use (&$groups) {
  63. $groups = $showGroups;
  64. }));
  65. $this->admin->expects($this->any())
  66. ->method('reorderShowGroup')
  67. ->will($this->returnCallback(function($group, $keys) use (&$groups) {
  68. $showGroups = $groups;
  69. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  70. $groups = $showGroups;
  71. }));
  72. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  73. // php 5.3 BC
  74. $fieldDescription = $this->getFieldDescriptionMock();
  75. $modelManager->expects($this->any())
  76. ->method('getNewFieldDescriptionInstance')
  77. ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
  78. $fieldDescriptionClone = clone $fieldDescription;
  79. $fieldDescriptionClone->setName($name);
  80. $fieldDescriptionClone->setOptions($options);
  81. return $fieldDescriptionClone;
  82. }));
  83. $this->admin->expects($this->any())
  84. ->method('getModelManager')
  85. ->will($this->returnValue($modelManager));
  86. $labelTranslatorStrategy = new NoopLabelTranslatorStrategy;
  87. $this->admin->expects($this->any())
  88. ->method('getLabelTranslatorStrategy')
  89. ->will($this->returnValue($labelTranslatorStrategy));
  90. $this->showBuilder->expects($this->any())
  91. ->method('addField')
  92. ->will($this->returnCallback(function($list, $type, $fieldDescription, $admin) {
  93. $list->add($fieldDescription);
  94. }));
  95. $this->showMapper = new ShowMapper($this->showBuilder, $this->fieldDescriptionCollection, $this->admin);
  96. }
  97. public function testFluidInterface()
  98. {
  99. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  100. $this->assertEquals($this->showMapper, $this->showMapper->add($fieldDescription));
  101. $this->assertEquals($this->showMapper, $this->showMapper->remove('fooName'));
  102. $this->assertEquals($this->showMapper, $this->showMapper->reorder(array()));
  103. }
  104. public function testGet()
  105. {
  106. $this->assertFalse($this->showMapper->has('fooName'));
  107. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  108. $this->showMapper->add($fieldDescription);
  109. $this->assertEquals($fieldDescription, $this->showMapper->get('fooName'));
  110. }
  111. public function testAdd()
  112. {
  113. $this->showMapper->add('fooName');
  114. $this->assertTrue($this->showMapper->has('fooName'));
  115. $fieldDescription = $this->showMapper->get('fooName');
  116. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
  117. $this->assertEquals('fooName', $fieldDescription->getName());
  118. $this->assertEquals('fooName', $fieldDescription->getOption('label'));
  119. }
  120. public function testAddRemove()
  121. {
  122. $this->assertFalse($this->showMapper->has('fooName'));
  123. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  124. $this->showMapper->add($fieldDescription);
  125. $this->assertTrue($this->showMapper->has('fooName'));
  126. $this->showMapper->remove('fooName');
  127. $this->assertFalse($this->showMapper->has('fooName'));
  128. }
  129. public function testAddException()
  130. {
  131. try {
  132. $this->showMapper->add(12345);
  133. } catch (\RuntimeException $e) {
  134. $this->assertContains('invalid state', $e->getMessage());
  135. return;
  136. }
  137. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  138. }
  139. public function testReorder()
  140. {
  141. $this->assertEquals(array(), $this->admin->getShowGroups());
  142. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  143. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  144. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  145. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  146. $this->showMapper->with('Group1');
  147. $this->showMapper->add($fieldDescription1);
  148. $this->showMapper->add($fieldDescription2);
  149. $this->showMapper->add($fieldDescription3);
  150. $this->showMapper->add($fieldDescription4);
  151. $this->assertEquals(array(
  152. 'Group1' =>array(
  153. 'collapsed' => false,
  154. 'fields' => array('fooName1'=>'fooName1', 'fooName2'=>'fooName2', 'fooName3'=>'fooName3', 'fooName4'=>'fooName4'),
  155. 'description' => false,
  156. 'translation_domain' => null,
  157. )), $this->admin->getShowGroups());
  158. $this->showMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  159. // print_r is used to compare order of items in associative arrays
  160. $this->assertEquals(print_r(array(
  161. 'Group1' =>array(
  162. 'collapsed' => false,
  163. 'fields' => array('fooName3'=>'fooName3', 'fooName2'=>'fooName2', 'fooName1'=>'fooName1', 'fooName4'=>'fooName4'),
  164. 'description' => false,
  165. 'translation_domain' => null,
  166. )), true), print_r($this->admin->getShowGroups(), true));
  167. }
  168. private function getFieldDescriptionMock($name=null, $label=null)
  169. {
  170. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  171. if ($name !== null) {
  172. $fieldDescription->setName($name);
  173. }
  174. if ($label !== null) {
  175. $fieldDescription->setOption('label', $label);
  176. }
  177. return $fieldDescription;
  178. }
  179. }