ShowMapperTest.php 8.0 KB

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