|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\HttpKernel\Tests\Event; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 17 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 18 | +use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar; |
| 19 | +use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController; |
| 20 | +use Symfony\Component\HttpKernel\Tests\TestHttpKernel; |
| 21 | + |
| 22 | +class ControllerEventTest extends TestCase |
| 23 | +{ |
| 24 | + public function testSetAttributes() |
| 25 | + { |
| 26 | + $request = new Request(); |
| 27 | + $request->attributes->set('_controller_reflectors', [1, 2]); |
| 28 | + $controller = [new AttributeController(), 'action']; |
| 29 | + $event = new ControllerEvent(new TestHttpKernel(), $controller, $request, HttpKernelInterface::MAIN_REQUEST); |
| 30 | + $event->setAttributes([]); |
| 31 | + |
| 32 | + $this->assertSame([], $event->getAttributes()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider provideGetAttributes |
| 37 | + */ |
| 38 | + public function testGetAttributes(callable $controller) |
| 39 | + { |
| 40 | + $request = new Request(); |
| 41 | + $reflector = new \ReflectionFunction($controller(...)); |
| 42 | + $request->attributes->set('_controller_reflectors', [str_contains($reflector->name, '{closure}') ? null : $reflector->getClosureScopeClass(), $reflector]); |
| 43 | + |
| 44 | + $event = new ControllerEvent(new TestHttpKernel(), $controller, $request, HttpKernelInterface::MAIN_REQUEST); |
| 45 | + |
| 46 | + $expected = [ |
| 47 | + Bar::class => [ |
| 48 | + new Bar('class'), |
| 49 | + new Bar('method'), |
| 50 | + ], |
| 51 | + ]; |
| 52 | + |
| 53 | + $this->assertEquals($expected, $event->getAttributes()); |
| 54 | + } |
| 55 | + |
| 56 | + public function provideGetAttributes() |
| 57 | + { |
| 58 | + yield [[new AttributeController(), '__invoke']]; |
| 59 | + yield [new AttributeController()]; |
| 60 | + yield [(new AttributeController())->__invoke(...)]; |
| 61 | + yield [#[Bar('class'), Bar('method')] static function () {}]; |
| 62 | + } |
| 63 | +} |
0 commit comments