AdminHelperTest.php 4.9 KB

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