BaseGroupedMapperTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Mapper;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
  13. /**
  14. * Test for BaseGroupedMapper.
  15. *
  16. * @author Andrej Hudec <pulzarraider@gmail.com>
  17. */
  18. class BaseGroupedMapperTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var BaseGroupedMapper
  22. */
  23. protected $baseGroupedMapper;
  24. private $tabs;
  25. private $groups;
  26. public function setUp()
  27. {
  28. $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $labelStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  32. $labelStrategy->expects($this->any())
  33. ->method('getLabel')
  34. ->will($this->returnCallback(function ($label) {
  35. return 'label_'.strtolower($label);
  36. }));
  37. $admin->expects($this->any())
  38. ->method('getLabelTranslatorStrategy')
  39. ->will($this->returnValue($labelStrategy));
  40. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  41. $configurationPool = new Pool($container, 'myTitle', 'myLogoTitle');
  42. $admin->expects($this->any())
  43. ->method('getConfigurationPool')
  44. ->will($this->returnValue($configurationPool));
  45. $builder = $this->getMockForAbstractClass('Sonata\AdminBundle\Builder\BuilderInterface');
  46. $this->baseGroupedMapper = $this->getMockForAbstractClass('Sonata\AdminBundle\Mapper\BaseGroupedMapper', array($builder, $admin));
  47. // php 5.3 BC
  48. $object = $this;
  49. $this->tabs = array();
  50. $this->groups = array();
  51. $this->baseGroupedMapper->expects($this->any())
  52. ->method('getTabs')
  53. ->will($this->returnCallback(function () use ($object) {
  54. return $object->getTabs();
  55. }));
  56. $this->baseGroupedMapper->expects($this->any())
  57. ->method('setTabs')
  58. ->will($this->returnCallback(function (array $tabs) use ($object) {
  59. $object->setTabs($tabs);
  60. }));
  61. $this->baseGroupedMapper->expects($this->any())
  62. ->method('getGroups')
  63. ->will($this->returnCallback(function () use ($object) {
  64. return $object->getTestGroups();
  65. }));
  66. $this->baseGroupedMapper->expects($this->any())
  67. ->method('setGroups')
  68. ->will($this->returnCallback(function (array $groups) use ($object) {
  69. $object->setTestGroups($groups);
  70. }));
  71. }
  72. public function testWith()
  73. {
  74. $this->assertCount(0, $this->tabs);
  75. $this->assertCount(0, $this->groups);
  76. $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
  77. $this->assertCount(1, $this->tabs);
  78. $this->assertCount(1, $this->groups);
  79. }
  80. public function testEnd()
  81. {
  82. $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
  83. }
  84. public function testTab()
  85. {
  86. $this->assertCount(0, $this->tabs);
  87. $this->assertCount(0, $this->groups);
  88. $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab'));
  89. $this->assertCount(1, $this->tabs);
  90. $this->assertCount(0, $this->groups);
  91. }
  92. public function testTab2()
  93. {
  94. $this->assertCount(0, $this->tabs);
  95. $this->assertCount(0, $this->groups);
  96. $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooTab', array('tab' => true)));
  97. $this->assertCount(1, $this->tabs);
  98. $this->assertCount(0, $this->groups);
  99. }
  100. public function testFluidInterface()
  101. {
  102. $this->assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab')->with('fooGroup1')->end()->with('fooGroup2')->end()->with('fooGroup3')->end()->end()->tab('barTab')->with('barGroup1')->end()->with('barGroup2')->end()->with('barGroup3')->end()->end());
  103. }
  104. /**
  105. * @expectedException \RuntimeException
  106. * @expectedExceptionMessage You should close previous group "fooGroup1" with end() before adding new tab "fooGroup2".
  107. */
  108. public function testGroupNotClosedException()
  109. {
  110. $this->baseGroupedMapper->with('fooGroup1');
  111. $this->baseGroupedMapper->with('fooGroup2');
  112. }
  113. /**
  114. * @expectedException \RuntimeException
  115. * @expectedExceptionMessage New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.
  116. */
  117. public function testGroupInTabException()
  118. {
  119. $this->baseGroupedMapper->with('fooGroup');
  120. $this->baseGroupedMapper->tab('fooTab');
  121. }
  122. /**
  123. * @expectedException \RuntimeException
  124. * @expectedExceptionMessage You should close previous tab "fooTab" with end() before adding new tab "barTab".
  125. */
  126. public function testTabInTabException()
  127. {
  128. $this->baseGroupedMapper->tab('fooTab');
  129. $this->baseGroupedMapper->tab('barTab');
  130. }
  131. public function testHasOpenTab()
  132. {
  133. $this->assertFalse($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns false when there are no tabs');
  134. $this->baseGroupedMapper->tab('fooTab');
  135. $this->assertTrue($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns true when there is an open tab');
  136. $this->baseGroupedMapper->end();
  137. $this->assertFalse($this->baseGroupedMapper->hasOpenTab(), '->hasOpenTab() returns false when all tabs are closed');
  138. }
  139. /**
  140. * @expectedException \RuntimeException
  141. * @expectedExceptionMessage No open tabs or groups, you cannot use end()
  142. */
  143. public function testEndException()
  144. {
  145. $this->baseGroupedMapper->end();
  146. }
  147. public function labelDataProvider()
  148. {
  149. return array(
  150. 'nominal use case not translated' => array(false, 'fooGroup1', null, 'fooGroup1'),
  151. 'nominal use case translated' => array(true, 'fooGroup1', null, 'label_foogroup1'),
  152. 'custom label not translated' => array(false, 'fooGroup1', 'custom_label', 'custom_label'),
  153. 'custom label translated' => array(true, 'fooGroup1', 'custom_label', 'custom_label'),
  154. );
  155. }
  156. /**
  157. * @dataProvider labelDataProvider
  158. */
  159. public function testLabel($translated, $name, $label, $expectedLabel)
  160. {
  161. $container = $this->baseGroupedMapper
  162. ->getAdmin()
  163. ->getConfigurationPool()
  164. ->getContainer();
  165. $container->expects($this->any())
  166. ->method('getParameter')
  167. ->will($this->returnValue($translated));
  168. $options = array();
  169. if (null !== $label) {
  170. $options['label'] = $label;
  171. }
  172. $this->baseGroupedMapper->with($name, $options);
  173. $this->assertSame($translated ? 'label_default' : 'default', $this->tabs['default']['label']);
  174. $this->assertSame($expectedLabel, $this->groups[$name]['label']);
  175. }
  176. public function getTabs()
  177. {
  178. return $this->tabs;
  179. }
  180. public function setTabs($tabs)
  181. {
  182. $this->tabs = $tabs;
  183. }
  184. public function getTestGroups()
  185. {
  186. return $this->groups;
  187. }
  188. public function setTestGroups($groups)
  189. {
  190. $this->groups = $groups;
  191. }
  192. }