Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Context/CookieBasedVatContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public function displayWithVat(): bool
throw new NoVatContextException();
}

$cookie = $request->cookies->get($this->cookieName);
if (null === $cookie) {
throw new NoVatContextException();
}

return (bool) $cookie;
// Anything but the two values written by the toggle action is treated as 'no context', which makes the
// composite context fall through to the next context instead of interpreting an arbitrary value
return match ($request->cookies->get($this->cookieName)) {
'1' => true,
'0' => false,
default => throw new NoVatContextException(),
};
}
}
37 changes: 35 additions & 2 deletions tests/Context/CookieBasedVatContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function it_throws_exception_if_cookie_is_not_set(): void
}

/** @test */
public function it_returns_true_if_cookie_value_is_truthy(): void
public function it_returns_true_if_cookie_value_is_one(): void
{
$this->requestStack->push(Request::create(
uri: '/',
Expand All @@ -69,7 +69,7 @@ public function it_returns_true_if_cookie_value_is_truthy(): void
}

/** @test */
public function it_returns_false_if_cookie_value_is_falsy(): void
public function it_returns_false_if_cookie_value_is_zero(): void
{
$this->requestStack->push(Request::create(
uri: '/',
Expand All @@ -85,4 +85,37 @@ public function it_returns_false_if_cookie_value_is_falsy(): void

$this->assertFalse($vatContext->displayWithVat());
}

/**
* @test
*
* @dataProvider unrecognizedCookieValueProvider
*/
public function it_throws_exception_if_cookie_value_is_not_recognized(string $value): void
{
$this->expectException(NoVatContextException::class);

$this->requestStack->push(Request::create(
uri: '/',
cookies: [
$this->cookieName => $value,
],
));

(new CookieBasedVatContext(
$this->requestStack,
$this->cookieName,
))->displayWithVat();
}

/**
* @return iterable<array-key, array{string}>
*/
public function unrecognizedCookieValueProvider(): iterable
{
yield 'empty string' => [''];
yield 'the string false' => ['false'];
yield 'the string true' => ['true'];
yield 'an arbitrary value' => ['yes please'];
}
}
Loading