diff --git a/src/Component/Checker/CallableChecker.php b/src/Component/Checker/CallableChecker.php new file mode 100644 index 00000000..3bb63735 --- /dev/null +++ b/src/Component/Checker/CallableChecker.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Component/Checker/IsEqualChecker.php b/src/Component/Checker/IsEqualChecker.php new file mode 100644 index 00000000..d826475c --- /dev/null +++ b/src/Component/Checker/IsEqualChecker.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/tests/Component/Checker/CallableCheckerTest.php b/tests/Component/Checker/CallableCheckerTest.php new file mode 100644 index 00000000..d90c89de --- /dev/null +++ b/tests/Component/Checker/CallableCheckerTest.php @@ -0,0 +1,88 @@ +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()); + } +} diff --git a/tests/Component/Checker/IsEqualCheckerTest.php b/tests/Component/Checker/IsEqualCheckerTest.php new file mode 100644 index 00000000..2783f68f --- /dev/null +++ b/tests/Component/Checker/IsEqualCheckerTest.php @@ -0,0 +1,58 @@ +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()); + } +}