Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3405b24

Browse filesBrowse files
committed
test type hints on factory arguments
1 parent a6eef12 commit 3405b24
Copy full SHA for 3405b24

File tree

Expand file treeCollapse file tree

2 files changed

+64
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+64
-0
lines changed

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeHintsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeHintsPassTest.php
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,65 @@ public function testProcessFactoryWhithClassName()
296296
$this->assertInstanceOf(Bar::class, $container->get(Bar::class));
297297
}
298298

299+
/**
300+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
301+
* @expectedExceptionMessage In service declaration "bar". Trying to inject a "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeHintsPass\Foo" as argument 0 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeHintsPass\Foo::createBarArguments", but the type hint expects a "stdClass"
302+
*/
303+
public function testProcessFactoryFailsOnInvalidParameterType()
304+
{
305+
$container = new ContainerBuilder();
306+
307+
$container->register('foo', Foo::class);
308+
$container->register('bar', Bar::class)
309+
->addArgument(new Reference('foo'))
310+
->setFactory(array(
311+
new Reference('foo'),
312+
'createBarArguments',
313+
));
314+
315+
(new CheckTypeHintsPass(true))->process($container);
316+
}
317+
318+
/**
319+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
320+
* @expectedExceptionMessage In service declaration "bar". Trying to inject a "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeHintsPass\Foo" as argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeHintsPass\Foo::createBarArguments", but the type hint expects a "stdClass"
321+
*/
322+
public function testProcessFactoryFailsOnInvalidParameterTypeOptional()
323+
{
324+
$container = new ContainerBuilder();
325+
326+
$container->register('stdClass', \stdClass::class);
327+
$container->register('foo', Foo::class);
328+
$container->register('bar', Bar::class)
329+
->addArgument(new Reference('stdClass'))
330+
->addArgument(new Reference('foo'))
331+
->setFactory(array(
332+
new Reference('foo'),
333+
'createBarArguments',
334+
));
335+
336+
(new CheckTypeHintsPass(true))->process($container);
337+
}
338+
339+
public function testProcessFactorySuccessOnValidTypes()
340+
{
341+
$container = new ContainerBuilder();
342+
343+
$container->register('stdClass', \stdClass::class);
344+
$container->register('foo', Foo::class);
345+
$container->register('bar', Bar::class)
346+
->addArgument(new Reference('stdClass'))
347+
->addArgument(new Reference('stdClass'))
348+
->setFactory(array(
349+
new Reference('foo'),
350+
'createBarArguments',
351+
));
352+
353+
(new CheckTypeHintsPass(true))->process($container);
354+
355+
$this->addToAssertionCount(1);
356+
}
357+
299358
public function testProcessDoesNotLoadCodeByDefault()
300359
{
301360
$container = new ContainerBuilder();

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeHintsPass/Foo.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeHintsPass/Foo.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ public static function createBar()
88
{
99
return new Bar(new \stdClass());
1010
}
11+
12+
public static function createBarArguments(\stdClass $stdClass, \stdClass $stdClassOptional = null)
13+
{
14+
return new Bar($stdClass);
15+
}
1116
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.