From 97361f18157df0b4f1858c1a868bd977e4878aeb Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 4 Mar 2017 16:52:39 +0100 Subject: [PATCH] [Form] backport DependencyInjectionExtension tests --- .../DependencyInjectionExtensionTest.php | 89 +++++++++++++++++++ src/Symfony/Component/Form/composer.json | 1 + 2 files changed, 90 insertions(+) create mode 100644 src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php diff --git a/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php new file mode 100644 index 0000000000..22f3f9d8e3 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php @@ -0,0 +1,89 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension; + +class DependencyInjectionExtensionTest extends TestCase +{ + public function testGetTypeExtensions() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); + + $services = array( + 'extension1' => $typeExtension1 = $this->createFormTypeExtensionMock('test'), + 'extension2' => $typeExtension2 = $this->createFormTypeExtensionMock('test'), + 'extension3' => $typeExtension3 = $this->createFormTypeExtensionMock('other'), + ); + + $container->expects($this->any()) + ->method('get') + ->willReturnCallback(function ($id) use ($services) { + if (isset($services[$id])) { + return $services[$id]; + } + + throw new ServiceNotFoundException($id); + }); + + $extension = new DependencyInjectionExtension($container, array(), array('test' => array('extension1', 'extension2'), 'other' => array('extension3')), array()); + + $this->assertTrue($extension->hasTypeExtensions('test')); + $this->assertFalse($extension->hasTypeExtensions('unknown')); + $this->assertSame(array($typeExtension1, $typeExtension2), $extension->getTypeExtensions('test')); + } + + public function testThrowExceptionForInvalidExtendedType() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); + + $container->expects($this->any()) + ->method('get') + ->with('extension') + ->willReturn($this->createFormTypeExtensionMock('unmatched')); + + $extension = new DependencyInjectionExtension($container, array(), array('test' => array('extension')), array()); + + $extension->getTypeExtensions('test'); + } + + public function testGetTypeGuesser() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); + $container + ->expects($this->once()) + ->method('get') + ->with('foo') + ->willReturn($this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock()); + $extension = new DependencyInjectionExtension($container, array(), array(), array('foo')); + + $this->assertInstanceOf('Symfony\Component\Form\FormTypeGuesserChain', $extension->getTypeGuesser()); + } + + public function testGetTypeGuesserReturnsNullWhenNoTypeGuessersHaveBeenConfigured() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); + $extension = new DependencyInjectionExtension($container, array(), array(), array()); + + $this->assertNull($extension->getTypeGuesser()); + } + + private function createFormTypeExtensionMock($extendedType) + { + $extension = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock(); + $extension->expects($this->any())->method('getExtendedType')->willReturn($extendedType); + + return $extension; + } +} diff --git a/src/Symfony/Component/Form/composer.json b/src/Symfony/Component/Form/composer.json index bd3c2bcfaa..5b3af06994 100644 --- a/src/Symfony/Component/Form/composer.json +++ b/src/Symfony/Component/Form/composer.json @@ -25,6 +25,7 @@ "require-dev": { "doctrine/collections": "~1.0", "symfony/validator": "~2.7.25|^2.8.18", + "symfony/dependency-injection": "~2.7", "symfony/http-foundation": "~2.2", "symfony/http-kernel": "~2.4", "symfony/security-csrf": "~2.4",