canManageCookieMock = $this->createMock(CanManageCookieInterface::class); $this->configMock = $this->createMock(Config::class); $this->isUserOptInMock = $this->createMock(IsUserOptInInterface::class); $this->getCategoryKeyMock = $this->createMock(GetCategoryKey::class); $this->isAllowedCategoryMock = $this->createMock(IsAllowedCategoryInterface::class); $getTrueNameMock = $this->createMock(GetTrueName::class); $getTrueNameMock->method('execute')->willReturnArgument(0); $isKnown = new IsKnown($this->getCategoryKeyMock); $this->isAllowedModel = new IsAllowed( $this->canManageCookieMock, $this->configMock, $this->isUserOptInMock, $this->getCategoryKeyMock, $this->isAllowedCategoryMock, $isKnown, $getTrueNameMock, ['test-key' => 'TEST_SYSTEM_COOKIE_NAME'] ); } public function testSystemCookie() { self::assertTrue($this->isAllowedModel->execute('TEST_SYSTEM_COOKIE_NAME')); } public function testCannotManageCookie() { $this->canManageCookieMock ->expects(self::once()) ->method('execute') ->willReturn(false); $this->getCategoryKeyMock ->expects(self::never()) ->method('execute'); $this->isAllowedCategoryMock ->expects(self::never()) ->method('execute'); self::assertTrue($this->isAllowedModel->execute('test_cookie_name')); } /** * @dataProvider canUseCookieBeforeOptInProvider * * @param bool $canUseCookieBeforeOptIn * @param bool $isKnown * @param bool $isAllowedCategory * @param bool $result */ public function testNotOptInUser( bool $canUseCookieBeforeOptIn, bool $isKnown, bool $isAllowedCategory, bool $result ) { $this->canManageCookieMock ->expects(self::once()) ->method('execute') ->willReturn(true); $this->isUserOptInMock ->expects(self::once()) ->method('execute') ->willReturn(false); $this->configMock ->expects(self::once()) ->method('canUseCookieBeforeOptIn') ->willReturn($canUseCookieBeforeOptIn); $this->getCategoryKeyMock ->method('execute') ->willReturn($isKnown ? 'test_category' : ''); $this->isAllowedCategoryMock ->method('execute') ->willReturn($isAllowedCategory); $this->configMock->expects(self::never())->method('canBlockUnknownCookie'); self::assertSame($result, $this->isAllowedModel->execute('test_cookie_name')); } public function canUseCookieBeforeOptInProvider(): array { return [ [ 'canUseCookieBeforeOptIn' => true, 'isKnown' => false, 'isAllowedCategory' => false, 'result' => true, ], [ 'canUseCookieBeforeOptIn' => false, 'isKnown' => false, 'isAllowedCategory' => false, 'result' => false, ], [ 'canUseCookieBeforeOptIn' => false, 'isKnown' => true, 'isAllowedCategory' => false, 'result' => false, ], [ 'canUseCookieBeforeOptIn' => false, 'isKnown' => true, 'isAllowedCategory' => true, 'result' => true, ], ]; } /** * @dataProvider canBlockUnknownCookieProvider * * @param bool $canBlockUnknownCookie * @param bool $result */ public function testUnknownCookie(bool $canBlockUnknownCookie, bool $result) { $this->canManageCookieMock ->expects(self::once()) ->method('execute') ->willReturn(true); $this->isUserOptInMock ->expects(self::once()) ->method('execute') ->willReturn(true); $this->getCategoryKeyMock ->expects(self::once()) ->method('execute') ->willReturn(''); $this->configMock ->expects(self::once()) ->method('canBlockUnknownCookie') ->willReturn($canBlockUnknownCookie); $this->isAllowedCategoryMock ->expects(self::never()) ->method('execute'); self::assertSame($result, $this->isAllowedModel->execute('test_cookie_name')); } public function canBlockUnknownCookieProvider(): array { return [ [ 'canBlockUnknownCookie' => true, 'result' => false, ], [ 'canBlockUnknownCookie' => false, 'result' => true, ], ]; } /** * @dataProvider isAllowedCategoryProvider * * @param bool $isAllowedCategory * @param bool $result */ public function testCookieCategoryValidation(bool $isAllowedCategory, bool $result) { $this->canManageCookieMock ->expects(self::once()) ->method('execute') ->willReturn(true); $this->isUserOptInMock ->expects(self::once()) ->method('execute') ->willReturn(true); $this->getCategoryKeyMock ->expects(self::exactly(2)) ->method('execute') ->willReturn('test_category'); $this->configMock ->expects(self::never()) ->method('canBlockUnknownCookie'); $this->isAllowedCategoryMock ->expects(self::once()) ->method('execute') ->willReturn($isAllowedCategory); self::assertSame($result, $this->isAllowedModel->execute('test_cookie_name')); } public function isAllowedCategoryProvider(): array { return [ [ 'isAllowedCategory' => true, 'result' => true, ], [ 'isAllowedCategory' => false, 'result' => false, ], ]; } }