DatagridMapperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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\Datagrid\DatagridMapper;
  13. use Sonata\AdminBundle\Datagrid\Datagrid;
  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->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  30. $proxyQuery = $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  31. $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
  32. $fieldDescriptionCollection = $this->getMock('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->getMock('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->getMock('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->assertEquals($this->datagridMapper, $this->datagridMapper->add($fieldDescription, null, array('field_name' => 'fooFilterName')));
  71. $this->assertEquals($this->datagridMapper, $this->datagridMapper->remove('fooName'));
  72. $this->assertEquals($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->assertEquals('foo.name', $filter->getName());
  82. $this->assertEquals('foo__name', $filter->getFormName());
  83. $this->assertEquals('text', $filter->getFieldType());
  84. $this->assertEquals('fooLabel', $filter->getLabel());
  85. $this->assertEquals(array('required' => false), $filter->getFieldOptions());
  86. $this->assertEquals(array(
  87. 'foo_default_option' => 'bar_default',
  88. 'label' => 'fooLabel',
  89. 'field_name' => 'fooFilterName',
  90. 'placeholder' => 'short_object_description_placeholder',
  91. 'link_parameters' => array(),
  92. 'show_filter' => null,
  93. 'advanced_filter' => true,
  94. ), $filter->getOptions());
  95. }
  96. public function testGet2()
  97. {
  98. $this->assertFalse($this->datagridMapper->has('fooName'));
  99. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  100. $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'));
  101. $filter = $this->datagridMapper->get('fooName');
  102. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
  103. $this->assertEquals('fooName', $filter->getName());
  104. $this->assertEquals('fooName', $filter->getFormName());
  105. $this->assertEquals('foo_field_type', $filter->getFieldType());
  106. $this->assertEquals('fooLabel', $filter->getLabel());
  107. $this->assertEquals(array('foo_field_option' => 'baz'), $filter->getFieldOptions());
  108. $this->assertEquals(array(
  109. 'foo_default_option' => 'bar_custom',
  110. 'label' => 'fooLabel',
  111. 'field_name' => 'fooFilterName',
  112. 'field_options' => array('foo_field_option' => 'baz'),
  113. 'field_type' => 'foo_field_type',
  114. 'placeholder' => 'short_object_description_placeholder',
  115. 'foo_filter_option' => 'foo_filter_option_value',
  116. 'link_parameters' => array(),
  117. 'show_filter' => null,
  118. 'advanced_filter' => true,
  119. ), $filter->getOptions());
  120. }
  121. public function testAdd()
  122. {
  123. $this->datagridMapper->add('fooName');
  124. $this->assertTrue($this->datagridMapper->has('fooName'));
  125. $fieldDescription = $this->datagridMapper->get('fooName');
  126. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
  127. $this->assertEquals('fooName', $fieldDescription->getName());
  128. }
  129. public function testAddWithoutFieldName()
  130. {
  131. $this->datagridMapper->add('foo.bar');
  132. $this->assertTrue($this->datagridMapper->has('foo.bar'));
  133. $fieldDescription = $this->datagridMapper->get('foo.bar');
  134. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
  135. $this->assertEquals('foo.bar', $fieldDescription->getName());
  136. $this->assertEquals('bar', $fieldDescription->getOption('field_name'));
  137. }
  138. public function testAddRemove()
  139. {
  140. $this->assertFalse($this->datagridMapper->has('fooName'));
  141. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  142. $this->datagridMapper->add($fieldDescription, null, array('field_name' => 'fooFilterName'));
  143. $this->assertTrue($this->datagridMapper->has('fooName'));
  144. $this->datagridMapper->remove('fooName');
  145. $this->assertFalse($this->datagridMapper->has('fooName'));
  146. $this->assertEquals('fooFilterName', $fieldDescription->getOption('field_name'));
  147. }
  148. public function testAddException()
  149. {
  150. try {
  151. $this->datagridMapper->add(12345);
  152. } catch (\RuntimeException $e) {
  153. $this->assertContains('Unknown field name in datagrid mapper. Field name should be either of FieldDescriptionInterface interface or string', $e->getMessage());
  154. return;
  155. }
  156. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  157. }
  158. public function testAddDuplicateNameException()
  159. {
  160. $tmpNames = array();
  161. $this->datagridMapper->getAdmin()
  162. ->expects($this->exactly(2))
  163. ->method('hasFilterFieldDescription')
  164. ->will($this->returnCallback(function ($name) use (&$tmpNames) {
  165. if (isset($tmpNames[$name])) {
  166. return true;
  167. }
  168. $tmpNames[$name] = $name;
  169. return false;
  170. }));
  171. try {
  172. $this->datagridMapper->add('fooName');
  173. $this->datagridMapper->add('fooName');
  174. } catch (\RuntimeException $e) {
  175. $this->assertContains('Duplicate field name "fooName" in datagrid mapper. Names should be unique.', $e->getMessage());
  176. return;
  177. }
  178. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  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->assertEquals(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->assertEquals(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. }