Skip to content

Add IsEqualChecker and CallableChecker #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2023
Merged
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
58 changes: 58 additions & 0 deletions src/Component/Checker/CallableChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Jose\Component\Checker;

use function call_user_func;
use InvalidArgumentException;
use function is_callable;

/**
* @see \Jose\Tests\Component\Checker\CallableCheckerTest
*/
final class CallableChecker implements ClaimChecker, HeaderChecker
{
/**
* @param string $key The claim or header parameter name to check.
* @param callable(mixed $value): bool $callable The callable function that will be invoked.
*/
public function __construct(
private readonly string $key,
private $callable,
private readonly bool $protectedHeaderOnly = true
) {
if (! is_callable($this->callable)) { // @phpstan-ignore-line
throw new InvalidArgumentException('The $callable argument must be a callable.');
}
}

public function checkClaim(mixed $value): void
{
if (call_user_func($this->callable, $value) !== true) {
throw new InvalidClaimException(sprintf('The "%s" claim is invalid.', $this->key), $this->key, $value);
}
}

public function supportedClaim(): string
{
return $this->key;
}

public function checkHeader(mixed $value): void
{
if (call_user_func($this->callable, $value) !== true) {
throw new InvalidHeaderException(sprintf('The "%s" header is invalid.', $this->key), $this->key, $value);
}
}

public function supportedHeader(): string
{
return $this->key;
}

public function protectedHeaderOnly(): bool
{
return $this->protectedHeaderOnly;
}
}
53 changes: 53 additions & 0 deletions src/Component/Checker/IsEqualChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Jose\Component\Checker;

/**
* @see \Jose\Tests\Component\Checker\IsEqualCheckerTest
*/
final class IsEqualChecker implements ClaimChecker, HeaderChecker
{
/**
* @param string $key The claim or header parameter name to check.
* @param mixed $value The expected value.
* @param bool $protectedHeaderOnly [optional] Whether the header parameter MUST be protected.
* This option has no effect for claim checkers.
*/
public function __construct(
private readonly string $key,
private readonly mixed $value,
private readonly bool $protectedHeaderOnly = true
) {
}

public function checkClaim(mixed $value): void
{
if ($value !== $this->value) {
throw new InvalidClaimException(sprintf('The "%s" claim is invalid.', $this->key), $this->key, $value);
}
}

public function supportedClaim(): string
{
return $this->key;
}

public function checkHeader(mixed $value): void
{
if ($value !== $this->value) {
throw new InvalidHeaderException(sprintf('The "%s" header is invalid.', $this->key), $this->key, $value);
}
}

public function supportedHeader(): string
{
return $this->key;
}

public function protectedHeaderOnly(): bool
{
return $this->protectedHeaderOnly;
}
}
88 changes: 88 additions & 0 deletions tests/Component/Checker/CallableCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Component\Checker;

use InvalidArgumentException;
use Jose\Component\Checker\CallableChecker;
use Jose\Component\Checker\InvalidClaimException;
use Jose\Component\Checker\InvalidHeaderException;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class CallableCheckerTest extends TestCase
{
/**
* @test
*/
public function theCallableIsCallable(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The $callable argument must be a callable.');

new CallableChecker('foo', 'not_a_callable');
}

/**
* @test
*/
public function theCallableDoesNotReturnABoolean(): void
{
$this->expectException(InvalidClaimException::class);

$checker = new CallableChecker('foo', fn (mixed $value) => 1);
$checker->checkClaim('baz');

$this->expectException(InvalidHeaderException::class);

$checker = new CallableChecker('foo', fn (mixed $value) => 0);
$checker->checkHeader('baz');
}

/**
* @test
*/
public function theClaimIsInvalid(): void
{
$this->expectException(InvalidClaimException::class);

$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
$checker->checkClaim('baz');
}

/**
* @test
*/
public function theHeaderIsInvalid(): void
{
$this->expectException(InvalidHeaderException::class);

$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
$checker->checkHeader('baz');
}

/**
* @test
*/
public function theClaimIsSupported(): void
{
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
$checker->checkClaim('bar');

static::assertSame('foo', $checker->supportedClaim());
}

/**
* @test
*/
public function theHeaderIsSupported(): void
{
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
$checker->checkHeader('bar');

static::assertSame('foo', $checker->supportedHeader());
}
}
58 changes: 58 additions & 0 deletions tests/Component/Checker/IsEqualCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Component\Checker;

use Jose\Component\Checker\InvalidClaimException;
use Jose\Component\Checker\InvalidHeaderException;
use Jose\Component\Checker\IsEqualChecker;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class IsEqualCheckerTest extends TestCase
{
/**
* @test
*/
public function theClaimIsInvalid(): void
{
$this->expectException(InvalidClaimException::class);

$checker = new IsEqualChecker('foo', 'bar');
$checker->checkClaim('baz');
}

/**
* @test
*/
public function theHeaderIsInvalid(): void
{
$this->expectException(InvalidHeaderException::class);

$checker = new IsEqualChecker('foo', 'bar');
$checker->checkHeader('baz');
}

/**
* @test
*/
public function theClaimIsSupported(): void
{
$checker = new IsEqualChecker('foo', 'bar');
$checker->checkClaim('bar');
static::assertSame('foo', $checker->supportedClaim());
}

/**
* @test
*/
public function theHeaderIsSupported(): void
{
$checker = new IsEqualChecker('foo', 'bar');
$checker->checkHeader('bar');
static::assertSame('foo', $checker->supportedHeader());
}
}