class ChainResponsePolicyTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/PageCache/ChainResponsePolicyTest.php \Drupal\Tests\Core\PageCache\ChainResponsePolicyTest
  2. 8.9.x core/tests/Drupal/Tests/Core/PageCache/ChainResponsePolicyTest.php \Drupal\Tests\Core\PageCache\ChainResponsePolicyTest
  3. 11.x core/tests/Drupal/Tests/Core/PageCache/ChainResponsePolicyTest.php \Drupal\Tests\Core\PageCache\ChainResponsePolicyTest

@coversDefaultClass \Drupal\Core\PageCache\ChainResponsePolicy @group PageCache

Hierarchy

Expanded class hierarchy of ChainResponsePolicyTest

File

core/tests/Drupal/Tests/Core/PageCache/ChainResponsePolicyTest.php, line 17

Namespace

Drupal\Tests\Core\PageCache
View 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;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $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() : void {
        $result = $this->policy
            ->check($this->response, $this->request);
        $this->assertNull($result);
    }
    
    /**
     * Asserts that check() returns NULL if a rule returns NULL.
     *
     * @covers ::check
     */
    public function testNullRuleChain() : void {
        $rule = $this->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
        $rule->expects($this->once())
            ->method('check')
            ->with($this->response, $this->request)
            ->willReturn(NULL);
        $this->policy
            ->addPolicy($rule);
        $result = $this->policy
            ->check($this->response, $this->request);
        $this->assertNull($result);
    }
    
    /**
     * Asserts that check() throws an exception if a rule returns an invalid value.
     *
     * @dataProvider providerChainExceptionOnInvalidReturnValue
     * @covers ::check
     */
    public function testChainExceptionOnInvalidReturnValue($return_value) : void {
        $rule = $this->createMock('Drupal\\Core\\PageCache\\ResponsePolicyInterface');
        $rule->expects($this->once())
            ->method('check')
            ->with($this->response, $this->request)
            ->willReturn($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 static 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() : void {
        $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)
            ->willReturn(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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
ChainResponsePolicyTest::$policy protected property The chain response policy under test.
ChainResponsePolicyTest::$request protected property A request object.
ChainResponsePolicyTest::$response protected property A response object.
ChainResponsePolicyTest::providerChainExceptionOnInvalidReturnValue public static function Provides test data for testChainExceptionOnInvalidReturnValue.
ChainResponsePolicyTest::setUp protected function Overrides UnitTestCase::setUp
ChainResponsePolicyTest::testChainExceptionOnInvalidReturnValue public function Asserts that check() throws an exception if a rule returns an invalid value.
ChainResponsePolicyTest::testEmptyChain public function Asserts that check() returns NULL if the chain is empty.
ChainResponsePolicyTest::testNullRuleChain public function Asserts that check() returns NULL if a rule returns NULL.
ChainResponsePolicyTest::testStopChainOnFirstDeny public function Asserts that check() returns immediately when a rule returned DENY.
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.