FilterFactoryTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Filter;
  11. use Sonata\AdminBundle\Filter\FilterFactory;
  12. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  13. class FilterFactoryTest extends PHPUnit_Framework_TestCase
  14. {
  15. public function testEmptyType()
  16. {
  17. $this->expectException('\RuntimeException', 'The type must be defined');
  18. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  19. $filter = new FilterFactory($container, array());
  20. $filter->create('test', null);
  21. }
  22. public function testUnknownType()
  23. {
  24. $this->expectException('\RuntimeException', 'No attached service to type named `mytype`');
  25. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  26. $filter = new FilterFactory($container, array());
  27. $filter->create('test', 'mytype');
  28. }
  29. public function testUnknownClassType()
  30. {
  31. $this->expectException('\RuntimeException', 'No attached service to type named `Sonata\AdminBundle\Form\Type\Filter\FooType`');
  32. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  33. $filter = new FilterFactory($container, array());
  34. $filter->create('test', 'Sonata\AdminBundle\Form\Type\Filter\FooType');
  35. }
  36. public function testClassType()
  37. {
  38. $this->expectException('\RuntimeException', 'The service `Sonata\AdminBundle\Form\Type\Filter\DefaultType` must implement `FilterInterface`');
  39. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  40. $filter = new FilterFactory($container, array());
  41. $filter->create('test', 'Sonata\AdminBundle\Form\Type\Filter\DefaultType');
  42. }
  43. public function testInvalidTypeInstance()
  44. {
  45. $this->expectException('\RuntimeException', 'The service `mytype` must implement `FilterInterface`');
  46. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  47. $container->expects($this->once())
  48. ->method('get')
  49. ->will($this->returnValue(false));
  50. $filter = new FilterFactory($container, array('mytype' => 'mytype'));
  51. $filter->create('test', 'mytype');
  52. }
  53. public function testCreateFilter()
  54. {
  55. $filter = $this->getMockForAbstractClass('Sonata\AdminBundle\Filter\FilterInterface');
  56. $filter->expects($this->once())
  57. ->method('initialize');
  58. $container = $this->getMockForAbstractClass('Symfony\Component\DependencyInjection\ContainerInterface');
  59. $container->expects($this->once())
  60. ->method('get')
  61. ->will($this->returnValue($filter));
  62. $filter = new FilterFactory($container, array('mytype' => 'mytype'));
  63. $filter->create('test', 'mytype');
  64. }
  65. }