AdminHelperTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }