DatagridTest.php 20 KB

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