FilterFactoryTest.php 3.0 KB

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