Skip to content

[HttpKernel] Add tests for #[TaggedIterator] & #[TaggedLocator] on controller arguments #49500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
Expand Down Expand Up @@ -460,10 +463,6 @@ public function testResponseArgumentIsIgnored()

public function testAutowireAttribute()
{
if (!class_exists(Autowire::class)) {
$this->markTestSkipped('#[Autowire] attribute not available.');
}

$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service', 'stdClass')->addArgument([]);

Expand Down Expand Up @@ -493,6 +492,42 @@ public function testAutowireAttribute()
$this->assertSame('foo', $locator->get('customAutowire'));
$this->assertFalse($locator->has('service2'));
}

public function testTaggedIteratorAndTaggedLocatorAttributes()
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service', \stdClass::class)->addArgument([]);

$container->register('bar', \stdClass::class)->addTag('foobar');
$container->register('baz', \stdClass::class)->addTag('foobar');

$container->register('foo', WithTaggedIteratorAndTaggedLocator::class)
->addTag('controller.service_arguments');

(new RegisterControllerArgumentLocatorsPass())->process($container);

$locatorId = (string) $resolver->getArgument(0);
$container->getDefinition($locatorId)->setPublic(true);

$container->compile();

/** @var ServiceLocator $locator */
$locator = $container->get($locatorId)->get('foo::fooAction');

$this->assertCount(2, $locator->getProvidedServices());

$this->assertTrue($locator->has('iterator'));
$this->assertInstanceOf(RewindableGenerator::class, $argIterator = $locator->get('iterator'));
$this->assertCount(2, $argIterator);

$this->assertTrue($locator->has('locator'));
$this->assertInstanceOf(ServiceLocator::class, $argLocator = $locator->get('locator'));
$this->assertCount(2, $argLocator);
$this->assertTrue($argLocator->has('bar'));
$this->assertTrue($argLocator->has('baz'));

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

class RegisterTestController
Expand Down Expand Up @@ -614,3 +649,12 @@ public function fooAction(
) {
}
}

class WithTaggedIteratorAndTaggedLocator
{
public function fooAction(
#[TaggedIterator('foobar')] iterable $iterator,
#[TaggedLocator('foobar')] ServiceLocator $locator,
) {
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"symfony/config": "^6.1",
"symfony/console": "^5.4|^6.0",
"symfony/css-selector": "^5.4|^6.0",
"symfony/dependency-injection": "^6.2",
"symfony/dependency-injection": "^6.3",
"symfony/dom-crawler": "^5.4|^6.0",
"symfony/expression-language": "^5.4|^6.0",
"symfony/finder": "^5.4|^6.0",
Expand All @@ -53,7 +53,7 @@
"symfony/config": "<6.1",
"symfony/console": "<5.4",
"symfony/form": "<5.4",
"symfony/dependency-injection": "<6.2",
"symfony/dependency-injection": "<6.3",
"symfony/doctrine-bridge": "<5.4",
"symfony/http-client": "<5.4",
"symfony/http-client-contracts": "<2.5",
Expand Down