class NegotiationMiddlewareTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
- 10 core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
- 9 core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
- 8.9.x core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
Tests Drupal\Core\StackMiddleware\NegotiationMiddleware.
Attributes
#[CoversClass(NegotiationMiddleware::class)]
#[Group('NegotiationMiddleware')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of NegotiationMiddlewareTest
File
-
core/
tests/ Drupal/ Tests/ Core/ StackMiddleware/ NegotiationMiddlewareTest.php, line 19
Namespace
Drupal\Tests\Core\StackMiddlewareView source
class NegotiationMiddlewareTest extends UnitTestCase {
/**
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* @var \Drupal\Tests\Core\StackMiddleware\StubNegotiationMiddleware
*/
protected $contentNegotiation;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->httpKernel = $this->prophesize(HttpKernelInterface::class);
$this->contentNegotiation = new StubNegotiationMiddleware($this->httpKernel
->reveal());
}
/**
* Tests the getContentType() method with AJAX iframe upload.
*
* @legacy-covers ::getContentType
*/
public function testAjaxIframeUpload() : void {
$request = new Request();
$request->request
->set('ajax_iframe_upload', '1');
$this->assertSame('iframeupload', $this->contentNegotiation
->getContentType($request));
}
/**
* Tests the specifying a format via query parameters gets used.
*
* @legacy-covers ::getContentType
*/
public function testFormatViaQueryParameter() : void {
$request = new Request();
$request->query
->set('_format', 'bob');
$this->assertSame('bob', $this->contentNegotiation
->getContentType($request));
}
/**
* Tests the getContentType() method when no priority format is found.
*
* @legacy-covers ::getContentType
*/
public function testUnknownContentTypeReturnsNull() : void {
$request = new Request();
$this->assertNull($this->contentNegotiation
->getContentType($request));
}
/**
* Tests the getContentType() method when no priority format is found but it's an AJAX request.
*
* @legacy-covers ::getContentType
*/
public function testUnknownContentTypeButAjaxRequest() : void {
$request = new Request();
$request->headers
->set('X-Requested-With', 'XMLHttpRequest');
$this->assertNull($this->contentNegotiation
->getContentType($request));
}
/**
* Tests that handle() correctly hands off to sub application.
*/
public function testHandle() : void {
$request = $this->prophesize(Request::class);
// Default empty format list should not set any formats.
$request->setFormat()
->shouldNotBeCalled();
// Request format will be set with default format.
$request->setRequestFormat()
->shouldNotBeCalled();
// Some getContentType calls we don't really care about but have to mock.
$request_mock = $request->reveal();
$request_mock->query = new InputBag();
$request_mock->request = new InputBag();
// Calling kernel app with default arguments.
$this->httpKernel
->handle($request_mock, HttpKernelInterface::MAIN_REQUEST, TRUE)
->shouldBeCalled()
->willReturn($this->createMock(Response::class));
$this->contentNegotiation
->handle($request_mock);
// Calling kernel app with specified arguments.
$this->httpKernel
->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
->shouldBeCalled()
->willReturn($this->createMock(Response::class));
$this->contentNegotiation
->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
}
/**
* Tests set format.
*
* @legacy-covers ::registerFormat
*/
public function testSetFormat() : void {
$httpKernel = $this->createMock(HttpKernelInterface::class);
$httpKernel->expects($this->once())
->method('handle')
->willReturn($this->createMock(Response::class));
$content_negotiation = new StubNegotiationMiddleware($httpKernel);
$request = $this->prophesize(Request::class);
// Default empty format list should not set any formats.
$request->setFormat('david', 'geeky/david')
->shouldBeCalled();
// Some calls we don't care about.
$request->setRequestFormat()
->shouldNotBeCalled();
$request_mock = $request->reveal();
$request_mock->query = new InputBag();
$request_mock->request = new InputBag();
// Trigger handle.
$content_negotiation->registerFormat('david', 'geeky/david');
$content_negotiation->handle($request_mock);
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| NegotiationMiddlewareTest::$contentNegotiation | protected | property | |||
| NegotiationMiddlewareTest::$httpKernel | protected | property | |||
| NegotiationMiddlewareTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| NegotiationMiddlewareTest::testAjaxIframeUpload | public | function | Tests the getContentType() method with AJAX iframe upload. | ||
| NegotiationMiddlewareTest::testFormatViaQueryParameter | public | function | Tests the specifying a format via query parameters gets used. | ||
| NegotiationMiddlewareTest::testHandle | public | function | Tests that handle() correctly hands off to sub application. | ||
| NegotiationMiddlewareTest::testSetFormat | public | function | Tests set format. | ||
| NegotiationMiddlewareTest::testUnknownContentTypeButAjaxRequest | public | function | Tests the getContentType() method when no priority format is found but it's an AJAX request. | ||
| NegotiationMiddlewareTest::testUnknownContentTypeReturnsNull | public | function | Tests the getContentType() method when no priority format is found. | ||
| 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::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::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.