AdminHelperTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Symfony\Component\Form\FormView;
  16. class AdminHelperTest extends PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var AdminHelper
  20. */
  21. protected $helper;
  22. public function setUp()
  23. {
  24. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  25. $pool = new Pool($container, 'title', 'logo.png');
  26. $this->helper = new AdminHelper($pool);
  27. }
  28. public function testGetChildFormBuilder()
  29. {
  30. $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
  31. $eventDispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  32. $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
  33. $childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory);
  34. $formBuilder->add($childFormBuilder);
  35. $this->assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
  36. $this->isInstanceOf('Symfony\Component\Form\FormBuilder', $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
  37. }
  38. public function testGetChildFormView()
  39. {
  40. $formView = new FormView();
  41. $formView->vars['id'] = 'test';
  42. $child = new FormView($formView);
  43. $child->vars['id'] = 'test_elementId';
  44. $this->assertNull($this->helper->getChildFormView($formView, 'foo'));
  45. $this->isInstanceOf('Symfony\Component\Form\FormView', $this->helper->getChildFormView($formView, 'test_elementId'));
  46. }
  47. public function testAddNewInstance()
  48. {
  49. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  50. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  51. $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  52. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  53. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
  54. $object = $this->getMockBuilder('stdClass')
  55. ->setMethods(array('addFooBar'))
  56. ->getMock();
  57. $object->expects($this->once())->method('addFooBar');
  58. $this->helper->addNewInstance($object, $fieldDescription);
  59. }
  60. public function testAddNewInstancePlural()
  61. {
  62. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  63. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  64. $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  65. $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
  66. $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBars')));
  67. $object = $this->getMockBuilder('stdClass')
  68. ->setMethods(array('addFooBar'))
  69. ->getMock();
  70. $object->expects($this->once())->method('addFooBar');
  71. $this->helper->addNewInstance($object, $fieldDescription);
  72. }
  73. public function testAddNewInstanceInflector()
  74. {
  75. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  76. $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
  77. $fieldDescription = $this->createMock('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->getMockBuilder('stdClass')
  81. ->setMethods(array('addEntry'))
  82. ->getMock();
  83. $object->expects($this->once())->method('addEntry');
  84. $this->helper->addNewInstance($object, $fieldDescription);
  85. }
  86. public function testGetElementAccessPath()
  87. {
  88. $object = $this->getMockBuilder('stdClass')
  89. ->setMethods(array('getPathToObject'))
  90. ->getMock();
  91. $subObject = $this->getMockBuilder('stdClass')
  92. ->setMethods(array('getAnother'))
  93. ->getMock();
  94. $sub2Object = $this->getMockBuilder('stdClass')
  95. ->setMethods(array('getMoreThings'))
  96. ->getMock();
  97. $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue(array($subObject)));
  98. $subObject->expects($this->atLeastOnce())->method('getAnother')->will($this->returnValue($sub2Object));
  99. $sub2Object->expects($this->atLeastOnce())->method('getMoreThings')->will($this->returnValue('Value'));
  100. $path = $this->helper->getElementAccessPath('uniquePartOfId_path_to_object_0_another_more_things', $object);
  101. $this->assertSame('path_to_object[0].another.more_things', $path);
  102. }
  103. public function testItThrowsExceptionWhenDoesNotFindTheFullPath()
  104. {
  105. $path = 'uniquePartOfId_path_to_object_0_more_calls';
  106. $object = $this->getMockBuilder('stdClass')
  107. ->setMethods(array('getPathToObject'))
  108. ->getMock();
  109. $subObject = $this->getMockBuilder('stdClass')
  110. ->setMethods(array('getMore'))
  111. ->getMock();
  112. $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue(array($subObject)));
  113. $subObject->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue('Value'));
  114. $this->expectException('Exception', 'Could not get element id from '.$path.' Failing part: calls');
  115. $this->helper->getElementAccessPath($path, $object);
  116. }
  117. public function testAppendFormFieldElementNested()
  118. {
  119. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  120. $object = $this->getMockBuilder('stdClass')
  121. ->setMethods(array('getSubObject'))
  122. ->getMock();
  123. $simpleObject = $this->getMockBuilder('stdClass')
  124. ->setMethods(array('getSubObject'))
  125. ->getMock();
  126. $subObject = $this->getMockBuilder('stdClass')
  127. ->setMethods(array('getAnd'))
  128. ->getMock();
  129. $sub2Object = $this->getMockBuilder('stdClass')
  130. ->setMethods(array('getMore'))
  131. ->getMock();
  132. $sub3Object = $this->getMockBuilder('stdClass')
  133. ->setMethods(array('getFinalData'))
  134. ->getMock();
  135. $dataMapper = $this->createMock('Symfony\Component\Form\DataMapperInterface');
  136. $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
  137. $eventDispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  138. $formBuilder = new FormBuilder('test', get_class($simpleObject), $eventDispatcher, $formFactory);
  139. $childFormBuilder = new FormBuilder('subObject', get_class($subObject), $eventDispatcher, $formFactory);
  140. $object->expects($this->atLeastOnce())->method('getSubObject')->will($this->returnValue(array($subObject)));
  141. $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
  142. $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue(array($sub3Object)));
  143. $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->will($this->returnValue('value'));
  144. $formBuilder->setCompound(true);
  145. $formBuilder->setDataMapper($dataMapper);
  146. $formBuilder->add($childFormBuilder);
  147. $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  148. $admin->expects($this->once())->method('getSubject')->will($this->returnValue($object));
  149. $this->expectException('Exception', 'unknown collection class');
  150. $this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
  151. }
  152. }