Skip to content

Commit

Permalink
Feat: Add NoRememberTokenAuthenticatable (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw authored Dec 27, 2019
1 parent 20be082 commit 357dded
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>`GenericStrictNullAuthenticatable` | ❗️ |||
| `NullAuthenticatable`<br>`StrictNullAuthenticatable` ||||
| `NoRememberTokenAuthenticatable`<br>`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
Expand Down
21 changes: 21 additions & 0 deletions src/Concerns/HasEloquentPassword.php
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');
}
}
13 changes: 13 additions & 0 deletions src/NoRememberTokenAuthenticatable.php
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;
}
}
13 changes: 13 additions & 0 deletions src/StrictNoRememberTokenAuthenticatable.php
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;
}
}
112 changes: 112 additions & 0 deletions tests/Unit/NoRememberTokenAuthenticatableTest.php
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();
}
}

0 comments on commit 357dded

Please sign in to comment.