DatagridMapperTest.php 11 KB

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