DatagridTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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\Datagrid;
  13. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  15. use Sonata\AdminBundle\Datagrid\PagerInterface;
  16. use Sonata\AdminBundle\Filter\FilterInterface;
  17. use Symfony\Component\Form\FormBuilder;
  18. /**
  19. * @author Andrej Hudec <pulzarraider@gmail.com>
  20. */
  21. class DatagridTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /**
  24. * @var Datagrid
  25. */
  26. private $datagrid;
  27. /**
  28. * @var PagerInterface
  29. */
  30. private $pager;
  31. /**
  32. * @var ProxyQueryInterface
  33. */
  34. private $query;
  35. /**
  36. * @var FormBuilder
  37. */
  38. private $formBuilder;
  39. /**
  40. * @var array
  41. */
  42. private $formTypes;
  43. public function setUp()
  44. {
  45. $this->query = $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  46. $this->columns = new FieldDescriptionCollection();
  47. $this->pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
  48. $this->formTypes = array();
  49. // php 5.3 BC
  50. $formTypes = &$this->formTypes;
  51. $this->formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->formBuilder->expects($this->any())
  55. ->method('get')
  56. ->will($this->returnCallback(function ($name) use (& $formTypes) {
  57. if (isset($formTypes[$name])) {
  58. return $formTypes[$name];
  59. }
  60. return null;
  61. }));
  62. // php 5.3 BC
  63. $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  64. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  65. $this->formBuilder->expects($this->any())
  66. ->method('add')
  67. ->will($this->returnCallback(function ($name, $type, $options) use (& $formTypes, $eventDispatcher, $formFactory) {
  68. $formTypes[$name] = new FormBuilder($name, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\TestEntity', $eventDispatcher, $formFactory, $options);
  69. return null;
  70. }));
  71. // php 5.3 BC
  72. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->formBuilder->expects($this->any())
  76. ->method('getForm')
  77. ->will($this->returnCallback(function () use ($form) {
  78. return $form;
  79. }));
  80. $values = array();
  81. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, $values);
  82. }
  83. public function testGetPager()
  84. {
  85. $this->assertEquals($this->pager, $this->datagrid->getPager());
  86. }
  87. public function testFilter()
  88. {
  89. $this->assertFalse($this->datagrid->hasFilter('foo'));
  90. $this->assertNull($this->datagrid->getFilter('foo'));
  91. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  92. $filter->expects($this->once())
  93. ->method('getName')
  94. ->will($this->returnValue('foo'));
  95. $this->datagrid->addFilter($filter);
  96. $this->assertTrue($this->datagrid->hasFilter('foo'));
  97. $this->assertFalse($this->datagrid->hasFilter('nonexistent'));
  98. $this->assertEquals($filter, $this->datagrid->getFilter('foo'));
  99. $this->datagrid->removeFilter('foo');
  100. $this->assertFalse($this->datagrid->hasFilter('foo'));
  101. }
  102. public function testGetFilters()
  103. {
  104. $this->assertEquals(array(), $this->datagrid->getFilters());
  105. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  106. $filter1->expects($this->once())
  107. ->method('getName')
  108. ->will($this->returnValue('foo'));
  109. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  110. $filter2->expects($this->once())
  111. ->method('getName')
  112. ->will($this->returnValue('bar'));
  113. $filter3 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  114. $filter3->expects($this->once())
  115. ->method('getName')
  116. ->will($this->returnValue('baz'));
  117. $this->datagrid->addFilter($filter1);
  118. $this->datagrid->addFilter($filter2);
  119. $this->datagrid->addFilter($filter3);
  120. $this->assertEquals(array('foo'=>$filter1, 'bar'=>$filter2, 'baz'=>$filter3), $this->datagrid->getFilters());
  121. $this->datagrid->removeFilter('bar');
  122. $this->assertEquals(array('foo'=>$filter1, 'baz'=>$filter3), $this->datagrid->getFilters());
  123. }
  124. public function testReorderFilters()
  125. {
  126. $this->assertEquals(array(), $this->datagrid->getFilters());
  127. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  128. $filter1->expects($this->once())
  129. ->method('getName')
  130. ->will($this->returnValue('foo'));
  131. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  132. $filter2->expects($this->once())
  133. ->method('getName')
  134. ->will($this->returnValue('bar'));
  135. $filter3 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  136. $filter3->expects($this->once())
  137. ->method('getName')
  138. ->will($this->returnValue('baz'));
  139. $this->datagrid->addFilter($filter1);
  140. $this->datagrid->addFilter($filter2);
  141. $this->datagrid->addFilter($filter3);
  142. $this->assertEquals(array('foo'=>$filter1, 'bar'=>$filter2, 'baz'=>$filter3), $this->datagrid->getFilters());
  143. $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($this->datagrid->getFilters()));
  144. $this->datagrid->reorderFilters(array('bar', 'baz', 'foo'));
  145. $this->assertEquals(array('bar'=>$filter2, 'baz'=>$filter3, 'foo'=>$filter1), $this->datagrid->getFilters());
  146. $this->assertEquals(array('bar', 'baz', 'foo'), array_keys($this->datagrid->getFilters()));
  147. }
  148. public function testGetValues()
  149. {
  150. $this->assertEquals(array(), $this->datagrid->getValues());
  151. $this->datagrid->setValue('foo', 'bar', 'baz');
  152. $this->assertEquals(array('foo'=>array('type'=>'bar', 'value'=>'baz')), $this->datagrid->getValues());
  153. }
  154. public function testGetColumns()
  155. {
  156. $this->assertEquals($this->columns, $this->datagrid->getColumns());
  157. }
  158. public function testGetQuery()
  159. {
  160. $this->assertEquals($this->query, $this->datagrid->getQuery());
  161. }
  162. public function testHasActiveFilters()
  163. {
  164. $this->assertFalse($this->datagrid->hasActiveFilters());
  165. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  166. $filter1->expects($this->once())
  167. ->method('getName')
  168. ->will($this->returnValue('foo'));
  169. $filter1->expects($this->any())
  170. ->method('isActive')
  171. ->will($this->returnValue(false));
  172. $this->datagrid->addFilter($filter1);
  173. $this->assertFalse($this->datagrid->hasActiveFilters());
  174. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  175. $filter2->expects($this->once())
  176. ->method('getName')
  177. ->will($this->returnValue('bar'));
  178. $filter2->expects($this->any())
  179. ->method('isActive')
  180. ->will($this->returnValue(true));
  181. $this->datagrid->addFilter($filter2);
  182. $this->assertTrue($this->datagrid->hasActiveFilters());
  183. }
  184. public function testGetForm()
  185. {
  186. $this->assertInstanceOf('Symfony\Component\Form\Form', $this->datagrid->getForm());
  187. }
  188. public function testGetResults()
  189. {
  190. $this->assertEquals(null, $this->datagrid->getResults());
  191. $this->pager->expects($this->once())
  192. ->method('getResults')
  193. ->will($this->returnValue(array('foo', 'bar')));
  194. $this->assertEquals(array('foo', 'bar'), $this->datagrid->getResults());
  195. }
  196. public function testBuildPager()
  197. {
  198. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  199. $filter1->expects($this->once())
  200. ->method('getName')
  201. ->will($this->returnValue('foo'));
  202. $filter1->expects($this->any())
  203. ->method('getFormName')
  204. ->will($this->returnValue('fooFormName'));
  205. $filter1->expects($this->any())
  206. ->method('isActive')
  207. ->will($this->returnValue(false));
  208. $filter1->expects($this->any())
  209. ->method('getRenderSettings')
  210. ->will($this->returnValue(array('foo1', array('bar1'=>'baz1'))));
  211. $this->datagrid->addFilter($filter1);
  212. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  213. $filter2->expects($this->once())
  214. ->method('getName')
  215. ->will($this->returnValue('bar'));
  216. $filter2->expects($this->any())
  217. ->method('getFormName')
  218. ->will($this->returnValue('barFormName'));
  219. $filter2->expects($this->any())
  220. ->method('isActive')
  221. ->will($this->returnValue(true));
  222. $filter2->expects($this->any())
  223. ->method('getRenderSettings')
  224. ->will($this->returnValue(array('foo2', array('bar2'=>'baz2'))));
  225. $this->datagrid->addFilter($filter2);
  226. $this->datagrid->buildPager();
  227. $this->assertEquals(array('foo'=>null, 'bar'=> null), $this->datagrid->getValues());
  228. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  229. $this->assertEquals(array('bar1' => 'baz1'), $this->formBuilder->get('fooFormName')->getOptions());
  230. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('barFormName'));
  231. $this->assertEquals(array('bar2' => 'baz2'), $this->formBuilder->get('barFormName')->getOptions());
  232. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  233. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  234. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  235. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  236. }
  237. /**
  238. * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
  239. * @expectedExceptionMessage Expected argument of type "FieldDescriptionInterface", "array" given
  240. */
  241. public function testBuildPagerWithException()
  242. {
  243. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  244. $filter->expects($this->once())
  245. ->method('getName')
  246. ->will($this->returnValue('foo'));
  247. $filter->expects($this->any())
  248. ->method('isActive')
  249. ->will($this->returnValue(false));
  250. $filter->expects($this->any())
  251. ->method('getRenderSettings')
  252. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  253. $this->datagrid->addFilter($filter);
  254. $this->datagrid->setValue('_sort_by', 'foo', 'baz');
  255. $this->datagrid->buildPager();
  256. }
  257. public function testBuildPagerWithSortBy()
  258. {
  259. $sortBy = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  260. $sortBy->expects($this->once())
  261. ->method('isSortable')
  262. ->will($this->returnValue(true));
  263. $this->pager->expects($this->once())
  264. ->method('setMaxPerPage')
  265. ->with($this->equalTo('25'))
  266. ->will($this->returnValue(null));
  267. $this->pager->expects($this->once())
  268. ->method('setPage')
  269. ->with($this->equalTo('1'))
  270. ->will($this->returnValue(null));
  271. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array('_sort_by'=>$sortBy));
  272. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  273. $filter->expects($this->once())
  274. ->method('getName')
  275. ->will($this->returnValue('foo'));
  276. $filter->expects($this->any())
  277. ->method('getFormName')
  278. ->will($this->returnValue('fooFormName'));
  279. $filter->expects($this->any())
  280. ->method('isActive')
  281. ->will($this->returnValue(false));
  282. $filter->expects($this->any())
  283. ->method('getRenderSettings')
  284. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  285. $this->datagrid->addFilter($filter);
  286. $this->datagrid->buildPager();
  287. $this->assertEquals(array('_sort_by'=>$sortBy, 'foo'=>null), $this->datagrid->getValues());
  288. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  289. $this->assertEquals(array('bar' => 'baz'), $this->formBuilder->get('fooFormName')->getOptions());
  290. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  291. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  292. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  293. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  294. }
  295. /**
  296. * @dataProvider getBuildPagerWithPageTests
  297. */
  298. public function testBuildPagerWithPage($page, $perPage)
  299. {
  300. $sortBy = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  301. $sortBy->expects($this->once())
  302. ->method('isSortable')
  303. ->will($this->returnValue(true));
  304. $this->pager->expects($this->once())
  305. ->method('setMaxPerPage')
  306. ->with($this->equalTo('50'))
  307. ->will($this->returnValue(null));
  308. $this->pager->expects($this->once())
  309. ->method('setPage')
  310. ->with($this->equalTo('3'))
  311. ->will($this->returnValue(null));
  312. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array('_sort_by'=>$sortBy, '_page'=>$page, '_per_page'=>$perPage));
  313. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  314. $filter->expects($this->once())
  315. ->method('getName')
  316. ->will($this->returnValue('foo'));
  317. $filter->expects($this->any())
  318. ->method('getFormName')
  319. ->will($this->returnValue('fooFormName'));
  320. $filter->expects($this->any())
  321. ->method('isActive')
  322. ->will($this->returnValue(false));
  323. $filter->expects($this->any())
  324. ->method('getRenderSettings')
  325. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  326. $this->datagrid->addFilter($filter);
  327. $this->datagrid->buildPager();
  328. $this->assertEquals(array('_sort_by'=>$sortBy, 'foo'=>null, '_page' => 3, '_per_page' => 50), $this->datagrid->getValues());
  329. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  330. $this->assertEquals(array('bar' => 'baz'), $this->formBuilder->get('fooFormName')->getOptions());
  331. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  332. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  333. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  334. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  335. }
  336. public function getBuildPagerWithPageTests()
  337. {
  338. // tests for php 5.3, because isset functionality was changed since php 5.4
  339. return array(
  340. array(3, 50),
  341. array('3', '50'),
  342. array(3, '50'),
  343. array('3', 50),
  344. );
  345. }
  346. /**
  347. * @dataProvider getBuildPagerWithPage2Tests
  348. */
  349. public function testBuildPagerWithPage2($page, $perPage)
  350. {
  351. $this->pager->expects($this->once())
  352. ->method('setMaxPerPage')
  353. ->with($this->equalTo('50'))
  354. ->will($this->returnValue(null));
  355. $this->pager->expects($this->once())
  356. ->method('setPage')
  357. ->with($this->equalTo('3'))
  358. ->will($this->returnValue(null));
  359. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array());
  360. $this->datagrid->setValue('_per_page', null, $perPage);
  361. $this->datagrid->setValue('_page', null, $page);
  362. $this->datagrid->buildPager();
  363. $this->assertEquals(array('_page' => array('type'=>null, 'value'=>3), '_per_page' => array('type'=>null, 'value'=>50)), $this->datagrid->getValues());
  364. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  365. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  366. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  367. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  368. }
  369. public function getBuildPagerWithPage2Tests()
  370. {
  371. // tests for php 5.3, because isset functionality was changed since php 5.4
  372. return array(
  373. array(3, 50),
  374. array('3', '50'),
  375. array(3, '50'),
  376. array('3', 50),
  377. );
  378. }
  379. }