DependencyInjectionExtensionTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Form\Tests\Extension\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  13. use Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension;
  14. class DependencyInjectionExtensionTest extends TestCase
  15. {
  16. public function testGetTypeExtensions()
  17. {
  18. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  19. $typeExtension1 = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock();
  20. $typeExtension1->expects($this->any())
  21. ->method('getExtendedType')
  22. ->willReturn('test');
  23. $typeExtension2 = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock();
  24. $typeExtension2->expects($this->any())
  25. ->method('getExtendedType')
  26. ->willReturn('test');
  27. $typeExtension3 = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock();
  28. $typeExtension3->expects($this->any())
  29. ->method('getExtendedType')
  30. ->willReturn('other');
  31. $services = array(
  32. 'extension1' => $typeExtension1 = $this->createFormTypeExtensionMock('test'),
  33. 'extension2' => $typeExtension2 = $this->createFormTypeExtensionMock('test'),
  34. 'extension3' => $typeExtension3 = $this->createFormTypeExtensionMock('other'),
  35. );
  36. $container->expects($this->any())
  37. ->method('get')
  38. ->willReturnCallback(function ($id) use ($services) {
  39. if (isset($services[$id])) {
  40. return $services[$id];
  41. }
  42. throw new ServiceNotFoundException($id);
  43. });
  44. $extension = new DependencyInjectionExtension($container, array(), array('test' => array('extension1', 'extension2'), 'other' => array('extension3')), array());
  45. $this->assertTrue($extension->hasTypeExtensions('test'));
  46. $this->assertFalse($extension->hasTypeExtensions('unknown'));
  47. $this->assertSame(array($typeExtension1, $typeExtension2), $extension->getTypeExtensions('test'));
  48. }
  49. /**
  50. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  51. */
  52. public function testThrowExceptionForInvalidExtendedType()
  53. {
  54. $formTypeExtension = $this->createFormTypeExtensionMock('unmatched');
  55. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  56. $container->expects($this->any())
  57. ->method('get')
  58. ->with('extension')
  59. ->willReturn($formTypeExtension);
  60. $extension = new DependencyInjectionExtension($container, array(), array('test' => array('extension')), array());
  61. $extensions = $extension->getTypeExtensions('test');
  62. $this->assertCount(1, $extensions);
  63. $this->assertSame($formTypeExtension, $extensions[0]);
  64. }
  65. public function testGetTypeGuesser()
  66. {
  67. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  68. $container
  69. ->expects($this->once())
  70. ->method('get')
  71. ->with('foo')
  72. ->willReturn($this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock());
  73. $extension = new DependencyInjectionExtension($container, array(), array(), array('foo'));
  74. $this->assertInstanceOf('Symfony\Component\Form\FormTypeGuesserChain', $extension->getTypeGuesser());
  75. }
  76. public function testGetTypeGuesserReturnsNullWhenNoTypeGuessersHaveBeenConfigured()
  77. {
  78. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  79. $extension = new DependencyInjectionExtension($container, array(), array(), array());
  80. $this->assertNull($extension->getTypeGuesser());
  81. }
  82. private function createFormTypeExtensionMock($extendedType)
  83. {
  84. $extension = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock();
  85. $extension->expects($this->any())->method('getExtendedType')->willReturn($extendedType);
  86. return $extension;
  87. }
  88. }