DatagridMapperTest.php 11 KB

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