class NegotiationMiddlewareTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
  2. 8.9.x core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest
  3. 11.x core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php \Drupal\Tests\Core\StackMiddleware\NegotiationMiddlewareTest

@coversDefaultClass \Drupal\Core\StackMiddleware\NegotiationMiddleware
@group NegotiationMiddleware

Hierarchy

Expanded class hierarchy of NegotiationMiddlewareTest

File

core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php, line 18

Namespace

Drupal\Tests\Core\StackMiddleware
View source
class NegotiationMiddlewareTest extends UnitTestCase {
  
  /**
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $app;
  
  /**
   * @var \Drupal\Tests\Core\StackMiddleware\StubNegotiationMiddleware
   */
  protected $contentNegotiation;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->app = $this->prophesize(HttpKernelInterface::class);
    $this->contentNegotiation = new StubNegotiationMiddleware($this->app
      ->reveal());
  }
  
  /**
   * Tests the getContentType() method with AJAX iframe upload.
   *
   * @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.
   *
   * @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.
   *
   * @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.
   *
   * @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.
   *
   * @covers ::handle
   */
  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->app
      ->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->app
      ->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE)
      ->shouldBeCalled()
      ->willReturn($this->createMock(Response::class));
    $this->contentNegotiation
      ->handle($request_mock, HttpKernelInterface::SUB_REQUEST, FALSE);
  }
  
  /**
   * @covers ::registerFormat
   */
  public function testSetFormat() : void {
    $app = $this->createMock(HttpKernelInterface::class);
    $app->expects($this->once())
      ->method('handle')
      ->willReturn($this->createMock(Response::class));
    $content_negotiation = new StubNegotiationMiddleware($app);
    $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 Overrides
NegotiationMiddlewareTest::$app protected property
NegotiationMiddlewareTest::$contentNegotiation 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 @covers ::registerFormat[[api-linebreak]]
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.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
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::setUpBeforeClass public static function
UnitTestCase::__get public function

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