Skip to content

Commit

Permalink
Improve: Eliminate abstract requirements from NullAuthenticatable (#2)
Browse files Browse the repository at this point in the history
* Refactor: Use mock() instead of spy()

* Improve: Eliminate abstract requirements from NullAuthenticatable
  • Loading branch information
mpyw authored Dec 11, 2019
1 parent 2b9b9fe commit 20be082
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 122 deletions.
41 changes: 19 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ They heavily rely on the following flow:

This library provides a helper that makes the useless contract methods to do nothing; always return nullish or falsy values.

Now we include **`NullAuthenticatable`** trait on a user model.

```php
<?php

Expand All @@ -124,31 +126,26 @@ use Mpyw\NullAuth\NullAuthenticatable;
class User extends Model implements Authenticatable
{
use NullAuthenticatable;

/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getKeyName()};
}
}
```

You only need to implement **[`getAuthIdentifierName()`]** and **[`getAuthIdentifier()`]** along with **`NullAuthenticatable`** trait,
but you don't have to worry about anything else.
Then only **[`getAuthIdentifierName()`]** and **[`getAuthIdentifier()`]** are provided as a valid implementation.

```php
<?php

$user = User::find(1);

// Minimal implementation for Authenticatable
var_dump($user->getAuthIdentifierName()); // string(2) "id"
var_dump($user->getAuthIdentifier()); // int(1)

// Useless implementation for Authenticatable when we don't use StatefulGuard
var_dump($user->getAuthPassword()); // string(0) ""
var_dump($user->getRememberTokenName()); // string(0) ""
var_dump($user->getRememberToken()); // string(0) ""
$user->setRememberToken('...'); // Does nothing
```

### Middleware-based Authentication

Expand Down
31 changes: 31 additions & 0 deletions src/Concerns/HasEloquentIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Mpyw\NullAuth\Concerns;

/**
* Trait HasEloquentIdentifier
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait HasEloquentIdentifier
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getKeyName()};
}
}
20 changes: 20 additions & 0 deletions src/Concerns/RequiresIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mpyw\NullAuth\Concerns;

trait RequiresIdentifier
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
abstract public function getAuthIdentifierName();

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
abstract public function getAuthIdentifier();
}
48 changes: 48 additions & 0 deletions src/GenericNullAuthenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Mpyw\NullAuth;

trait GenericNullAuthenticatable
{
use Concerns\RequiresIdentifier;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return '';
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return '';
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return '';
}
}
50 changes: 50 additions & 0 deletions src/GenericStrictNullAuthenticatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mpyw\NullAuth;

use LogicException;

trait GenericStrictNullAuthenticatable
{
use Concerns\RequiresIdentifier;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
throw new LogicException('Not implemented');
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
throw new LogicException('Not implemented');
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*/
public function setRememberToken($value)
{
throw new LogicException('Not implemented');
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
throw new LogicException('Not implemented');
}
}
55 changes: 2 additions & 53 deletions src/NullAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,6 @@

trait NullAuthenticatable
{
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
abstract public function getAuthIdentifierName();

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
abstract public function getAuthIdentifier();

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return '';
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return '';
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return '';
}
use GenericNullAuthenticatable,
Concerns\HasEloquentIdentifier;
}
45 changes: 2 additions & 43 deletions src/StrictNullAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,8 @@

namespace Mpyw\NullAuth;

use LogicException;

trait StrictNullAuthenticatable
{
use NullAuthenticatable;

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
throw new LogicException('Not implemented');
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
throw new LogicException('Not implemented');
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*/
public function setRememberToken($value)
{
throw new LogicException('Not implemented');
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
throw new LogicException('Not implemented');
}
use GenericStrictNullAuthenticatable,
Concerns\HasEloquentIdentifier;
}
10 changes: 10 additions & 0 deletions tests/Unit/NullAuthenticatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ public function setUp(): void

$this->user = new class($this->attributes) extends GenericUser {
use NullAuthenticatable;

public function getKeyName()
{
return 'id';
}
};

$this->strict = new class($this->attributes) extends GenericUser {
use StrictNullAuthenticatable;

public function getKeyName()
{
return 'id';
}
};
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/NullGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function testUser(): void

public function testValidate(): void
{
$this->guard = Mockery::spy(NullGuard::class . '[user]');
$this->guard = Mockery::mock(NullGuard::class . '[user]');
$this->assertFalse($this->guard->validate(['email' => 'xxx@example.com', 'password' => 'abc123']));
$this->guard->shouldNotHaveReceived('user');

$this->guard = Mockery::spy(NullGuard::class . '[user]');
$this->guard = Mockery::mock(NullGuard::class . '[user]');
$this->assertFalse($this->guard->validate(['email' => 'xxx@example.com', 'password' => 'abc123']));
$this->guard->shouldNotHaveReceived('user');
}
Expand All @@ -66,12 +66,12 @@ public function testAuthenticate(): void

public function testHasUser(): void
{
$this->guard = Mockery::spy(NullGuard::class . '[user]');
$this->guard = Mockery::mock(NullGuard::class . '[user]');
$this->guard->setUser($this->user);
$this->assertTrue($this->guard->hasUser());
$this->guard->shouldNotHaveReceived('user');

$this->guard = Mockery::spy(NullGuard::class . '[user]');
$this->guard = Mockery::mock(NullGuard::class . '[user]');
$this->guard->unsetUser();
$this->assertFalse($this->guard->hasUser());
$this->guard->shouldNotHaveReceived('user');
Expand Down

0 comments on commit 20be082

Please sign in to comment.