SearchHandlerTest.php 3.0 KB

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