Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a470019

Browse files
committed
[DependencyInjection] Invokable Factory Services (Unit Tests)
1 parent 910b87d commit a470019

File tree

7 files changed

+28
-0
lines changed

7 files changed

+28
-0
lines changed

src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
1617

1718
class DefinitionTest extends TestCase
1819
{
@@ -35,6 +36,9 @@ public function testSetGetFactory()
3536

3637
$def->setFactory('Foo::bar');
3738
$this->assertEquals(['Foo', 'bar'], $def->getFactory(), '->setFactory() converts string static method call to the array');
39+
40+
$def->setFactory($ref = new Reference('baz'));
41+
$this->assertSame([$ref, '__invoke'], $def->getFactory(), '->setFactory() converts service reference to class invoke call');
3842
$this->assertSame(['factory' => true], $def->getChanges());
3943
}
4044

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services6.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
<service id="new_factory4" class="BazClass">
6060
<factory method="getInstance" />
6161
</service>
62+
<service id="new_factory5" class="FooBarClass">
63+
<factory service="baz" />
64+
</service>
6265
<service id="alias_for_foo" alias="foo" />
6366
<service id="another_alias_for_foo" alias="foo" public="false" />
6467
<service id="0" class="FooClass" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
factory:
3+
class: Baz
4+
invalid_factory:
5+
class: FooBarClass
6+
factory: '@factory:method'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
services:
22
factory: { class: FooBarClass, factory: baz:getClass}
33
factory_with_static_call: { class: FooBarClass, factory: FooBacFactory::createFooBar}
4+
invokable_factory: { class: FooBarClass, factory: '@factory' }

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ services:
3434
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
3535
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
3636
new_factory4: { class: BazClass, factory: [~, getInstance]}
37+
new_factory5: { class: FooBarClass, factory: '@baz' }
3738
Acme\WithShortCutArgs: [foo, '@baz']
3839
alias_for_foo: '@foo'
3940
another_alias_for_foo:

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public function testLoadServices()
267267
$this->assertEquals([new Reference('baz'), 'getClass'], $services['new_factory2']->getFactory(), '->load() parses the factory tag');
268268
$this->assertEquals(['BazClass', 'getInstance'], $services['new_factory3']->getFactory(), '->load() parses the factory tag');
269269
$this->assertSame([null, 'getInstance'], $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
270+
$this->assertEquals([new Reference('baz'), '__invoke'], $services['new_factory5']->getFactory(), '->load() accepts service reference as invokable factory');
270271

271272
$aliases = $container->getAliases();
272273
$this->assertArrayHasKey('alias_for_foo', $aliases, '->load() parses <service> elements');

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
2020
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2223
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2324
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2425
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -157,6 +158,7 @@ public function testLoadServices()
157158
$this->assertEquals([new Reference('baz'), 'getClass'], $services['new_factory2']->getFactory(), '->load() parses the factory tag');
158159
$this->assertEquals(['BazClass', 'getInstance'], $services['new_factory3']->getFactory(), '->load() parses the factory tag');
159160
$this->assertSame([null, 'getInstance'], $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
161+
$this->assertEquals([new Reference('baz'), '__invoke'], $services['new_factory5']->getFactory(), '->load() accepts service reference as invokable factory');
160162
$this->assertEquals(['foo', new Reference('baz')], $services['Acme\WithShortCutArgs']->getArguments(), '->load() parses short service definition');
161163

162164
$aliases = $container->getAliases();
@@ -195,6 +197,16 @@ public function testLoadFactoryShortSyntax()
195197

196198
$this->assertEquals([new Reference('baz'), 'getClass'], $services['factory']->getFactory(), '->load() parses the factory tag with service:method');
197199
$this->assertEquals(['FooBacFactory', 'createFooBar'], $services['factory_with_static_call']->getFactory(), '->load() parses the factory tag with Class::method');
200+
$this->assertEquals([new Reference('factory'), '__invoke'], $services['invokable_factory']->getFactory(), '->load() parses string service reference');
201+
}
202+
203+
public function testFactorySyntaxError()
204+
{
205+
$container = new ContainerBuilder();
206+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
207+
$this->expectException(InvalidArgumentException::class);
208+
$this->expectExceptionMessage('The value of the "factory" option for the "invalid_factory" service must be the id of the service without the "@" prefix (replace "@factory" with "factory").');
209+
$loader->load('bad_factory_syntax.yml');
198210
}
199211

200212
public function testLoadConfiguratorShortSyntax()

0 commit comments

Comments
 (0)