DatagridTest.php 20 KB

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