From 357dded3c2a4bc9c8d9d24903583a339bd46256b Mon Sep 17 00:00:00 2001 From: mpyw Date: Fri, 27 Dec 2019 15:38:38 +0900 Subject: [PATCH] Feat: Add NoRememberTokenAuthenticatable (#4) --- README.md | 22 ++++ src/Concerns/HasEloquentPassword.php | 21 ++++ src/NoRememberTokenAuthenticatable.php | 13 ++ src/StrictNoRememberTokenAuthenticatable.php | 13 ++ .../NoRememberTokenAuthenticatableTest.php | 112 ++++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 src/Concerns/HasEloquentPassword.php create mode 100644 src/NoRememberTokenAuthenticatable.php create mode 100644 src/StrictNoRememberTokenAuthenticatable.php create mode 100644 tests/Unit/NoRememberTokenAuthenticatableTest.php diff --git a/README.md b/README.md index 9a99f56..72ded47 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,28 @@ Null Guard for Laravel. Designed for Middleware-based authentication and testing composer require mpyw/null-auth ``` +## Features + +### `NullAuthenticatable` family + +| Trait | ID | Password | Remember Token | +|:---|:---:|:---:|:---:| +| `GenericNullAuthenticatable`
`GenericStrictNullAuthenticatable` | ❗️ | ❌| ❌| +| `NullAuthenticatable`
`StrictNullAuthenticatable` | ✅| ❌| ❌| +| `NoRememberTokenAuthenticatable`
`StrictNoRememberTokenAuthenticatable` | ✅| ✅| ❌| + +- ❗️shows containing abstract methods. +- `Strict` classes throw `LogicException` on bad function calls. + +### `NullGuard` + +- `NullGuard::user()` always returns Authenticatable already set by `NullGuard::setUser()`. +- `NullGuard::unsetUser()` can unset user. + +### `NullUserProvider` + +- All methods do nothing and always returns falsy value. + ## Usage ### Basic Usage diff --git a/src/Concerns/HasEloquentPassword.php b/src/Concerns/HasEloquentPassword.php new file mode 100644 index 0000000..757ec7e --- /dev/null +++ b/src/Concerns/HasEloquentPassword.php @@ -0,0 +1,21 @@ +getAttribute('password'); + } +} diff --git a/src/NoRememberTokenAuthenticatable.php b/src/NoRememberTokenAuthenticatable.php new file mode 100644 index 0000000..7078578 --- /dev/null +++ b/src/NoRememberTokenAuthenticatable.php @@ -0,0 +1,13 @@ +attributes = [ + 'id' => 123, + 'password' => 'foo', + 'remember_token' => 'xxx', + ]; + + $this->user = new class($this->attributes) extends GenericUser { + use NoRememberTokenAuthenticatable; + + public function getKeyName() + { + return 'id'; + } + + public function getAttribute(string $key) + { + return $this->attributes[$key]; + } + }; + + $this->strict = new class($this->attributes) extends GenericUser { + use StrictNoRememberTokenAuthenticatable; + + public function getKeyName() + { + return 'id'; + } + + public function getAttribute(string $key) + { + return $this->attributes[$key]; + } + }; + } + + public function testGetAuthIdentifierName(): void + { + $this->assertSame('id', $this->user->getAuthIdentifierName()); + $this->assertSame('id', $this->strict->getAuthIdentifierName()); + } + + public function testGetAuthIdentifier(): void + { + $this->assertSame(123, $this->user->getAuthIdentifier()); + $this->assertSame(123, $this->strict->getAuthIdentifier()); + } + + public function testGetAuthPassword(): void + { + $this->assertSame('foo', $this->user->getAuthPassword()); + $this->assertSame('foo', $this->strict->getAuthPassword()); + } + + public function testGetRememberToken(): void + { + $this->assertSame('', $this->user->getRememberToken()); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Not implemented'); + $this->strict->getRememberToken(); + } + + public function testSetRememberToken(): void + { + $this->user->setRememberToken('yyy'); + $this->assertSame('xxx', $this->user->remember_token); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Not implemented'); + $this->strict->setRememberToken('yyy'); + } + + public function testGetRememberTokenName(): void + { + $this->assertSame('', $this->user->getRememberTokenName()); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Not implemented'); + $this->strict->getRememberTokenName(); + } +}