DatagridTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 testHasDisplayableFilters()
  185. {
  186. $this->assertFalse($this->datagrid->hasDisplayableFilters());
  187. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  188. $filter1->expects($this->once())
  189. ->method('getName')
  190. ->will($this->returnValue('foo'));
  191. $filter1->expects($this->any())
  192. ->method('getOption')
  193. ->will($this->returnValue(false));
  194. $filter1->expects($this->any())
  195. ->method('isActive')
  196. ->will($this->returnValue(false));
  197. $this->datagrid->addFilter($filter1);
  198. $this->assertFalse($this->datagrid->hasDisplayableFilters());
  199. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  200. $filter2->expects($this->once())
  201. ->method('getName')
  202. ->will($this->returnValue('bar'));
  203. $filter2->expects($this->any())
  204. ->method('getOption')
  205. ->will($this->returnValue(true));
  206. $filter2->expects($this->any())
  207. ->method('isActive')
  208. ->will($this->returnValue(true));
  209. $this->datagrid->addFilter($filter2);
  210. $this->assertTrue($this->datagrid->hasDisplayableFilters());
  211. }
  212. public function testGetForm()
  213. {
  214. $this->assertInstanceOf('Symfony\Component\Form\Form', $this->datagrid->getForm());
  215. }
  216. public function testGetResults()
  217. {
  218. $this->assertEquals(null, $this->datagrid->getResults());
  219. $this->pager->expects($this->once())
  220. ->method('getResults')
  221. ->will($this->returnValue(array('foo', 'bar')));
  222. $this->assertEquals(array('foo', 'bar'), $this->datagrid->getResults());
  223. }
  224. public function testBuildPager()
  225. {
  226. $filter1 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  227. $filter1->expects($this->once())
  228. ->method('getName')
  229. ->will($this->returnValue('foo'));
  230. $filter1->expects($this->any())
  231. ->method('getFormName')
  232. ->will($this->returnValue('fooFormName'));
  233. $filter1->expects($this->any())
  234. ->method('isActive')
  235. ->will($this->returnValue(false));
  236. $filter1->expects($this->any())
  237. ->method('getRenderSettings')
  238. ->will($this->returnValue(array('foo1', array('bar1'=>'baz1'))));
  239. $this->datagrid->addFilter($filter1);
  240. $filter2 = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  241. $filter2->expects($this->once())
  242. ->method('getName')
  243. ->will($this->returnValue('bar'));
  244. $filter2->expects($this->any())
  245. ->method('getFormName')
  246. ->will($this->returnValue('barFormName'));
  247. $filter2->expects($this->any())
  248. ->method('isActive')
  249. ->will($this->returnValue(true));
  250. $filter2->expects($this->any())
  251. ->method('getRenderSettings')
  252. ->will($this->returnValue(array('foo2', array('bar2'=>'baz2'))));
  253. $this->datagrid->addFilter($filter2);
  254. $this->datagrid->buildPager();
  255. $this->assertEquals(array('foo'=>null, 'bar'=> null), $this->datagrid->getValues());
  256. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  257. $this->assertEquals(array('bar1' => 'baz1'), $this->formBuilder->get('fooFormName')->getOptions());
  258. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('barFormName'));
  259. $this->assertEquals(array('bar2' => 'baz2'), $this->formBuilder->get('barFormName')->getOptions());
  260. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  261. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  262. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  263. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  264. }
  265. /**
  266. * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
  267. * @expectedExceptionMessage Expected argument of type "FieldDescriptionInterface", "array" given
  268. */
  269. public function testBuildPagerWithException()
  270. {
  271. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  272. $filter->expects($this->once())
  273. ->method('getName')
  274. ->will($this->returnValue('foo'));
  275. $filter->expects($this->any())
  276. ->method('isActive')
  277. ->will($this->returnValue(false));
  278. $filter->expects($this->any())
  279. ->method('getRenderSettings')
  280. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  281. $this->datagrid->addFilter($filter);
  282. $this->datagrid->setValue('_sort_by', 'foo', 'baz');
  283. $this->datagrid->buildPager();
  284. }
  285. public function testBuildPagerWithSortBy()
  286. {
  287. $sortBy = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  288. $sortBy->expects($this->once())
  289. ->method('isSortable')
  290. ->will($this->returnValue(true));
  291. $this->pager->expects($this->once())
  292. ->method('setMaxPerPage')
  293. ->with($this->equalTo('25'))
  294. ->will($this->returnValue(null));
  295. $this->pager->expects($this->once())
  296. ->method('setPage')
  297. ->with($this->equalTo('1'))
  298. ->will($this->returnValue(null));
  299. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array('_sort_by'=>$sortBy));
  300. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  301. $filter->expects($this->once())
  302. ->method('getName')
  303. ->will($this->returnValue('foo'));
  304. $filter->expects($this->any())
  305. ->method('getFormName')
  306. ->will($this->returnValue('fooFormName'));
  307. $filter->expects($this->any())
  308. ->method('isActive')
  309. ->will($this->returnValue(false));
  310. $filter->expects($this->any())
  311. ->method('getRenderSettings')
  312. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  313. $this->datagrid->addFilter($filter);
  314. $this->datagrid->buildPager();
  315. $this->assertEquals(array('_sort_by'=>$sortBy, 'foo'=>null), $this->datagrid->getValues());
  316. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  317. $this->assertEquals(array('bar' => 'baz'), $this->formBuilder->get('fooFormName')->getOptions());
  318. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  319. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  320. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  321. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  322. }
  323. /**
  324. * @dataProvider getBuildPagerWithPageTests
  325. */
  326. public function testBuildPagerWithPage($page, $perPage)
  327. {
  328. $sortBy = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  329. $sortBy->expects($this->once())
  330. ->method('isSortable')
  331. ->will($this->returnValue(true));
  332. $this->pager->expects($this->once())
  333. ->method('setMaxPerPage')
  334. ->with($this->equalTo('50'))
  335. ->will($this->returnValue(null));
  336. $this->pager->expects($this->once())
  337. ->method('setPage')
  338. ->with($this->equalTo('3'))
  339. ->will($this->returnValue(null));
  340. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array('_sort_by'=>$sortBy, '_page'=>$page, '_per_page'=>$perPage));
  341. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  342. $filter->expects($this->once())
  343. ->method('getName')
  344. ->will($this->returnValue('foo'));
  345. $filter->expects($this->any())
  346. ->method('getFormName')
  347. ->will($this->returnValue('fooFormName'));
  348. $filter->expects($this->any())
  349. ->method('isActive')
  350. ->will($this->returnValue(false));
  351. $filter->expects($this->any())
  352. ->method('getRenderSettings')
  353. ->will($this->returnValue(array('foo', array('bar'=>'baz'))));
  354. $this->datagrid->addFilter($filter);
  355. $this->datagrid->buildPager();
  356. $this->assertEquals(array('_sort_by'=>$sortBy, 'foo'=>null, '_page' => 3, '_per_page' => 50), $this->datagrid->getValues());
  357. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('fooFormName'));
  358. $this->assertEquals(array('bar' => 'baz'), $this->formBuilder->get('fooFormName')->getOptions());
  359. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  360. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  361. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  362. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  363. }
  364. public function getBuildPagerWithPageTests()
  365. {
  366. // tests for php 5.3, because isset functionality was changed since php 5.4
  367. return array(
  368. array(3, 50),
  369. array('3', '50'),
  370. array(3, '50'),
  371. array('3', 50),
  372. );
  373. }
  374. /**
  375. * @dataProvider getBuildPagerWithPage2Tests
  376. */
  377. public function testBuildPagerWithPage2($page, $perPage)
  378. {
  379. $this->pager->expects($this->once())
  380. ->method('setMaxPerPage')
  381. ->with($this->equalTo('50'))
  382. ->will($this->returnValue(null));
  383. $this->pager->expects($this->once())
  384. ->method('setPage')
  385. ->with($this->equalTo('3'))
  386. ->will($this->returnValue(null));
  387. $this->datagrid = new Datagrid($this->query, $this->columns, $this->pager, $this->formBuilder, array());
  388. $this->datagrid->setValue('_per_page', null, $perPage);
  389. $this->datagrid->setValue('_page', null, $page);
  390. $this->datagrid->buildPager();
  391. $this->assertEquals(array('_page' => array('type'=>null, 'value'=>3), '_per_page' => array('type'=>null, 'value'=>50)), $this->datagrid->getValues());
  392. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_by'));
  393. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_sort_order'));
  394. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_page'));
  395. $this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->formBuilder->get('_per_page'));
  396. }
  397. public function getBuildPagerWithPage2Tests()
  398. {
  399. // tests for php 5.3, because isset functionality was changed since php 5.4
  400. return array(
  401. array(3, 50),
  402. array('3', '50'),
  403. array(3, '50'),
  404. array('3', 50),
  405. );
  406. }
  407. }