AdminHelperTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. public function testGetElementAccessPath()
  85. {
  86. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  87. $pool = new Pool($container, 'title', 'logo.png');
  88. $helper = new AdminHelper($pool);
  89. $object = $this->getMock('stdClass', array('getPathToObject'));
  90. $subObject = $this->getMock('stdClass', array('getAnd'));
  91. $sub2Object = $this->getMock('stdClass', array('getMore'));
  92. $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue(array($subObject)));
  93. $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
  94. $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue('Value'));
  95. $path = $helper->getElementAccessPath('uniquePartOfId_path_to_object_0_and_more', $object);
  96. $this->assertSame('path_to_object[0].and.more', $path);
  97. }
  98. /**
  99. * tests only so far that actual value/object is retrieved.
  100. *
  101. * @expectedException Exception
  102. * @expectedExceptionCode 0
  103. * @expectedExceptionMessage unknown collection class
  104. */
  105. public function testAppendFormFieldElementNested()
  106. {
  107. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  108. $pool = new Pool($container, 'title', 'logo.png');
  109. $helper = new AdminHelper($pool);
  110. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  111. $object = $this->getMock('stdClass', array('getSubObject'));
  112. $simpleObject = $this->getMock('stdClass', array('getSubObject'));
  113. $subObject = $this->getMock('stdClass', array('getAnd'));
  114. $sub2Object = $this->getMock('stdClass', array('getMore'));
  115. $sub3Object = $this->getMock('stdClass', array('getFinalData'));
  116. $dataMapper = $this->getMock('Symfony\Component\Form\DataMapperInterface');
  117. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  118. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  119. $formBuilder = new FormBuilder('test', get_class($simpleObject), $eventDispatcher, $formFactory);
  120. $childFormBuilder = new FormBuilder('subObject', get_class($subObject), $eventDispatcher, $formFactory);
  121. $object->expects($this->atLeastOnce())->method('getSubObject')->will($this->returnValue(array($subObject)));
  122. $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
  123. $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue(array($sub3Object)));
  124. $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->will($this->returnValue('value'));
  125. $formBuilder->setCompound(true);
  126. $formBuilder->setDataMapper($dataMapper);
  127. $formBuilder->add($childFormBuilder);
  128. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  129. $admin->expects($this->once())->method('getSubject')->will($this->returnValue($object));
  130. $helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
  131. }
  132. }