class CacheableExceptionTest
Tests Cacheable Exception.
Attributes
#[Group('Http')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Http\CacheableExceptionTest extends \Drupal\Tests\UnitTestCase
 
 
Expanded class hierarchy of CacheableExceptionTest
File
- 
              core/
tests/ Drupal/ Tests/ Core/ Http/ CacheableExceptionTest.php, line 31  
Namespace
Drupal\Tests\Core\HttpView source
class CacheableExceptionTest extends UnitTestCase {
  
  /**
   * Tests cacheable http exception.
   *
   * @legacy-covers \Drupal\Core\Http\Exception\CacheableHttpException
   */
  public function testCacheableHttpException() : void {
    $exception = new CacheableHttpException((new CacheableMetadata())->setCacheContexts([
      'route',
    ]), 500, 'test message', NULL, [
      'X-Drupal-Exception' => 'Test',
    ], 123);
    $this->assertSame([
      'route',
    ], $exception->getCacheContexts());
    $this->assertSame(500, $exception->getStatusCode());
    $this->assertSame('test message', $exception->getMessage());
    $this->assertSame([
      'X-Drupal-Exception' => 'Test',
    ], $exception->getHeaders());
    $this->assertSame(123, $exception->getCode());
  }
  
  /**
   * Tests exceptions.
   */
  public function testExceptions($status_code, $class, $argument = NULL, $expected_headers = []) : void {
    $cacheable_metadata = (new CacheableMetadata())->setCacheContexts([
      'route',
    ]);
    $message = "{$class} test message";
    $previous = new class ('Error of PHP 7+') extends \Error {
};
    if ($argument) {
      $exception = new $class($cacheable_metadata, $argument, $message, $previous, 123);
    }
    else {
      $exception = new $class($cacheable_metadata, $message, $previous, 123);
    }
    $this->assertSame([
      'route',
    ], $exception->getCacheContexts());
    $this->assertSame($message, $exception->getMessage());
    $this->assertSame($status_code, $exception->getStatusCode());
    $this->assertSame($expected_headers, $exception->getHeaders());
    $this->assertSame($previous, $exception->getPrevious());
    $this->assertSame(123, $exception->getCode());
  }
  public static function providerTestExceptions() {
    return [
      [
        400,
        CacheableBadRequestHttpException::class,
      ],
      [
        401,
        CacheableUnauthorizedHttpException::class,
        'test challenge',
        [
          'WWW-Authenticate' => 'test challenge',
        ],
      ],
      [
        403,
        CacheableAccessDeniedHttpException::class,
      ],
      [
        404,
        CacheableNotFoundHttpException::class,
      ],
      [
        405,
        CacheableMethodNotAllowedHttpException::class,
        [
          'POST',
          'PUT',
        ],
        [
          'Allow' => 'POST, PUT',
        ],
      ],
      [
        406,
        CacheableNotAcceptableHttpException::class,
      ],
      [
        409,
        CacheableConflictHttpException::class,
      ],
      [
        410,
        CacheableGoneHttpException::class,
      ],
      [
        411,
        CacheableLengthRequiredHttpException::class,
      ],
      [
        412,
        CacheablePreconditionFailedHttpException::class,
      ],
      [
        415,
        CacheableUnsupportedMediaTypeHttpException::class,
      ],
      [
        422,
        CacheableUnprocessableEntityHttpException::class,
      ],
      [
        428,
        CacheablePreconditionRequiredHttpException::class,
      ],
      [
        429,
        CacheableTooManyRequestsHttpException::class,
        60,
        [
          'Retry-After' => 60,
        ],
      ],
      [
        503,
        CacheableServiceUnavailableHttpException::class,
        60,
        [
          'Retry-After' => 60,
        ],
      ],
    ];
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides | 
|---|---|---|---|---|
| CacheableExceptionTest::providerTestExceptions | public static | function | ||
| CacheableExceptionTest::testCacheableHttpException | public | function | Tests cacheable http exception. | |
| CacheableExceptionTest::testExceptions | public | function | Tests exceptions. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| 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::setUp | protected | function | 366 | |
| 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.