BaseGroupedMapperTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Mapper;
  11. use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
  12. /**
  13. * Test for BaseGroupedMapper
  14. *
  15. * @author Andrej Hudec <pulzarraider@gmail.com>
  16. */
  17. class BaseGroupedMapperTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var BaseGroupedMapper
  21. */
  22. protected $baseGroupedMapper;
  23. private $tabs;
  24. private $groups;
  25. public function setUp()
  26. {
  27. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  28. $builder = $this->getMock('Sonata\AdminBundle\Builder\BuilderInterface');
  29. $this->baseGroupedMapper = $this->getMockForAbstractClass('Sonata\AdminBundle\Mapper\BaseGroupedMapper', array($builder, $admin));
  30. // php 5.3 BC
  31. $object = $this;
  32. $this->tabs = array();
  33. $this->groups = array();
  34. $this->baseGroupedMapper->expects($this->any())
  35. ->method('getTabs')
  36. ->will($this->returnCallback(function () use ($object) {
  37. return $object->getTabs();
  38. }));
  39. $this->baseGroupedMapper->expects($this->any())
  40. ->method('setTabs')
  41. ->will($this->returnCallback(function (array $tabs) use ($object) {
  42. $object->setTabs($tabs);
  43. }));
  44. $this->baseGroupedMapper->expects($this->any())
  45. ->method('getGroups')
  46. ->will($this->returnCallback(function () use ($object) {
  47. return $object->getGroups();
  48. }));
  49. $this->baseGroupedMapper->expects($this->any())
  50. ->method('setGroups')
  51. ->will($this->returnCallback(function (array $groups) use ($object) {
  52. $object->setGroups($groups);
  53. }));
  54. }
  55. public function testWith()
  56. {
  57. $this->assertCount(0, $this->tabs);
  58. $this->assertCount(0, $this->groups);
  59. $this->assertEquals($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
  60. $this->assertCount(1, $this->tabs);
  61. $this->assertCount(1, $this->groups);
  62. }
  63. public function testEnd()
  64. {
  65. $this->assertEquals($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooGroup'));
  66. }
  67. public function testTab()
  68. {
  69. $this->assertCount(0, $this->tabs);
  70. $this->assertCount(0, $this->groups);
  71. $this->assertEquals($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab'));
  72. $this->assertCount(1, $this->tabs);
  73. $this->assertCount(0, $this->groups);
  74. }
  75. public function testTab2()
  76. {
  77. $this->assertCount(0, $this->tabs);
  78. $this->assertCount(0, $this->groups);
  79. $this->assertEquals($this->baseGroupedMapper, $this->baseGroupedMapper->with('fooTab', array('tab'=>true)));
  80. $this->assertCount(1, $this->tabs);
  81. $this->assertCount(0, $this->groups);
  82. }
  83. public function testFluidInterface()
  84. {
  85. $this->assertEquals($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());
  86. }
  87. /**
  88. * @expectedException RuntimeException
  89. * @expectedExceptionMessage You should close previous group "fooGroup1" with end() before adding new tab "fooGroup2".
  90. */
  91. public function testGroupNotClosedException()
  92. {
  93. $this->baseGroupedMapper->with('fooGroup1');
  94. $this->baseGroupedMapper->with('fooGroup2');
  95. }
  96. /**
  97. * @expectedException RuntimeException
  98. * @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.
  99. */
  100. public function testGroupInTabException()
  101. {
  102. $this->baseGroupedMapper->with('fooGroup');
  103. $this->baseGroupedMapper->tab('fooTab');
  104. }
  105. /**
  106. * @expectedException RuntimeException
  107. * @expectedExceptionMessage You should close previous tab "fooTab" with end() before adding new tab "barTab".
  108. */
  109. public function testTabInTabException()
  110. {
  111. $this->baseGroupedMapper->tab('fooTab');
  112. $this->baseGroupedMapper->tab('barTab');
  113. }
  114. /**
  115. * @expectedException RuntimeException
  116. * @expectedExceptionMessage No open tabs or groups, you cannot use end()
  117. */
  118. public function testEndException()
  119. {
  120. $this->baseGroupedMapper->end();
  121. }
  122. public function getTabs()
  123. {
  124. return $this->tabs;
  125. }
  126. public function setTabs($tabs)
  127. {
  128. $this->tabs = $tabs;
  129. }
  130. public function getGroups()
  131. {
  132. return $this->groups;
  133. }
  134. public function setGroups($groups)
  135. {
  136. $this->groups = $groups;
  137. }
  138. }