FormMapperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. )));
  41. $this->formMapper->with('foobar');
  42. }
  43. public function testWithOptions()
  44. {
  45. $this->admin->expects($this->once())
  46. ->method('setFormGroups')
  47. ->with(array(
  48. 'foobar' => array(
  49. 'collapsed' => false,
  50. 'fields' => array(),
  51. 'description' => false,
  52. 'translation_domain' => 'Foobar',
  53. )));
  54. $this->formMapper->with('foobar', array(
  55. 'translation_domain' => 'Foobar',
  56. ));
  57. }
  58. public function testWithFieldsCascadeTranslationDomain()
  59. {
  60. $formGroups = array(
  61. 'foobar' => array(
  62. 'collapsed' => false,
  63. 'fields' => array(),
  64. 'description' => false,
  65. 'translation_domain' => 'Foobar',
  66. )
  67. );
  68. $this->admin->expects($this->exactly(2))
  69. ->method('setFormGroups');
  70. $this->admin->expects($this->any())
  71. ->method('getFormGroups')
  72. ->will($this->returnValue($formGroups));
  73. $this->admin->expects($this->once())
  74. ->method('getModelManager')
  75. ->will($this->returnValue($this->modelManager));
  76. $this->modelManager->expects($this->once())
  77. ->method('getNewFieldDescriptionInstance')
  78. ->with(
  79. null, // mock admin ->getClass returns null
  80. 'foo',
  81. array(
  82. 'translation_domain' => 'Foobar',
  83. 'type' => 'bar',
  84. )
  85. )
  86. ->will($this->returnValue($this->fieldDescription));
  87. $this->contractor->expects($this->once())
  88. ->method('getDefaultOptions')
  89. ->will($this->returnValue(array()));
  90. $this->admin->expects($this->once())
  91. ->method('getLabelTranslatorStrategy')
  92. ->will($this->returnValue($this->labelTranslatorStrategy));
  93. $this->formMapper->with('foobar', array(
  94. 'translation_domain' => 'Foobar'
  95. ))
  96. ->add('foo', 'bar')
  97. ->end();
  98. }
  99. }