ShowMapperTest.php 6.2 KB

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