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