FormMapperTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Form;
  11. use Sonata\AdminBundle\Admin\Admin;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. class TestAdmin extends Admin
  14. {
  15. }
  16. class FormMapperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var \Sonata\AdminBundle\Builder\FormContractorInterface
  20. */
  21. protected $contractor;
  22. /**
  23. * @var \Symfony\Component\Form\FormBuilder
  24. */
  25. protected $builder;
  26. /**
  27. * @var \Sonata\AdminBundle\Admin\AdminInterface
  28. */
  29. protected $admin;
  30. /**
  31. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  32. */
  33. protected $modelManager;
  34. /**
  35. * @var \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  36. */
  37. protected $fieldDescription;
  38. /**
  39. * @var \Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface
  40. */
  41. protected $labelTranslatorStrategy;
  42. /**
  43. * @var FormMapper
  44. */
  45. protected $formMapper;
  46. public function setUp()
  47. {
  48. $this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  49. $this->builder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->admin = new TestAdmin('code', 'class', 'controller');
  53. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  54. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  55. $this->labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  56. $this->formMapper = new FormMapper(
  57. $this->contractor,
  58. $this->builder,
  59. $this->admin
  60. );
  61. }
  62. public function testWithNoOptions()
  63. {
  64. $this->formMapper->with('foobar');
  65. $this->assertEquals(array('default' => array (
  66. 'collapsed' => false,
  67. 'class' => false,
  68. 'description' => false,
  69. 'translation_domain' => null,
  70. 'auto_created' => true,
  71. 'groups' => array('foobar'),
  72. 'tab' => true,
  73. 'name' => 'default'
  74. )), $this->admin->getFormTabs());
  75. $this->assertEquals(array('foobar' => array(
  76. 'collapsed' => false,
  77. 'class' => false,
  78. 'description' => false,
  79. 'translation_domain' => null,
  80. 'fields' => array (),
  81. 'name' => 'foobar'
  82. )), $this->admin->getFormGroups());
  83. }
  84. public function testWithOptions()
  85. {
  86. $this->formMapper->with('foobar', array(
  87. 'translation_domain' => 'Foobar',
  88. ));
  89. $this->assertEquals(array('foobar' => array(
  90. 'collapsed' => false,
  91. 'class' => false,
  92. 'description' => false,
  93. 'translation_domain' => 'Foobar',
  94. 'fields' => array (),
  95. 'name' => 'foobar'
  96. )), $this->admin->getFormGroups());
  97. $this->assertEquals(array('default' => array (
  98. 'collapsed' => false,
  99. 'class' => false,
  100. 'description' => false,
  101. 'translation_domain' => 'Foobar',
  102. 'auto_created' => true,
  103. 'groups' => array('foobar'),
  104. 'tab' => true,
  105. 'name' => 'default'
  106. )), $this->admin->getFormTabs());
  107. }
  108. public function testWithFieldsCascadeTranslationDomain()
  109. {
  110. $formGroups = array(
  111. 'foobar' => array(
  112. 'collapsed' => false,
  113. 'fields' => array(),
  114. 'description' => false,
  115. 'translation_domain' => 'Foobar',
  116. 'class' => false,
  117. 'name' => 'foobar'
  118. )
  119. );
  120. $this->admin->setModelManager($this->modelManager);
  121. $this->modelManager->expects($this->once())
  122. ->method('getNewFieldDescriptionInstance')
  123. ->with(
  124. 'class',
  125. 'foo',
  126. array(
  127. 'translation_domain' => 'Foobar',
  128. 'type' => 'bar',
  129. )
  130. )
  131. ->will($this->returnValue($this->fieldDescription));
  132. $this->contractor->expects($this->once())
  133. ->method('getDefaultOptions')
  134. ->will($this->returnValue(array()));
  135. $this->admin->setLabelTranslatorStrategy($this->labelTranslatorStrategy);
  136. $this->formMapper->with('foobar', array(
  137. 'translation_domain' => 'Foobar'
  138. ))
  139. ->add('foo', 'bar')
  140. ->end();
  141. $this->assertEquals(array('default' => array (
  142. 'collapsed' => false,
  143. 'class' => false,
  144. 'description' => false,
  145. 'translation_domain' => 'Foobar',
  146. 'auto_created' => true,
  147. 'groups' => array ('foobar'),
  148. 'tab' => true,
  149. 'name' => 'default'
  150. )), $this->admin->getFormTabs());
  151. $this->assertEquals(array('foobar' => array(
  152. 'collapsed' => false,
  153. 'class' => false,
  154. 'description' => false,
  155. 'translation_domain' => 'Foobar',
  156. 'fields' => array(
  157. 'foo' => 'foo'
  158. ),
  159. 'name' => 'foobar'
  160. )), $this->admin->getFormGroups());
  161. }
  162. public function testRemoveCascadeRemoveFieldFromFormGroup()
  163. {
  164. $this->formMapper->with('foo');
  165. $this->formMapper->remove('foo');
  166. }
  167. }