Skip to content

Commit 38c48ef

Browse files
kbondnicolas-grekas
authored andcommitted
[DependencyInjection] Add tests for AutowireLocator/AutowireIterator
1 parent 8ef38b3 commit 38c48ef

File tree

7 files changed

+111
-14
lines changed

7 files changed

+111
-14
lines changed

src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Container\ContainerInterface;
1616
use Symfony\Component\Config\FileLocator;
1717
use Symfony\Component\DependencyInjection\Alias;
18+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
1819
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1920
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
2021
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -32,6 +33,7 @@
3233
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredInterface2;
3334
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredService1;
3435
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredService2;
36+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutowireIteratorConsumer;
3537
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutowireLocatorConsumer;
3638
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
3739
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
@@ -392,6 +394,7 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
392394
public function testLocatorConfiguredViaAttribute()
393395
{
394396
$container = new ContainerBuilder();
397+
$container->setParameter('some.parameter', 'foo');
395398
$container->register(BarTagClass::class)
396399
->setPublic(true)
397400
;
@@ -411,6 +414,36 @@ public function testLocatorConfiguredViaAttribute()
411414
self::assertSame($container->get(BarTagClass::class), $s->locator->get(BarTagClass::class));
412415
self::assertSame($container->get(FooTagClass::class), $s->locator->get('with_key'));
413416
self::assertFalse($s->locator->has('nullable'));
417+
self::assertSame('foo', $s->locator->get('subscribed'));
418+
}
419+
420+
public function testIteratorConfiguredViaAttribute()
421+
{
422+
$container = new ContainerBuilder();
423+
$container->setParameter('some.parameter', 'foo');
424+
$container->register(BarTagClass::class)
425+
->setPublic(true)
426+
;
427+
$container->register(FooTagClass::class)
428+
->setPublic(true)
429+
;
430+
$container->register(AutowireIteratorConsumer::class)
431+
->setAutowired(true)
432+
->setPublic(true)
433+
;
434+
435+
$container->compile();
436+
437+
/** @var AutowireIteratorConsumer $s */
438+
$s = $container->get(AutowireIteratorConsumer::class);
439+
440+
self::assertInstanceOf(RewindableGenerator::class, $s->iterator);
441+
442+
$values = iterator_to_array($s->iterator);
443+
self::assertCount(3, $values);
444+
self::assertSame($container->get(BarTagClass::class), $values[BarTagClass::class]);
445+
self::assertSame($container->get(FooTagClass::class), $values['with_key']);
446+
self::assertSame('foo', $values['subscribed']);
414447
}
415448

416449
public function testTaggedServiceWithIndexAttributeAndDefaultMethodConfiguredViaAttribute()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\DependencyInjection\Tests\Fixtures;
13+
14+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
16+
use Symfony\Contracts\Service\Attribute\SubscribedService;
17+
18+
final class AutowireIteratorConsumer
19+
{
20+
public function __construct(
21+
#[AutowireIterator([
22+
BarTagClass::class,
23+
'with_key' => FooTagClass::class,
24+
'nullable' => '?invalid',
25+
'subscribed' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')),
26+
])]
27+
public readonly iterable $iterator,
28+
) {
29+
}
30+
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/AutowireLocatorConsumer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1516
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
17+
use Symfony\Contracts\Service\Attribute\SubscribedService;
1618

