-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add NoRememberTokenAuthenticatable (#4)
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Mpyw\NullAuth\Concerns; | ||
|
||
/** | ||
* Trait HasEloquentPassword | ||
* | ||
* @mixin \Illuminate\Database\Eloquent\Model | ||
*/ | ||
trait HasEloquentPassword | ||
{ | ||
/** | ||
* Get the unique identifier for the user. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getAuthPassword() | ||
{ | ||
return $this->getAttribute('password'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Mpyw\NullAuth; | ||
|
||
use Mpyw\NullAuth\Concerns\HasEloquentPassword; | ||
|
||
trait NoRememberTokenAuthenticatable | ||
{ | ||
use NullAuthenticatable, | ||
HasEloquentPassword { | ||
HasEloquentPassword::getAuthPassword insteadof NullAuthenticatable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Mpyw\NullAuth; | ||
|
||
use Mpyw\NullAuth\Concerns\HasEloquentPassword; | ||
|
||
trait StrictNoRememberTokenAuthenticatable | ||
{ | ||
use StrictNullAuthenticatable, | ||
HasEloquentPassword { | ||
HasEloquentPassword::getAuthPassword insteadof StrictNullAuthenticatable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Mpyw\NullAuth\Tests\Unit; | ||
|
||
use Illuminate\Auth\GenericUser; | ||
use LogicException; | ||
use Mpyw\NullAuth\NoRememberTokenAuthenticatable; | ||
use Mpyw\NullAuth\StrictNoRememberTokenAuthenticatable; | ||
use Mpyw\NullAuth\Tests\TestCase; | ||
|
||
class NoRememberTokenAuthenticatableTest extends TestCase | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $attributes; | ||
|
||
/** | ||
* @var \Illuminate\Auth\GenericUser|\Mpyw\NullAuth\NoRememberTokenAuthenticatable | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* @var \Illuminate\Auth\GenericUser|\Mpyw\NullAuth\NoRememberTokenAuthenticatable | ||
*/ | ||
protected $strict; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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(); | ||
} | ||
} |