class ChainResponsePolicyTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/PageCache/ChainResponsePolicyTest.php \Drupal\Tests\Core\PageCache\ChainResponsePolicyTest
@coversDefaultClass \Drupal\Core\PageCache\ChainResponsePolicy @group PageCache
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\PageCache\ChainResponsePolicyTest
Expanded class hierarchy of ChainResponsePolicyTest
File
- core/
tests/ Drupal/ Tests/ Core/ PageCache/ ChainResponsePolicyTest.php, line 15
Namespace
Drupal\Tests\Core\PageCacheView source
class ChainResponsePolicyTest extends UnitTestCase {
/**
* The chain response policy under test.
*
* @var \Drupal\Core\PageCache\ChainResponsePolicy
*/
protected $policy;
/**
* A request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* A response object.
*
* @var \Symfony\Component\HttpFoundation\Response
*/
protected $response;
protected function setUp() : void {
$this->policy = new ChainResponsePolicy();
$this->response = new Response();
$this->request = new Request();
}
/**
* Asserts that check() returns NULL if the chain is empty.
*
* @covers ::check
*/
public function testEmptyChain() {
$result = $this->policy
->check($this->response, $this->request);
$this
->assertSame(NULL, $result);
}
/**
* Asserts that check() returns NULL if a rule returns NULL.
*
* @covers ::check
*/
public function testNullRuleChain() {
$rule = $this
->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->response, $this->request)
->will($this
->returnValue(NULL));
$this->policy
->addPolicy($rule);
$result = $this->policy
->check($this->response, $this->request);
$this
->assertSame(NULL, $result);
}
/**
* Asserts that check() throws an exception if a rule returns an invalid value.
*
* @dataProvider providerChainExceptionOnInvalidReturnValue
* @covers ::check
*/
public function testChainExceptionOnInvalidReturnValue($return_value) {
$rule = $this
->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->response, $this->request)
->will($this
->returnValue($return_value));
$this->policy
->addPolicy($rule);
$this
->expectException(\UnexpectedValueException::class);
$this->policy
->check($this->response, $this->request);
}
/**
* Provides test data for testChainExceptionOnInvalidReturnValue.
*
* @return array
* Test input and expected result.
*/
public function providerChainExceptionOnInvalidReturnValue() {
return [
[
FALSE,
],
[
0,
],
[
1,
],
[
TRUE,
],
[
[
1,
2,
3,
],
],
[
new \stdClass(),
],
];
}
/**
* Asserts that check() returns immediately when a rule returned DENY.
*/
public function testStopChainOnFirstDeny() {
$rule1 = $this
->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
$rule1
->expects($this
->once())
->method('check')
->with($this->response, $this->request);
$this->policy
->addPolicy($rule1);
$deny_rule = $this
->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
$deny_rule
->expects($this
->once())
->method('check')
->with($this->response, $this->request)
->will($this
->returnValue(ResponsePolicyInterface::DENY));
$this->policy
->addPolicy($deny_rule);
$ignored_rule = $this
->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
$ignored_rule
->expects($this
->never())
->method('check');
$this->policy
->addPolicy($ignored_rule);
$actual_result = $this->policy
->check($this->response, $this->request);
$this
->assertSame(ResponsePolicyInterface::DENY, $actual_result);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChainResponsePolicyTest:: |
protected | property | The chain response policy under test. | |
ChainResponsePolicyTest:: |
protected | property | A request object. | |
ChainResponsePolicyTest:: |
protected | property | A response object. | |
ChainResponsePolicyTest:: |
public | function | Provides test data for testChainExceptionOnInvalidReturnValue. | |
ChainResponsePolicyTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ChainResponsePolicyTest:: |
public | function | Asserts that check() throws an exception if a rule returns an invalid value. | |
ChainResponsePolicyTest:: |
public | function | Asserts that check() returns NULL if the chain is empty. | |
ChainResponsePolicyTest:: |
public | function | Asserts that check() returns NULL if a rule returns NULL. | |
ChainResponsePolicyTest:: |
public | function | Asserts that check() returns immediately when a rule returned DENY. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
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 | 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:: |
public static | function |