FormMapperTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Form\FormMapper;
  12. class FormMapperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  17. $this->builder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  18. ->disableOriginalConstructor()
  19. ->getMock();
  20. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  21. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  22. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  23. $this->labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  24. $this->formMapper = new FormMapper(
  25. $this->contractor,
  26. $this->builder,
  27. $this->admin
  28. );
  29. }
  30. public function testWithNoOptions()
  31. {
  32. $this->admin->expects($this->once())
  33. ->method('setFormGroups')
  34. ->with(array(
  35. 'foobar' => array(
  36. 'collapsed' => false,
  37. 'fields' => array(),
  38. 'description' => false,
  39. 'translation_domain' => null,
  40. 'class' => false,
  41. )));
  42. $this->formMapper->with('foobar');
  43. }
  44. public function testWithOptions()
  45. {
  46. $this->admin->expects($this->once())
  47. ->method('setFormGroups')
  48. ->with(array(
  49. 'foobar' => array(
  50. 'collapsed' => false,
  51. 'fields' => array(),
  52. 'description' => false,
  53. 'translation_domain' => 'Foobar',
  54. 'class' => false,
  55. )));
  56. $this->formMapper->with('foobar', array(
  57. 'translation_domain' => 'Foobar',
  58. ));
  59. }
  60. public function testWithFieldsCascadeTranslationDomain()
  61. {
  62. $formGroups = array(
  63. 'foobar' => array(
  64. 'collapsed' => false,
  65. 'fields' => array(),
  66. 'description' => false,
  67. 'translation_domain' => 'Foobar',
  68. 'class' => false,
  69. )
  70. );
  71. $this->admin->expects($this->exactly(2))
  72. ->method('setFormGroups');
  73. $this->admin->expects($this->any())
  74. ->method('getFormGroups')
  75. ->will($this->returnValue($formGroups));
  76. $this->admin->expects($this->once())
  77. ->method('getModelManager')
  78. ->will($this->returnValue($this->modelManager));
  79. $this->modelManager->expects($this->once())
  80. ->method('getNewFieldDescriptionInstance')
  81. ->with(
  82. null, // mock admin ->getClass returns null
  83. 'foo',
  84. array(
  85. 'translation_domain' => 'Foobar',
  86. 'type' => 'bar',
  87. )
  88. )
  89. ->will($this->returnValue($this->fieldDescription));
  90. $this->contractor->expects($this->once())
  91. ->method('getDefaultOptions')
  92. ->will($this->returnValue(array()));
  93. $this->admin->expects($this->once())
  94. ->method('getLabelTranslatorStrategy')
  95. ->will($this->returnValue($this->labelTranslatorStrategy));
  96. $this->formMapper->with('foobar', array(
  97. 'translation_domain' => 'Foobar'
  98. ))
  99. ->add('foo', 'bar')
  100. ->end();
  101. }
  102. public function testRemoveCascadeRemoveFieldFromFormGroup()
  103. {
  104. $this->admin->expects($this->once())
  105. ->method('removeFieldFromFormGroup')
  106. ->with('foo')
  107. ;
  108. $this->formMapper->remove('foo');
  109. }
  110. }