DatagridMapperTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. ), $filter->getOptions());
  100. }
  101. public function testGet2()
  102. {
  103. $this->assertFalse($this->datagridMapper->has('fooName'));
  104. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  105. $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'));
  106. $filter = $this->datagridMapper->get('fooName');
  107. $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
  108. $this->assertEquals('fooName', $filter->getName());
  109. $this->assertEquals('fooName', $filter->getFormName());
  110. $this->assertEquals('foo_field_type', $filter->getFieldType());
  111. $this->assertEquals('fooLabel', $filter->getLabel());
  112. $this->assertEquals(array('foo_field_option'=>'baz'), $filter->getFieldOptions());
  113. $this->assertEquals(array(
  114. 'foo_default_option' => 'bar_custom',
  115. 'label' => 'fooLabel',
  116. 'field_name' => 'fooFilterName',
  117. 'field_options' => array ('foo_field_option' => 'baz'),
  118. 'field_type' => 'foo_field_type',
  119. 'placeholder' => 'short_object_description_placeholder',
  120. 'foo_filter_option' => 'foo_filter_option_value',
  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->assertEquals('fooName', $fieldDescription->getName());
  130. }
  131. public function testAddRemove()
  132. {
  133. $this->assertFalse($this->datagridMapper->has('fooName'));
  134. $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
  135. $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName'));
  136. $this->assertTrue($this->datagridMapper->has('fooName'));
  137. $this->datagridMapper->remove('fooName');
  138. $this->assertFalse($this->datagridMapper->has('fooName'));
  139. }
  140. public function testAddException()
  141. {
  142. try {
  143. $this->datagridMapper->add(12345);
  144. } catch (\RuntimeException $e) {
  145. $this->assertContains('invalid state', $e->getMessage());
  146. return;
  147. }
  148. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  149. }
  150. public function testAddException2()
  151. {
  152. try {
  153. $this->datagridMapper->getAdmin()
  154. ->expects($this->any())
  155. ->method('hasFilterFieldDescription')
  156. ->will($this->returnValue(true))
  157. ;
  158. $this->datagridMapper->add('field');
  159. } catch (\RuntimeException $e) {
  160. $this->assertContains('The field "field" is already defined', $e->getMessage());
  161. return;
  162. }
  163. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  164. }
  165. public function testReorder()
  166. {
  167. $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
  168. $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
  169. $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
  170. $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
  171. $this->datagridMapper->add($fieldDescription1, null, array('field_name'=>'fooFilterName1'));
  172. $this->datagridMapper->add($fieldDescription2, null, array('field_name'=>'fooFilterName2'));
  173. $this->datagridMapper->add($fieldDescription3, null, array('field_name'=>'fooFilterName3'));
  174. $this->datagridMapper->add($fieldDescription4, null, array('field_name'=>'fooFilterName4'));
  175. $this->assertEquals(array(
  176. 'fooName1',
  177. 'fooName2',
  178. 'fooName3',
  179. 'fooName4',
  180. ), array_keys($this->datagrid->getFilters()));
  181. $this->datagridMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
  182. $this->assertEquals(array(
  183. 'fooName3',
  184. 'fooName2',
  185. 'fooName1',
  186. 'fooName4',
  187. ), array_keys($this->datagrid->getFilters()));
  188. }
  189. private function getFieldDescriptionMock($name=null, $label=null)
  190. {
  191. $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
  192. if ($name !== null) {
  193. $fieldDescription->setName($name);
  194. }
  195. if ($label !== null) {
  196. $fieldDescription->setOption('label', $label);
  197. }
  198. return $fieldDescription;
  199. }
  200. }