diff --git a/CHANGELOG.md b/CHANGELOG.md index ad3bea0..95868c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,17 @@ ### Changed -- `Innmind\HttpAuthentication\Authenticator::__invoke()` now returns an `Innmind\Immutable\Attempt` -- All resolvers now return an `Innmind\Immutable\Attempt` +- All authenticators now return an `Innmind\Immutable\Attempt` +- All authenticators can return any type inside the `Attempt` + +### Removed + +- All resolvers have been replaced by `callable`s +- `Innmind\HttpAuthentication\ViaStorage` +- `Innmind\HttpAuthentication\Any` +- `Innmind\HttpAuthentication\ValidateAuthorizationHeader` +- `Innmind\HttpAuthentication\Authenticator` +- `Innmind\HttpAuthentication\Identity` ## 4.0.0 - 2023-11-01 diff --git a/src/Any.php b/src/Any.php deleted file mode 100644 index fb36908..0000000 --- a/src/Any.php +++ /dev/null @@ -1,35 +0,0 @@ - */ - private Sequence $authenticators; - - /** - * @no-named-arguments - */ - public function __construct(Authenticator ...$authenticators) - { - $this->authenticators = Sequence::of(...$authenticators); - } - - public function __invoke(ServerRequest $request): Attempt - { - /** @var Attempt */ - return $this->authenticators->reduce( - Attempt::error(new \LogicException('No authenticator defined')), - static fn(Attempt $identity, $authenticate) => $identity->recover( - static fn() => $authenticate($request), - ), - ); - } -} diff --git a/src/Authenticator.php b/src/Authenticator.php deleted file mode 100644 index 9c5c5d9..0000000 --- a/src/Authenticator.php +++ /dev/null @@ -1,15 +0,0 @@ - - */ - public function __invoke(ServerRequest $request): Attempt; -} diff --git a/src/Identity.php b/src/Identity.php deleted file mode 100644 index 1557d35..0000000 --- a/src/Identity.php +++ /dev/null @@ -1,9 +0,0 @@ -authenticate = $authenticate; - } - - public function __invoke(ServerRequest $request): Attempt - { - if (!$request->headers()->contains('Authorization')) { - return ($this->authenticate)($request); - } - - return $request - ->headers() - ->find(Authorization::class) - ->attempt(static fn() => new \RuntimeException('No Authorization header')) - ->flatMap(fn() => ($this->authenticate)($request)); - } -} diff --git a/src/ViaAuthorization.php b/src/ViaAuthorization.php index 838c881..4374121 100644 --- a/src/ViaAuthorization.php +++ b/src/ViaAuthorization.php @@ -3,7 +3,6 @@ namespace Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\ViaAuthorization\Resolver; use Innmind\Http\{ ServerRequest, Header\Authorization, @@ -14,15 +13,25 @@ Predicate\Instance, }; -final class ViaAuthorization implements Authenticator +/** + * @template T + */ +final class ViaAuthorization { - private Resolver $resolve; + /** @var callable(AuthorizationValue): Attempt */ + private $resolve; - public function __construct(Resolver $resolve) + /** + * @param callable(AuthorizationValue): Attempt $resolve + */ + public function __construct(callable $resolve) { $this->resolve = $resolve; } + /** + * @return Attempt + */ public function __invoke(ServerRequest $request): Attempt { return $request diff --git a/src/ViaAuthorization/NullResolver.php b/src/ViaAuthorization/NullResolver.php deleted file mode 100644 index b7d2497..0000000 --- a/src/ViaAuthorization/NullResolver.php +++ /dev/null @@ -1,17 +0,0 @@ - */ - return Attempt::error(new \LogicException('Not implemented')); - } -} diff --git a/src/ViaAuthorization/Resolver.php b/src/ViaAuthorization/Resolver.php deleted file mode 100644 index c5135ca..0000000 --- a/src/ViaAuthorization/Resolver.php +++ /dev/null @@ -1,16 +0,0 @@ - - */ - public function __invoke(AuthorizationValue $value): Attempt; -} diff --git a/src/ViaBasicAuthorization.php b/src/ViaBasicAuthorization.php index 003c2b3..7caa170 100644 --- a/src/ViaBasicAuthorization.php +++ b/src/ViaBasicAuthorization.php @@ -3,22 +3,31 @@ namespace Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\ViaBasicAuthorization\Resolver; use Innmind\Http\{ ServerRequest, Header\Authorization, }; use Innmind\Immutable\Attempt; -final class ViaBasicAuthorization implements Authenticator +/** + * @template T + */ +final class ViaBasicAuthorization { - private Resolver $resolve; + /** @var callable(string, string): Attempt */ + private $resolve; - public function __construct(Resolver $resolve) + /** + * @param callable(string, string): Attempt $resolve + */ + public function __construct(callable $resolve) { $this->resolve = $resolve; } + /** + * @return Attempt + */ public function __invoke(ServerRequest $request): Attempt { return $request diff --git a/src/ViaBasicAuthorization/NullResolver.php b/src/ViaBasicAuthorization/NullResolver.php deleted file mode 100644 index 9ee4c15..0000000 --- a/src/ViaBasicAuthorization/NullResolver.php +++ /dev/null @@ -1,16 +0,0 @@ - */ - return Attempt::error(new \LogicException('Not implemented')); - } -} diff --git a/src/ViaBasicAuthorization/Resolver.php b/src/ViaBasicAuthorization/Resolver.php deleted file mode 100644 index d5ac2fb..0000000 --- a/src/ViaBasicAuthorization/Resolver.php +++ /dev/null @@ -1,15 +0,0 @@ - - */ - public function __invoke(string $user, string $password): Attempt; -} diff --git a/src/ViaForm.php b/src/ViaForm.php index 1311ddd..002b4ee 100644 --- a/src/ViaForm.php +++ b/src/ViaForm.php @@ -3,9 +3,9 @@ namespace Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\ViaForm\Resolver; use Innmind\Http\{ ServerRequest, + ServerRequest\Form, Method, }; use Innmind\Immutable\{ @@ -13,15 +13,25 @@ Attempt, }; -final class ViaForm implements Authenticator +/** + * @template T + */ +final class ViaForm { - private Resolver $resolve; + /** @var callable(Form): Attempt */ + private $resolve; - public function __construct(Resolver $resolve) + /** + * @param callable(Form): Attempt $resolve + */ + public function __construct(callable $resolve) { $this->resolve = $resolve; } + /** + * @return Attempt + */ public function __invoke(ServerRequest $request): Attempt { return Maybe::just($request) diff --git a/src/ViaForm/NullResolver.php b/src/ViaForm/NullResolver.php deleted file mode 100644 index 251f6a2..0000000 --- a/src/ViaForm/NullResolver.php +++ /dev/null @@ -1,17 +0,0 @@ - */ - return Attempt::error(new \LogicException('Not implemented')); - } -} diff --git a/src/ViaForm/Resolver.php b/src/ViaForm/Resolver.php deleted file mode 100644 index 82110b2..0000000 --- a/src/ViaForm/Resolver.php +++ /dev/null @@ -1,16 +0,0 @@ - - */ - public function __invoke(Form $form): Attempt; -} diff --git a/src/ViaStorage.php b/src/ViaStorage.php deleted file mode 100644 index 5015b87..0000000 --- a/src/ViaStorage.php +++ /dev/null @@ -1,37 +0,0 @@ -authenticate = $authenticate; - $this->storage = $storage; - } - - public function __invoke(ServerRequest $request): Attempt - { - return $this - ->storage - ->get($request) - ->attempt(static fn() => new \RuntimeException('Identity not in storage')) - ->recover( - fn() => ($this->authenticate)($request)->map( - function($identity) use ($request) { - $this->storage->set($request, $identity); - - return $identity; - }, - ), - ); - } -} diff --git a/src/ViaStorage/InMemory.php b/src/ViaStorage/InMemory.php deleted file mode 100644 index 6a40efc..0000000 --- a/src/ViaStorage/InMemory.php +++ /dev/null @@ -1,32 +0,0 @@ - */ - private Map $identities; - - public function __construct() - { - $this->identities = Map::of(); - } - - public function get(ServerRequest $request): Maybe - { - return $this->identities->get($request); - } - - public function set(ServerRequest $request, Identity $identity): void - { - $this->identities = ($this->identities)($request, $identity); - } -} diff --git a/src/ViaStorage/NullStorage.php b/src/ViaStorage/NullStorage.php deleted file mode 100644 index 8e0676d..0000000 --- a/src/ViaStorage/NullStorage.php +++ /dev/null @@ -1,22 +0,0 @@ - */ - return Maybe::nothing(); - } - - public function set(ServerRequest $request, Identity $identity): void - { - // pass - } -} diff --git a/src/ViaStorage/Storage.php b/src/ViaStorage/Storage.php deleted file mode 100644 index 7becdaf..0000000 --- a/src/ViaStorage/Storage.php +++ /dev/null @@ -1,17 +0,0 @@ - - */ - public function get(ServerRequest $request): Maybe; - public function set(ServerRequest $request, Identity $identity): void; -} diff --git a/src/ViaUrlAuthority.php b/src/ViaUrlAuthority.php index 29b8e25..aecb7b4 100644 --- a/src/ViaUrlAuthority.php +++ b/src/ViaUrlAuthority.php @@ -3,7 +3,6 @@ namespace Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\ViaUrlAuthority\Resolver; use Innmind\Http\ServerRequest; use Innmind\Url\Authority\UserInformation\{ User, @@ -11,22 +10,32 @@ }; use Innmind\Immutable\Attempt; -final class ViaUrlAuthority implements Authenticator +/** + * @template T + */ +final class ViaUrlAuthority { - private Resolver $resolve; + /** @var callable(User, Password): Attempt */ + private $resolve; - public function __construct(Resolver $resolve) + /** + * @param callable(User, Password): Attempt $resolve + */ + public function __construct(callable $resolve) { $this->resolve = $resolve; } + /** + * @return Attempt + */ public function __invoke(ServerRequest $request): Attempt { $user = $request->url()->authority()->userInformation()->user(); $password = $request->url()->authority()->userInformation()->password(); if ($user->equals(User::none()) && $password->equals(Password::none())) { - /** @var Attempt */ + /** @var Attempt */ return Attempt::error(new \RuntimeException('No authentication provided')); } diff --git a/src/ViaUrlAuthority/NullResolver.php b/src/ViaUrlAuthority/NullResolver.php deleted file mode 100644 index e3e5014..0000000 --- a/src/ViaUrlAuthority/NullResolver.php +++ /dev/null @@ -1,20 +0,0 @@ - */ - return Attempt::error(new \LogicException('Not implemented')); - } -} diff --git a/src/ViaUrlAuthority/Resolver.php b/src/ViaUrlAuthority/Resolver.php deleted file mode 100644 index 6748d0f..0000000 --- a/src/ViaUrlAuthority/Resolver.php +++ /dev/null @@ -1,19 +0,0 @@ - - */ - public function __invoke(User $user, Password $password): Attempt; -} diff --git a/tests/AnyTest.php b/tests/AnyTest.php deleted file mode 100644 index 826d02b..0000000 --- a/tests/AnyTest.php +++ /dev/null @@ -1,78 +0,0 @@ -assertInstanceOf(Authenticator::class, new Any); - } - - public function testReturnNothingWhenNoAuthenticationProvided() - { - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertNull((new Any)($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testInvokation() - { - $authenticate = new Any( - $notSupported = $this->createMock(Authenticator::class), - $notImplemented = $this->createMock(Authenticator::class), - $expected = $this->createMock(Authenticator::class), - $notCalled = $this->createMock(Authenticator::class), - ); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - $notSupported - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::error(new \Exception)); - $notImplemented - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::error(new \Exception)); - $expected - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); - $notCalled - ->expects($this->never()) - ->method('__invoke'); - - $this->assertSame($identity, $authenticate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ValidateAuthorizationHeaderTest.php b/tests/ValidateAuthorizationHeaderTest.php deleted file mode 100644 index 7c2eff1..0000000 --- a/tests/ValidateAuthorizationHeaderTest.php +++ /dev/null @@ -1,111 +0,0 @@ -assertInstanceOf( - Authenticator::class, - new ValidateAuthorizationHeader( - $this->createMock(Authenticator::class), - ), - ); - } - - public function testForwardAuthenticationWhenNoHeader() - { - $validate = new ValidateAuthorizationHeader( - $authenticate = $this->createMock(Authenticator::class), - ); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - $authenticate - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::result($expected = $this->createMock(Identity::class))); - - $this->assertSame($expected, $validate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testReturnNothingWhenHeaderNotOfExpectedType() - { - $validate = new ValidateAuthorizationHeader( - $authenticate = $this->createMock(Authenticator::class), - ); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - Headers::of( - new Header( - 'Authorization', - new Value('Bearer foo'), - ), - ), - ); - $authenticate - ->expects($this->never()) - ->method('__invoke'); - - $this->assertNull($validate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testForwardAuthenticationWhenValidHeader() - { - $validate = new ValidateAuthorizationHeader( - $authenticate = $this->createMock(Authenticator::class), - ); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - Headers::of( - new Authorization( - new AuthorizationValue('Bearer', 'foo'), - ), - ), - ); - $authenticate - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::result($expected = $this->createMock(Identity::class))); - - $this->assertSame($expected, $validate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaAuthorization/NullResolverTest.php b/tests/ViaAuthorization/NullResolverTest.php deleted file mode 100644 index e101105..0000000 --- a/tests/ViaAuthorization/NullResolverTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertInstanceOf(Resolver::class, new NullResolver); - } - - public function testInvokation() - { - $this->assertNull((new NullResolver)(new AuthorizationValue('Bearer', 'foo'))->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaAuthorizationTest.php b/tests/ViaAuthorizationTest.php index 7e55a8e..90ba5cf 100644 --- a/tests/ViaAuthorizationTest.php +++ b/tests/ViaAuthorizationTest.php @@ -3,13 +3,7 @@ namespace Tests\Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\{ - ViaAuthorization, - ViaAuthorization\Resolver, - ViaAuthorization\NullResolver, - Authenticator, - Identity, -}; +use Innmind\HttpAuthentication\ViaAuthorization; use Innmind\Http\{ ServerRequest, Method, @@ -26,22 +20,11 @@ class ViaAuthorizationTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Authenticator::class, - new ViaAuthorization(new NullResolver), - ); - } - public function testReturnNothingWhenNoAuthorizationHeader() { $authenticate = new ViaAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -57,11 +40,8 @@ public function testReturnNothingWhenNoAuthorizationHeader() public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() { $authenticate = new ViaAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -80,14 +60,9 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() public function testInvokation() { $authenticate = new ViaAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn($value) => Attempt::result($value), ); $expected = new AuthorizationValue('Bearer', 'foo'); - $resolver - ->expects($this->once()) - ->method('__invoke') - ->with($expected) - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -97,7 +72,7 @@ public function testInvokation() ), ); - $this->assertSame($identity, $authenticate($request)->match( + $this->assertSame($expected, $authenticate($request)->match( static fn($identity) => $identity, static fn() => null, )); diff --git a/tests/ViaBasicAuthorization/NullResolverTest.php b/tests/ViaBasicAuthorization/NullResolverTest.php deleted file mode 100644 index fc50b0b..0000000 --- a/tests/ViaBasicAuthorization/NullResolverTest.php +++ /dev/null @@ -1,26 +0,0 @@ -assertInstanceOf(Resolver::class, new NullResolver); - } - - public function testInvokation() - { - $this->assertNull((new NullResolver)('user', 'password')->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaBasicAuthorizationTest.php b/tests/ViaBasicAuthorizationTest.php index 439bac8..e12ebdf 100644 --- a/tests/ViaBasicAuthorizationTest.php +++ b/tests/ViaBasicAuthorizationTest.php @@ -3,13 +3,7 @@ namespace Tests\Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\{ - ViaBasicAuthorization, - ViaBasicAuthorization\Resolver, - ViaBasicAuthorization\NullResolver, - Authenticator, - Identity, -}; +use Innmind\HttpAuthentication\ViaBasicAuthorization; use Innmind\Http\{ ServerRequest, Method, @@ -25,22 +19,11 @@ class ViaBasicAuthorizationTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Authenticator::class, - new ViaBasicAuthorization(new NullResolver), - ); - } - public function testReturnNothingWhenNoAuthorizationHeader() { $authenticate = new ViaBasicAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -56,11 +39,8 @@ public function testReturnNothingWhenNoAuthorizationHeader() public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() { $authenticate = new ViaBasicAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -79,11 +59,8 @@ public function testReturnNothingWhenAuthorizationHeaderNotParsedCorrectly() public function testReturnNothingWhenNotBasicAuthorization() { $authenticate = new ViaBasicAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -102,13 +79,8 @@ public function testReturnNothingWhenNotBasicAuthorization() public function testInvokation() { $authenticate = new ViaBasicAuthorization( - $resolver = $this->createMock(Resolver::class), + static fn($user, $password) => Attempt::result([$user, $password]), ); - $resolver - ->expects($this->once()) - ->method('__invoke') - ->with('foo', 'bar') - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -118,7 +90,7 @@ public function testInvokation() ), ); - $this->assertSame($identity, $authenticate($request)->match( + $this->assertSame(['foo', 'bar'], $authenticate($request)->match( static fn($identity) => $identity, static fn() => null, )); diff --git a/tests/ViaForm/NullResolverTest.php b/tests/ViaForm/NullResolverTest.php deleted file mode 100644 index 2da9adc..0000000 --- a/tests/ViaForm/NullResolverTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertInstanceOf(Resolver::class, new NullResolver); - } - - public function testInvokation() - { - $this->assertNull((new NullResolver)(Form::of([]))->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaFormTest.php b/tests/ViaFormTest.php index d725dae..a8f2a50 100644 --- a/tests/ViaFormTest.php +++ b/tests/ViaFormTest.php @@ -3,13 +3,7 @@ namespace Tests\Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\{ - ViaForm, - Authenticator, - ViaForm\Resolver, - ViaForm\NullResolver, - Identity, -}; +use Innmind\HttpAuthentication\ViaForm; use Innmind\Http\{ ServerRequest, Method, @@ -21,22 +15,11 @@ class ViaFormTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Authenticator::class, - new ViaForm(new NullResolver), - ); - } - public function testReturnNothingWhenNotPostRequest() { $authenticate = new ViaForm( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( Url::of('/'), Method::get, @@ -52,20 +35,15 @@ public function testReturnNothingWhenNotPostRequest() public function testInvokation() { $authenticate = new ViaForm( - $resolver = $this->createMock(Resolver::class), + static fn($value) => Attempt::result($value), ); $request = ServerRequest::of( Url::of('/'), Method::post, ProtocolVersion::v11, ); - $resolver - ->expects($this->once()) - ->method('__invoke') - ->with($request->form()) - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); - $this->assertSame($identity, $authenticate($request)->match( + $this->assertSame($request->form(), $authenticate($request)->match( static fn($identity) => $identity, static fn() => null, )); diff --git a/tests/ViaStorage/InMemoryTest.php b/tests/ViaStorage/InMemoryTest.php deleted file mode 100644 index 2cd2b79..0000000 --- a/tests/ViaStorage/InMemoryTest.php +++ /dev/null @@ -1,76 +0,0 @@ -assertInstanceOf(Storage::class, new InMemory); - } - - public function testGet() - { - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertNull((new InMemory)->get($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testHas() - { - $storage = new InMemory; - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertFalse($storage->get($request)->match( - static fn() => true, - static fn() => false, - )); - $storage->set($request, $this->createMock(Identity::class)); - $this->assertTrue($storage->get($request)->match( - static fn() => true, - static fn() => false, - )); - } - - public function testSet() - { - $storage = new InMemory; - $identity = $this->createMock(Identity::class); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertNull($storage->set($request, $identity)); - $this->assertSame($identity, $storage->get($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaStorage/NullStorageTest.php b/tests/ViaStorage/NullStorageTest.php deleted file mode 100644 index aed953f..0000000 --- a/tests/ViaStorage/NullStorageTest.php +++ /dev/null @@ -1,79 +0,0 @@ -assertInstanceOf(Storage::class, new NullStorage); - } - - public function testGet() - { - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertNull((new NullStorage)->get($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testHas() - { - $storage = new NullStorage; - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertFalse($storage->get($request)->match( - static fn() => true, - static fn() => false, - )); - $storage->set($request, $this->createMock(Identity::class)); - $this->assertFalse($storage->get($request)->match( - static fn() => true, - static fn() => false, - )); - } - - public function testSet() - { - $storage = new NullStorage; - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - - $this->assertNull($storage->set( - $request, - $this->createMock(Identity::class), - )); - - $this->assertNull($storage->get($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaStorageTest.php b/tests/ViaStorageTest.php deleted file mode 100644 index 3d348df..0000000 --- a/tests/ViaStorageTest.php +++ /dev/null @@ -1,88 +0,0 @@ -assertInstanceOf( - Authenticator::class, - new ViaStorage( - $this->createMock(Authenticator::class), - new InMemory, - ), - ); - } - - public function testAuthenticateViaInnerAuthenticator() - { - $authenticate = new ViaStorage( - $inner = $this->createMock(Authenticator::class), - $storage = new InMemory, - ); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - $inner - ->expects($this->once()) - ->method('__invoke') - ->with($request) - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); - - $this->assertSame($identity, $authenticate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - $this->assertTrue($storage->get($request)->match( - static fn() => true, - static fn() => false, - )); - // second time is to make sure it uses the storage - $this->assertSame($identity, $authenticate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } - - public function testAuthenticateViaStorage() - { - $authenticate = new ViaStorage( - $inner = $this->createMock(Authenticator::class), - $storage = new InMemory, - ); - $identity = $this->createMock(Identity::class); - $request = ServerRequest::of( - Url::of('/'), - Method::get, - ProtocolVersion::v11, - ); - $inner - ->expects($this->never()) - ->method('__invoke'); - $storage->set($request, $identity); - - $this->assertSame($identity, $authenticate($request)->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaUrlAuthority/NullResolverTest.php b/tests/ViaUrlAuthority/NullResolverTest.php deleted file mode 100644 index 26e54db..0000000 --- a/tests/ViaUrlAuthority/NullResolverTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertInstanceOf(Resolver::class, new NullResolver); - } - - public function testInvokation() - { - $this->assertNull((new NullResolver)( - User::none(), - Password::none(), - )->match( - static fn($identity) => $identity, - static fn() => null, - )); - } -} diff --git a/tests/ViaUrlAuthorityTest.php b/tests/ViaUrlAuthorityTest.php index 1dba920..8048219 100644 --- a/tests/ViaUrlAuthorityTest.php +++ b/tests/ViaUrlAuthorityTest.php @@ -3,13 +3,7 @@ namespace Tests\Innmind\HttpAuthentication; -use Innmind\HttpAuthentication\{ - ViaUrlAuthority, - Authenticator, - ViaUrlAuthority\Resolver, - ViaUrlAuthority\NullResolver, - Identity, -}; +use Innmind\HttpAuthentication\ViaUrlAuthority; use Innmind\Url\Url; use Innmind\Http\{ ServerRequest, @@ -21,23 +15,12 @@ class ViaUrlAuthorityTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Authenticator::class, - new ViaUrlAuthority(new NullResolver), - ); - } - public function testReturnNothingWhenNoUserProvidedInTheUrl() { $authenticate = new ViaUrlAuthority( - $resolver = $this->createMock(Resolver::class), + static fn() => throw new \Exception, ); $url = Url::of('https://localhost/'); - $resolver - ->expects($this->never()) - ->method('__invoke'); $request = ServerRequest::of( $url, Method::get, @@ -53,23 +36,18 @@ public function testReturnNothingWhenNoUserProvidedInTheUrl() public function testInvokation() { $authenticate = new ViaUrlAuthority( - $resolver = $this->createMock(Resolver::class), + static fn($user, $password) => Attempt::result([$user, $password]), ); $url = Url::of('https://user:password@localhost/'); $user = $url->authority()->userInformation()->user(); $password = $url->authority()->userInformation()->password(); - $resolver - ->expects($this->once()) - ->method('__invoke') - ->with($user, $password) - ->willReturn(Attempt::result($identity = $this->createMock(Identity::class))); $request = ServerRequest::of( $url, Method::get, ProtocolVersion::v11, ); - $this->assertSame($identity, $authenticate($request)->match( + $this->assertSame([$user, $password], $authenticate($request)->match( static fn($identity) => $identity, static fn() => null, ));