DatagridMapperTest.php 9.7 KB

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