SearchHandlerTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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\Search;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Sonata\AdminBundle\Search\SearchHandler;
  14. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  15. use Sonata\AdminBundle\Filter\FilterInterface;
  16. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  17. class SearchHandlerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @param AdminInterface $admin
  21. *
  22. * @return Pool
  23. */
  24. public function getPool(AdminInterface $admin = null)
  25. {
  26. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  27. $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($admin) {
  28. if ($id == 'fake') {
  29. throw new ServiceNotFoundException('Fake service does not exist');
  30. }
  31. return $admin;
  32. }));
  33. return new Pool($container, 'title', 'logo', array('asd'));
  34. }
  35. public function testBuildPagerWithNoGlobalSearchField()
  36. {
  37. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  38. $filter->expects($this->once())->method('getOption')->will($this->returnValue(false));
  39. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  40. $datagrid->expects($this->once())->method('getFilters')->will($this->returnValue(array($filter)));
  41. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  42. $admin->expects($this->once())->method('getDatagrid')->will($this->returnValue($datagrid));
  43. $handler = new SearchHandler($this->getPool($admin));
  44. $this->assertFalse($handler->search($admin, 'myservice'));
  45. }
  46. public function testBuildPagerWithGlobalSearchField()
  47. {
  48. $filter = $this->getMock('Sonata\AdminBundle\Filter\FilterInterface');
  49. $filter->expects($this->once())->method('getOption')->will($this->returnValue(true));
  50. $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
  51. $pager->expects($this->once())->method('setPage');
  52. $pager->expects($this->once())->method('setMaxPerPage');
  53. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  54. $datagrid->expects($this->once())->method('getFilters')->will($this->returnValue(array($filter)));
  55. $datagrid->expects($this->once())->method('setValue');
  56. $datagrid->expects($this->once())->method('getPager')->will($this->returnValue($pager));
  57. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  58. $admin->expects($this->once())->method('getDatagrid')->will($this->returnValue($datagrid));
  59. $handler = new SearchHandler($this->getPool($admin));
  60. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\PagerInterface', $handler->search($admin, 'myservice'));
  61. }
  62. }