DatagridMapperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
  83. ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
  84. : 'text', $filter->getFieldType());
  85. $this->assertSame('fooLabel', $filter->getLabel());
  86. $this->assertSame(array('required' => false), $filter->getFieldOptions());
  87. $this->assertSame(array(
  88. 'show_filter' => null,
  89. 'advanced_filter' => true,
  90. 'foo_default_option' => 'bar_default',
  91. 'label' => 'fooLabel',
  92. 'field_name' => 'fooFilterName',
  93. 'placeholder' => 'short_object_description_placeholder',
  94. 'link_parameters' => array(),
  95. ), $filter->getOptions());
  96. }
  97. public function testGet2()
  98. {
  99. $this->assertFalse($this->datagridMapper->has('fooName'));
  100. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  101. $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'));
  102. $filter = $this->datagridMapper->get('fooName');
  103. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
  104. $this->assertSame('fooName', $filter->getName());
  105. $this->assertSame('fooName', $filter->getFormName());
  106. $this->assertSame('foo_field_type', $filter->getFieldType());
  107. $this->assertSame('fooLabel', $filter->getLabel());
  108. $this->assertSame(array('foo_field_option' => 'baz'), $filter->getFieldOptions());
  109. $this->assertSame(array(
  110. 'show_filter' => null,
  111. 'advanced_filter' => true,
  112. 'foo_default_option' => 'bar_custom',
  113. 'label' => 'fooLabel',
  114. 'field_name' => 'fooFilterName',
  115. 'foo_filter_option' => 'foo_filter_option_value',
  116. 'field_options' => array('foo_field_option' => 'baz'),
  117. 'field_type' => 'foo_field_type',
  118. 'placeholder' => 'short_object_description_placeholder',
  119. 'link_parameters' => array(),
  120. ), $filter->getOptions());
  121. }
  122. public function testAdd()
  123. {
  124. $this->datagridMapper->add('fooName');
  125. $this->assertTrue($this->datagridMapper->has('fooName'));
  126. $fieldDescription = $this->datagridMapper->get('fooName');
  127. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
  128. $this->assertSame('fooName', $fieldDescription->getName());
  129. }
  130. public function testAddWithoutFieldName()
  131. {
  132. $this->datagridMapper->add('foo.bar');
  133. $this->assertTrue($this->datagridMapper->has('foo.bar'));
  134. $fieldDescription = $this->datagridMapper->get('foo.bar');
  135. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
  136. $this->assertSame('foo.bar', $fieldDescription->getName());
  137. $this->assertSame('bar', $fieldDescription->getOption('field_name'));
  138. }
  139. public function testAddRemove()
  140. {
  141. $this->assertFalse($this->datagridMapper->has('fooName'));
  142. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  143. $this->datagridMapper->add($fieldDescription, null, array('field_name' => 'fooFilterName'));
  144. $this->assertTrue($this->datagridMapper->has('fooName'));
  145. $this->datagridMapper->remove('fooName');
  146. $this->assertFalse($this->datagridMapper->has('fooName'));
  147. $this->assertSame('fooFilterName', $fieldDescription->getOption('field_name'));
  148. }
  149. public function testAddException()
  150. {
  151. try {
  152. $this->datagridMapper->add(12345);
  153. } catch (\RuntimeException $e) {
  154. $this->assertContains('Unknown field name in datagrid mapper. Field name should be either of FieldDescriptionInterface interface or string', $e->getMessage());
  155. return;
  156. }
  157. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  158. }
  159. public function testAddDuplicateNameException()
  160. {
  161. $tmpNames = array();
  162. $this->datagridMapper->getAdmin()
  163. ->expects($this->exactly(2))
  164. ->method('hasFilterFieldDescription')
  165. ->will($this->returnCallback(function ($name) use (&$tmpNames) {
  166. if (isset($tmpNames[$name])) {
  167. return true;
  168. }
  169. $tmpNames[$name] = $name;
  170. return false;
  171. }));
  172. try {
  173. $this->datagridMapper->add('fooName');
  174. $this->datagridMapper->add('fooName');
  175. } catch (\RuntimeException $e) {
  176. $this->assertContains('Duplicate field name "fooName" in datagrid mapper. Names should be unique.', $e->getMessage());
  177. return;
  178. }
  179. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  180. }
  181. public function testKeys()
  182. {
  183. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  184. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  185. $this->datagridMapper->add($fieldDescription1, null, array('field_name' => 'fooFilterName1'));
  186. $this->datagridMapper->add($fieldDescription2, null, array('field_name' => 'fooFilterName2'));
  187. $this->assertSame(array('fooName1', 'fooName2'), $this->datagridMapper->keys());
  188. }
  189. public function testReorder()
  190. {
  191. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  192. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  193. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  194. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  195. $this->datagridMapper->add($fieldDescription1, null, array('field_name' => 'fooFilterName1'));
  196. $this->datagridMapper->add($fieldDescription2, null, array('field_name' => 'fooFilterName2'));
  197. $this->datagridMapper->add($fieldDescription3, null, array('field_name' => 'fooFilterName3'));
  198. $this->datagridMapper->add($fieldDescription4, null, array('field_name' => 'fooFilterName4'));
  199. $this->assertSame(array(
  200. 'fooName1',
  201. 'fooName2',
  202. 'fooName3',
  203. 'fooName4',
  204. ), array_keys($this->datagrid->getFilters()));
  205. $this->datagridMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  206. $this->assertSame(array(
  207. 'fooName3',
  208. 'fooName2',
  209. 'fooName1',
  210. 'fooName4',
  211. ), array_keys($this->datagrid->getFilters()));
  212. }
  213. private function getFieldDescriptionMock($name = null, $label = null)
  214. {
  215. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  216. if ($name !== null) {
  217. $fieldDescription->setName($name);
  218. }
  219. if ($label !== null) {
  220. $fieldDescription->setOption('label', $label);
  221. }
  222. return $fieldDescription;
  223. }
  224. }