DatagridMapperTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. */
  11. namespace Sonata\AdminBundle\Tests\Datagrid;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  14. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  15. use Sonata\AdminBundle\Datagrid\Datagrid;
  16. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  17. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  18. use Sonata\AdminBundle\Datagrid\PagerInterface;
  19. use Sonata\AdminBundle\Filter\Filter;
  20. use Sonata\AdminBundle\Filter\FilterInterface;
  21. /**
  22. * @author Andrej Hudec <pulzarraider@gmail.com>
  23. */
  24. class DatagridMapperTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @var DatagridMapper
  28. */
  29. private $datagridMapper;
  30. /**
  31. * @var Datagrid
  32. */
  33. private $datagrid;
  34. public function setUp()
  35. {
  36. $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  37. $proxyQuery = $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  38. $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
  39. $fieldDescriptionCollection = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  40. $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->datagrid = new Datagrid($proxyQuery, $fieldDescriptionCollection, $pager, $formBuilder, array());
  44. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  45. // php 5.3 BC
  46. $filter = $this->getMockForAbstractClass('Sonata\AdminBundle\Filter\Filter');
  47. $filter->expects($this->any())
  48. ->method('getDefaultOptions')
  49. ->will($this->returnValue(array('foo_default_option'=>'bar_default')));
  50. $datagridBuilder->expects($this->any())
  51. ->method('addFilter')
  52. ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, $admin) use ($filter) {
  53. $fieldDescription->setType($type);
  54. $filterClone = clone $filter;
  55. $filterClone->initialize($fieldDescription->getName(), $fieldDescription->getOptions());
  56. $datagrid->addFilter($filterClone);
  57. }));
  58. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  59. // php 5.3 BC
  60. $fieldDescription = $this->getFieldDescriptionMock();
  61. $modelManager->expects($this->any())
  62. ->method('getNewFieldDescriptionInstance')
  63. ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
  64. $fieldDescriptionClone = clone $fieldDescription;
  65. $fieldDescriptionClone->setName($name);
  66. $fieldDescriptionClone->setOptions($options);
  67. return $fieldDescriptionClone;
  68. }));
  69. $admin->expects($this->any())
  70. ->method('getModelManager')
  71. ->will($this->returnValue($modelManager));
  72. $this->datagridMapper = new DatagridMapper($datagridBuilder, $this->datagrid, $admin);
  73. }
  74. public function testFluidInterface()
  75. {
  76. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  77. $this->assertEquals($this->datagridMapper, $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName')));
  78. $this->assertEquals($this->datagridMapper, $this->datagridMapper->remove('fooName'));
  79. $this->assertEquals($this->datagridMapper, $this->datagridMapper->reorder(array()));
  80. }
  81. public function testGet()
  82. {
  83. $this->assertFalse($this->datagridMapper->has('fooName'));
  84. $fieldDescription = $this->getFieldDescriptionMock('foo.name', 'fooLabel');
  85. $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName'));
  86. $filter = $this->datagridMapper->get('foo.name');
  87. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
  88. $this->assertEquals('foo.name', $filter->getName());
  89. $this->assertEquals('foo__name', $filter->getFormName());
  90. $this->assertEquals('text', $filter->getFieldType());
  91. $this->assertEquals('fooLabel', $filter->getLabel());
  92. $this->assertEquals(array('required'=>false), $filter->getFieldOptions());
  93. $this->assertEquals(array(
  94. 'foo_default_option' => 'bar_default',
  95. 'label' => 'fooLabel',
  96. 'field_name' => 'fooFilterName',
  97. 'placeholder' => 'short_object_description_placeholder',
  98. 'link_parameters' => array(),
  99. 'show_filter' => true
  100. ), $filter->getOptions());
  101. }
  102. public function testGet2()
  103. {
  104. $this->assertFalse($this->datagridMapper->has('fooName'));
  105. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  106. $this->datagridMapper->add($fieldDescription, 'foo_type', array('field_name'=>'fooFilterName', 'foo_filter_option'=>'foo_filter_option_value', 'foo_default_option'=>'bar_custom'), 'foo_field_type', array('foo_field_option'=>'baz'));
  107. $filter = $this->datagridMapper->get('fooName');
  108. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
  109. $this->assertEquals('fooName', $filter->getName());
  110. $this->assertEquals('fooName', $filter->getFormName());
  111. $this->assertEquals('foo_field_type', $filter->getFieldType());
  112. $this->assertEquals('fooLabel', $filter->getLabel());
  113. $this->assertEquals(array('foo_field_option'=>'baz'), $filter->getFieldOptions());
  114. $this->assertEquals(array(
  115. 'foo_default_option' => 'bar_custom',
  116. 'label' => 'fooLabel',
  117. 'field_name' => 'fooFilterName',
  118. 'field_options' => array ('foo_field_option' => 'baz'),
  119. 'field_type' => 'foo_field_type',
  120. 'placeholder' => 'short_object_description_placeholder',
  121. 'foo_filter_option' => 'foo_filter_option_value',
  122. 'link_parameters' => array(),
  123. 'show_filter' => true
  124. ), $filter->getOptions());
  125. }
  126. public function testAdd()
  127. {
  128. $this->datagridMapper->add('fooName');
  129. $this->assertTrue($this->datagridMapper->has('fooName'));
  130. $fieldDescription = $this->datagridMapper->get('fooName');
  131. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
  132. $this->assertEquals('fooName', $fieldDescription->getName());
  133. }
  134. public function testAddRemove()
  135. {
  136. $this->assertFalse($this->datagridMapper->has('fooName'));
  137. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  138. $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName'));
  139. $this->assertTrue($this->datagridMapper->has('fooName'));
  140. $this->datagridMapper->remove('fooName');
  141. $this->assertFalse($this->datagridMapper->has('fooName'));
  142. }
  143. public function testAddException()
  144. {
  145. try {
  146. $this->datagridMapper->add(12345);
  147. } catch (\RuntimeException $e) {
  148. $this->assertContains('invalid state', $e->getMessage());
  149. return;
  150. }
  151. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  152. }
  153. public function testAddException2()
  154. {
  155. try {
  156. $this->datagridMapper->getAdmin()
  157. ->expects($this->any())
  158. ->method('hasFilterFieldDescription')
  159. ->will($this->returnValue(true))
  160. ;
  161. $this->datagridMapper->add('field');
  162. } catch (\RuntimeException $e) {
  163. $this->assertContains('The field "field" is already defined', $e->getMessage());
  164. return;
  165. }
  166. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  167. }
  168. public function testReorder()
  169. {
  170. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  171. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  172. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  173. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  174. $this->datagridMapper->add($fieldDescription1, null, array('field_name'=>'fooFilterName1'));
  175. $this->datagridMapper->add($fieldDescription2, null, array('field_name'=>'fooFilterName2'));
  176. $this->datagridMapper->add($fieldDescription3, null, array('field_name'=>'fooFilterName3'));
  177. $this->datagridMapper->add($fieldDescription4, null, array('field_name'=>'fooFilterName4'));
  178. $this->assertEquals(array(
  179. 'fooName1',
  180. 'fooName2',
  181. 'fooName3',
  182. 'fooName4',
  183. ), array_keys($this->datagrid->getFilters()));
  184. $this->datagridMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  185. $this->assertEquals(array(
  186. 'fooName3',
  187. 'fooName2',
  188. 'fooName1',
  189. 'fooName4',
  190. ), array_keys($this->datagrid->getFilters()));
  191. }
  192. private function getFieldDescriptionMock($name = null, $label = null)
  193. {
  194. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  195. if ($name !== null) {
  196. $fieldDescription->setName($name);
  197. }
  198. if ($label !== null) {
  199. $fieldDescription->setOption('label', $label);
  200. }
  201. return $fieldDescription;
  202. }
  203. }