|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\User\EventListener; |
| 10 | + |
| 11 | +use Ibexa\Tests\User\Stub\RestrictedControllerStub; |
| 12 | +use Ibexa\User\EventListener\PerformAccessCheckSubscriber; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 16 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 17 | + |
| 18 | +final class PerformAccessCheckSubscriberTest extends TestCase |
| 19 | +{ |
| 20 | + private PerformAccessCheckSubscriber $subscriber; |
| 21 | + |
| 22 | + private HttpKernelInterface $kernel; |
| 23 | + |
| 24 | + private Request $request; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->kernel = $this->createMock(HttpKernelInterface::class); |
| 29 | + $this->request = new Request(); |
| 30 | + $this->subscriber = new PerformAccessCheckSubscriber([]); |
| 31 | + } |
| 32 | + |
| 33 | + public function testArrayController(): void |
| 34 | + { |
| 35 | + $controller = new RestrictedControllerStub(); |
| 36 | + $event = new ControllerEvent( |
| 37 | + $this->kernel, |
| 38 | + [$controller, 'action'], |
| 39 | + $this->request, |
| 40 | + HttpKernelInterface::MAIN_REQUEST |
| 41 | + ); |
| 42 | + |
| 43 | + $this->subscriber->onControllerEvent($event); |
| 44 | + |
| 45 | + self::assertTrue($controller->wasCheckPerformed()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testInvokableController(): void |
| 49 | + { |
| 50 | + $controller = new RestrictedControllerStub(); |
| 51 | + $event = new ControllerEvent( |
| 52 | + $this->kernel, |
| 53 | + $controller, |
| 54 | + $this->request, |
| 55 | + HttpKernelInterface::MAIN_REQUEST |
| 56 | + ); |
| 57 | + |
| 58 | + $this->subscriber->onControllerEvent($event); |
| 59 | + |
| 60 | + self::assertTrue($controller->wasCheckPerformed()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testStringControllerWithServiceLookup(): void |
| 64 | + { |
| 65 | + $controller = new RestrictedControllerStub(); |
| 66 | + $this->subscriber = new PerformAccessCheckSubscriber([$controller]); |
| 67 | + |
| 68 | + $event = new ControllerEvent( |
| 69 | + $this->kernel, |
| 70 | + RestrictedControllerStub::class . '::staticAction', |
| 71 | + $this->request, |
| 72 | + HttpKernelInterface::MAIN_REQUEST |
| 73 | + ); |
| 74 | + |
| 75 | + $this->subscriber->onControllerEvent($event); |
| 76 | + |
| 77 | + self::assertTrue($controller->wasCheckPerformed()); |
| 78 | + } |
| 79 | +} |
0 commit comments