AdminHelperTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Admin;
  11. use Sonata\AdminBundle\Admin\AdminHelper;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  18. use Sonata\AdminBundle\Admin\AdminInterface;
  19. use Symfony\Component\Form\FormView;
  20. class AdminHelperTest extends \PHPUnit_Framework_TestCase
  21. {
  22. public function testGetChildFormBuilder()
  23. {
  24. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  25. $pool = new Pool($container, 'title', 'logo.png');
  26. $helper = new AdminHelper($pool);
  27. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  28. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  29. $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
  30. $childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory);
  31. $formBuilder->add($childFormBuilder);
  32. $this->assertNull($helper->getChildFormBuilder($formBuilder, 'foo'));
  33. $this->isInstanceOf('Symfony\Component\Form\FormBuilder', $helper->getChildFormBuilder($formBuilder, 'test_elementId'));
  34. }
  35. public function testGetChildFormView()
  36. {
  37. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  38. $pool = new Pool($container, 'title', 'logo.png');
  39. $helper = new AdminHelper($pool);
  40. $formView = new FormView();
  41. $formView->vars['id'] = 'test';
  42. $child = new FormView($formView);
  43. $child->vars['id'] = 'test_elementId';
  44. $this->assertNull($helper->getChildFormView($formView, 'foo'));
  45. $this->isInstanceOf('Symfony\Component\Form\FormView', $helper->getChildFormView($formView, 'test_elementId'));
  46. }
  47. public function testAddNewInstance()
  48. {
  49. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  50. $pool = new Pool($container, 'title', 'logo.png');
  51. $helper = new AdminHelper($pool);
  52. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  53. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  54. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  55. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  56. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
  57. $object = $this->getMock('sdtClass', array('addFooBar'));
  58. $object->expects($this->once())->method('addFooBar');
  59. $helper->addNewInstance($object, $fieldDescription);
  60. }
  61. public function testAddNewInstancePlural()
  62. {
  63. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  64. $pool = new Pool($container, 'title', 'logo.png');
  65. $helper = new AdminHelper($pool);
  66. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  67. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  68. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  69. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  70. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBars')));
  71. $object = $this->getMock('sdtClass', array('addFooBar'));
  72. $object->expects($this->once())->method('addFooBar');
  73. $helper->addNewInstance($object, $fieldDescription);
  74. }
  75. public function testAddNewInstanceInflector()
  76. {
  77. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  78. $pool = new Pool($container, 'title', 'logo.png');
  79. $helper = new AdminHelper($pool);
  80. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  81. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  82. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  83. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  84. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'entries')));
  85. $object = $this->getMock('sdtClass', array('addEntry'));
  86. $object->expects($this->once())->method('addEntry');
  87. $helper->addNewInstance($object, $fieldDescription);
  88. }
  89. }