8000 minor #21868 [Form] backport DependencyInjectionExtension tests (xabbuh) · symfony/symfony@511e0c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 511e0c3

Browse files
committed
minor #21868 [Form] backport DependencyInjectionExtension tests (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] backport DependencyInjectionExtension tests | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- 97361f1 [Form] backport DependencyInjectionExtension tests
2 parents ea8025b + 97361f1 commit 511e0c3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"require-dev": {
2626
"doctrine/collections": "~1.0",
2727
"symfony/validator": "~2.7.25|^2.8.18",
28+
"symfony/dependency-injection": "~2.7",
2829
"symfony/http-foundation": "~2.2",
2930
"symfony/http-kernel": "~2.4",
3031
"symfony/security-csrf": "~2.4",

0 commit comments

Comments
 (0)
0