DatagridTest.php 20 KB

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