AdminHelperTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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', $formFactory, $eventDispatcher);
  30. $childFormBuilder = new FormBuilder('elementId', $formFactory, $eventDispatcher);
  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->set('id', 'test');
  42. $child = new FormView();
  43. $child->set('id', 'test_elementId');
  44. $formView->setChildren(array('elementId' => $child));
  45. $this->assertNull($helper->getChildFormView($formView, 'foo'));
  46. $this->isInstanceOf('Symfony\Component\Form\FormView', $helper->getChildFormView($formView, 'test_elementId'));
  47. }
  48. public function testaddNewInstance()
  49. {
  50. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  51. $pool = new Pool($container, 'title', 'logo.png');
  52. $helper = new AdminHelper($pool);
  53. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  54. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  55. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  56. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  57. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
  58. $object = $this->getMock('sdtClass', array('addFooBar'));
  59. $object->expects($this->once())->method('addFooBar');
  60. $helper->addNewInstance($object, $fieldDescription);
  61. }
  62. }