class OptionsRequestSubscriberTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest
  2. 8.9.x core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest
  3. 10 core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest

@coversDefaultClass \Drupal\Core\EventSubscriber\OptionsRequestSubscriber @group EventSubscriber

Hierarchy

Expanded class hierarchy of OptionsRequestSubscriberTest

File

core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php, line 20

Namespace

Drupal\Tests\Core\EventSubscriber
View source
class OptionsRequestSubscriberTest extends UnitTestCase {
    
    /**
     * @covers ::onRequest
     */
    public function testWithNonOptionRequest() : void {
        $kernel = $this->prophesize(HttpKernelInterface::class);
        $request = Request::create('/example', 'GET');
        $route_provider = $this->prophesize(RouteProviderInterface::class);
        $route_provider->getRouteCollectionForRequest($request)
            ->shouldNotBeCalled();
        $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
        $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MAIN_REQUEST);
        $subscriber->onRequest($event);
        $this->assertFalse($event->hasResponse());
    }
    
    /**
     * @covers ::onRequest
     */
    public function testWithoutMatchingRoutes() : void {
        $kernel = $this->prophesize(HttpKernelInterface::class);
        $request = Request::create('/example', 'OPTIONS');
        $route_provider = $this->prophesize(RouteProviderInterface::class);
        $route_provider->getRouteCollectionForRequest($request)
            ->willReturn(new RouteCollection())
            ->shouldBeCalled();
        $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
        $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MAIN_REQUEST);
        $subscriber->onRequest($event);
        $this->assertFalse($event->hasResponse());
    }
    
    /**
     * @covers ::onRequest
     * @dataProvider providerTestOnRequestWithOptionsRequest
     */
    public function testWithOptionsRequest(RouteCollection $collection, $expected_header) : void {
        $kernel = $this->prophesize(HttpKernelInterface::class);
        $request = Request::create('/example', 'OPTIONS');
        $route_provider = $this->prophesize(RouteProviderInterface::class);
        $route_provider->getRouteCollectionForRequest($request)
            ->willReturn($collection)
            ->shouldBeCalled();
        $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
        $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MAIN_REQUEST);
        $subscriber->onRequest($event);
        $this->assertTrue($event->hasResponse());
        $response = $event->getResponse();
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals($expected_header, $response->headers
            ->get('Allow'));
    }
    public static function providerTestOnRequestWithOptionsRequest() {
        $data = [];
        foreach ([
            'GET',
            'POST',
            'PATCH',
            'PUT',
            'DELETE',
        ] as $method) {
            $collection = new RouteCollection();
            $collection->add('example.1', new Route('/example', [], [], [], '', [], [
                $method,
            ]));
            $data['one_route_' . $method] = [
                $collection,
                $method,
            ];
        }
        foreach ([
            'GET',
            'POST',
            'PATCH',
            'PUT',
            'DELETE',
        ] as $method_a) {
            foreach ([
                'GET',
                'POST',
                'PATCH',
                'PUT',
                'DELETE',
            ] as $method_b) {
                if ($method_a != $method_b) {
                    $collection = new RouteCollection();
                    $collection->add('example.1', new Route('/example', [], [], [], '', [], [
                        $method_a,
                        $method_b,
                    ]));
                    $data['one_route_' . $method_a . '_' . $method_b] = [
                        $collection,
                        $method_a . ', ' . $method_b,
                    ];
                }
            }
        }
        foreach ([
            'GET',
            'POST',
            'PATCH',
            'PUT',
            'DELETE',
        ] as $method_a) {
            foreach ([
                'GET',
                'POST',
                'PATCH',
                'PUT',
                'DELETE',
            ] as $method_b) {
                foreach ([
                    'GET',
                    'POST',
                    'PATCH',
                    'PUT',
                    'DELETE',
                ] as $method_c) {
                    $collection = new RouteCollection();
                    $collection->add('example.1', new Route('/example', [], [], [], '', [], [
                        $method_a,
                    ]));
                    $collection->add('example.2', new Route('/example', [], [], [], '', [], [
                        $method_a,
                        $method_b,
                    ]));
                    $collection->add('example.3', new Route('/example', [], [], [], '', [], [
                        $method_b,
                        $method_c,
                    ]));
                    $methods = array_unique([
                        $method_a,
                        $method_b,
                        $method_c,
                    ]);
                    $data['multiple_routes_' . $method_a . '_' . $method_b . '_' . $method_c] = [
                        $collection,
                        implode(', ', $methods),
                    ];
                }
            }
        }
        return $data;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
OptionsRequestSubscriberTest::providerTestOnRequestWithOptionsRequest public static function
OptionsRequestSubscriberTest::testWithNonOptionRequest public function @covers ::onRequest
OptionsRequestSubscriberTest::testWithOptionsRequest public function @covers ::onRequest
@dataProvider providerTestOnRequestWithOptionsRequest
OptionsRequestSubscriberTest::testWithoutMatchingRoutes public function @covers ::onRequest
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.