1719
final class AutowireLocatorConsumer
1820
{
@@ -21,6 +23,7 @@ public function __construct(
2123
BarTagClass::class,
2224
'with_key' => FooTagClass::class,
2325
'nullable' => '?invalid',
26+
'subscribed' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')),
2427
])]
2528
public readonly ContainerInterface $locator,
2629
) {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedIteratorConsumer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

14-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
14+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
1515

1616
final class TaggedIteratorConsumer
1717
{
1818
public function __construct(
19-
#[TaggedIterator('foo_bar', indexAttribute: 'foo')]
19+
#[AutowireIterator('foo_bar', indexAttribute: 'foo')]
2020
private iterable $param,
2121
) {
2222
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
1616

1717
final class TaggedLocatorConsumer
1818
{
1919
public function __construct(
20-
#[TaggedLocator('foo_bar', indexAttribute: 'foo')]
20+
#[AutowireLocator('foo_bar', indexAttribute: 'foo')]
2121
private ContainerInterface $locator,
2222
) {
2323
}

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1818
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1919
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
20+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
2021
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
2122
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
2223
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
@@ -31,6 +32,7 @@
3132
use Symfony\Component\HttpFoundation\Response;
3233
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
3334
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
35+
use Symfony\Contracts\Service\Attribute\SubscribedService;
3436

3537
class RegisterControllerArgumentLocatorsPassTest extends TestCase
3638
{
@@ -499,6 +501,7 @@ public function testAutowireAttribute()
499501
public function testTaggedIteratorAndTaggedLocatorAttributes()
500502
{
501503
$container = new ContainerBuilder();
504+
$container->setParameter('some.parameter', 'bar');
502505
$resolver = $container->register('argument_resolver.service', \stdClass::class)->addArgument([]);
503506

504507
$container->register('bar', \stdClass::class)->addTag('foobar');
@@ -517,25 +520,48 @@ public function testTaggedIteratorAndTaggedLocatorAttributes()
517520
/** @var ServiceLocator $locator */
518521
$locator = $container->get($locatorId)->get('foo::fooAction');
519522

520-
$this->assertCount(3, $locator->getProvidedServices());
523+
$this->assertCount(7, $locator->getProvidedServices());
521524

522-
$this->assertTrue($locator->has('iterator'));
523-
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator'));
525+
$this->assertTrue($locator->has('iterator1'));
526+
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator1'));
524527
$this->assertCount(2, $argIterator);
525528

526-
$this->assertTrue($locator->has('locator'));
527-
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator'));
529+
$this->assertTrue($locator->has('iterator2'));
530+
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator2'));
531+
$this->assertCount(2, $argIterator);
532+
533+
$this->assertTrue($locator->has('locator1'));
534+
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator1'));
535+
$this->assertCount(2, $argLocator);
536+
$this->assertTrue($argLocator->has('bar'));
537+
$this->assertTrue($argLocator->has('baz'));
538+
539+
$this->assertSame(iterator_to_array($argIterator), [$argLocator->get('bar'), $argLocator->get('baz')]);
540+
541+
$this->assertTrue($locator->has('locator2'));
542+
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator2'));
528543
$this->assertCount(2, $argLocator);
529544
$this->assertTrue($argLocator->has('bar'));
530545
$this->assertTrue($argLocator->has('baz'));
531546

532547
$this->assertSame(iterator_to_array($argIterator), [$argLocator->get('bar'), $argLocator->get('baz')]);
533548

534-
$this->assertTrue($locator->has('container'));
535-
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('container'));
549+
$this->assertTrue($locator->has('container1'));
550+
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('container1'));
536551
$this->assertCount(2, $argLocator);
537552
$this->assertTrue($argLocator->has('bar'));
538553
$this->assertTrue($argLocator->has('baz'));
554+
555+
$this->assertTrue($locator->has('container2'));
556+
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('container2'));
557+
$this->assertCount(1, $argLocator);
558+
$this->assertTrue($argLocator->has('foo'));
559+
$this->assertSame('bar', $argLocator->get('foo'));
560+
561+
$this->assertTrue($locator->has('iterator3'));
562+
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator3'));
563+
$this->assertCount(1, $argIterator);
564+
$this->assertSame('bar', iterator_to_array($argIterator)['foo']);
539565
}
540566
}
541567

@@ -674,9 +700,13 @@ public function fooAction(
674700
class WithTaggedIteratorAndTaggedLocator
675701
{
676702
public function fooAction(
677-
#[TaggedIterator('foobar')] iterable $iterator,
678-
#[TaggedLocator('foobar')] ServiceLocator $locator,
679-
#[AutowireLocator(['bar', 'baz'])] ContainerInterface $container,
703+
#[TaggedIterator('foobar')] iterable $iterator1,
704+
#[AutowireIterator('foobar')] iterable $iterator2,
705+
#[TaggedLocator('foobar')] ServiceLocator $locator1,
706+
#[AutowireLocator('foobar')] ServiceLocator $locator2,
707+
#[AutowireLocator(['bar', 'baz'])] ContainerInterface $container1,
708+
#[AutowireLocator(['foo' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%'))])] ContainerInterface $container2,
709+
#[AutowireIterator(['foo' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%'))])] iterable $iterator3,
680710
) {
681711
}
682712
}

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"symfony/http-client-contracts": "<2.5",
6464
"symfony/mailer": "<5.4",
6565
"symfony/messenger": "<5.4",
66+
"symfony/service-contracts": "<3.2",
6667
"symfony/translation": "<5.4",
6768
"symfony/translation-contracts": "<2.5",
6869
"symfony/twig-bridge": "<5.4",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy