class OptionsRequestSubscriberTest in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest
- 10 core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest
@coversDefaultClass \Drupal\Core\EventSubscriber\OptionsRequestSubscriber @group EventSubscriber
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\EventSubscriber\OptionsRequestSubscriberTest
Expanded class hierarchy of OptionsRequestSubscriberTest
File
- core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ OptionsRequestSubscriberTest.php, line 18
Namespace
Drupal\Tests\Core\EventSubscriberView source
class OptionsRequestSubscriberTest extends UnitTestCase {
/**
* @covers ::onRequest
*/
public function testWithNonOptionRequest() {
$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 GetResponseEvent($kernel
->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber
->onRequest($event);
$this
->assertFalse($event
->hasResponse());
}
/**
* @covers ::onRequest
*/
public function testWithoutMatchingRoutes() {
$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 GetResponseEvent($kernel
->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber
->onRequest($event);
$this
->assertFalse($event
->hasResponse());
}
/**
* @covers ::onRequest
* @dataProvider providerTestOnRequestWithOptionsRequest
*/
public function testWithOptionsRequest(RouteCollection $collection, $expected_header) {
$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 GetResponseEvent($kernel
->reveal(), $request, HttpKernelInterface::MASTER_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 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OptionsRequestSubscriberTest:: |
public | function | ||
OptionsRequestSubscriberTest:: |
public | function | @covers ::onRequest | |
OptionsRequestSubscriberTest:: |
public | function | @covers ::onRequest @dataProvider providerTestOnRequestWithOptionsRequest | |
OptionsRequestSubscriberTest:: |
public | function | @covers ::onRequest | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |