AdminHelperTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Admin;
  11. use Sonata\AdminBundle\Admin\AdminHelper;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Symfony\Component\Form\FormView;
  15. class AdminHelperTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var AdminHelper
  19. */
  20. protected $helper;
  21. public function setUp()
  22. {
  23. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  24. $pool = new Pool($container, 'title', 'logo.png');
  25. $this->helper = new AdminHelper($pool);
  26. }
  27. public function testGetChildFormBuilder()
  28. {
  29. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  30. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  31. $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
  32. $childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory);
  33. $formBuilder->add($childFormBuilder);
  34. $this->assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
  35. $this->isInstanceOf('Symfony\Component\Form\FormBuilder', $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
  36. }
  37. public function testGetChildFormView()
  38. {
  39. $formView = new FormView();
  40. $formView->vars['id'] = 'test';
  41. $child = new FormView($formView);
  42. $child->vars['id'] = 'test_elementId';
  43. $this->assertNull($this->helper->getChildFormView($formView, 'foo'));
  44. $this->isInstanceOf('Symfony\Component\Form\FormView', $this->helper->getChildFormView($formView, 'test_elementId'));
  45. }
  46. public function testAddNewInstance()
  47. {
  48. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  49. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  50. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  51. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  52. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
  53. $object = $this->getMock('sdtClass', array('addFooBar'));
  54. $object->expects($this->once())->method('addFooBar');
  55. $this->helper->addNewInstance($object, $fieldDescription);
  56. }
  57. public function testAddNewInstancePlural()
  58. {
  59. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  60. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  61. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  62. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  63. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBars')));
  64. $object = $this->getMock('sdtClass', array('addFooBar'));
  65. $object->expects($this->once())->method('addFooBar');
  66. $this->helper->addNewInstance($object, $fieldDescription);
  67. }
  68. public function testAddNewInstanceInflector()
  69. {
  70. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  71. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  72. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  73. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  74. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'entries')));
  75. $object = $this->getMock('sdtClass', array('addEntry'));
  76. $object->expects($this->once())->method('addEntry');
  77. $this->helper->addNewInstance($object, $fieldDescription);
  78. }
  79. public function testGetElementAccessPath()
  80. {
  81. $object = $this->getMock('stdClass', array('getPathToObject'));
  82. $subObject = $this->getMock('stdClass', array('getAnd'));
  83. $sub2Object = $this->getMock('stdClass', array('getMore'));
  84. $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue(array($subObject)));
  85. $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
  86. $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue('Value'));
  87. $path = $this->helper->getElementAccessPath('uniquePartOfId_path_to_object_0_and_more', $object);
  88. $this->assertSame('path_to_object[0].and.more', $path);
  89. }
  90. public function testAppendFormFieldElementNested()
  91. {
  92. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  93. $object = $this->getMock('stdClass', array('getSubObject'));
  94. $simpleObject = $this->getMock('stdClass', array('getSubObject'));
  95. $subObject = $this->getMock('stdClass', array('getAnd'));
  96. $sub2Object = $this->getMock('stdClass', array('getMore'));
  97. $sub3Object = $this->getMock('stdClass', array('getFinalData'));
  98. $dataMapper = $this->getMock('Symfony\Component\Form\DataMapperInterface');
  99. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  100. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  101. $formBuilder = new FormBuilder('test', get_class($simpleObject), $eventDispatcher, $formFactory);
  102. $childFormBuilder = new FormBuilder('subObject', get_class($subObject), $eventDispatcher, $formFactory);
  103. $object->expects($this->atLeastOnce())->method('getSubObject')->will($this->returnValue(array($subObject)));
  104. $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
  105. $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue(array($sub3Object)));
  106. $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->will($this->returnValue('value'));
  107. $formBuilder->setCompound(true);
  108. $formBuilder->setDataMapper($dataMapper);
  109. $formBuilder->add($childFormBuilder);
  110. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  111. $admin->expects($this->once())->method('getSubject')->will($this->returnValue($object));
  112. $this->setExpectedException('Exception', 'unknown collection class');
  113. $this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
  114. }
  115. }