DatagridTest.php 18 KB

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