-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
411 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Contracts\Auth\Guard; | ||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class GuardAssert extends AbstractExpectationCallsMap implements Guard | ||
{ | ||
/** | ||
* @param array<GuardCheckExpectation|null> $check | ||
* @param array<GuardGuestExpectation|null> $guest | ||
* @param array<GuardUserExpectation|null> $user | ||
* @param array<GuardIdExpectation|null> $id | ||
* @param array<GuardValidateExpectation|null> $validate | ||
* @param array<GuardHasUserExpectation|null> $hasUser | ||
* @param array<GuardSetUserExpectation|null> $setUser | ||
*/ | ||
public function __construct( | ||
array $check = [], | ||
array $guest = [], | ||
array $user = [], | ||
array $id = [], | ||
array $validate = [], | ||
array $hasUser = [], | ||
array $setUser = [], | ||
) { | ||
$this->setExpectations(GuardCheckExpectation::class, array_values(array_filter($check))); | ||
$this->setExpectations(GuardGuestExpectation::class, array_values(array_filter($guest))); | ||
$this->setExpectations(GuardUserExpectation::class, array_values(array_filter($user))); | ||
$this->setExpectations(GuardIdExpectation::class, array_values(array_filter($id))); | ||
$this->setExpectations(GuardValidateExpectation::class, array_values(array_filter($validate))); | ||
$this->setExpectations(GuardHasUserExpectation::class, array_values(array_filter($hasUser))); | ||
$this->setExpectations(GuardSetUserExpectation::class, array_values(array_filter($setUser))); | ||
} | ||
|
||
/** | ||
* Determine if the current user is authenticated. | ||
* | ||
* @return bool | ||
*/ | ||
public function check() | ||
{ | ||
$expectation = $this->getExpectation(GuardCheckExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Determine if the current user is a guest. | ||
* | ||
* @return bool | ||
*/ | ||
public function guest() | ||
{ | ||
$expectation = $this->getExpectation(GuardGuestExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Get the currently authenticated user. | ||
* | ||
* @return Authenticatable|null | ||
*/ | ||
public function user() | ||
{ | ||
$expectation = $this->getExpectation(GuardUserExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Get the ID for the currently authenticated user. | ||
* | ||
* @return int|string|null | ||
*/ | ||
public function id() | ||
{ | ||
$expectation = $this->getExpectation(GuardIdExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Validate a user's credentials. | ||
* | ||
* @return bool | ||
*/ | ||
public function validate(array $credentials = []) | ||
{ | ||
$expectation = $this->getExpectation(GuardValidateExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->credentials, $credentials, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $credentials, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Determine if the guard has a user instance. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasUser() | ||
{ | ||
$expectation = $this->getExpectation(GuardHasUserExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Set the current user. | ||
*/ | ||
public function setUser(Authenticatable $user) | ||
{ | ||
$expectation = $this->getExpectation(GuardSetUserExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->user, $user, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $user, $expectation); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Testing/Laravel/Contracts/Auth/GuardCheckExpectation.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardCheckExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Testing/Laravel/Contracts/Auth/GuardGuestExpectation.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardGuestExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Testing/Laravel/Contracts/Auth/GuardHasUserExpectation.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardHasUserExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardIdExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Testing/Laravel/Contracts/Auth/GuardSetUserExpectation.php
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
|
||
final class GuardSetUserExpectation | ||
{ | ||
/** | ||
* @param Closure(Authenticatable, self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly Authenticatable $user, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Testing/Laravel/Contracts/Auth/GuardUserExpectation.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardUserExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Testing/Laravel/Contracts/Auth/GuardValidateExpectation.php
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Auth; | ||
|
||
use Closure; | ||
|
||
final class GuardValidateExpectation | ||
{ | ||
/** | ||
* @param Closure(array, self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly array $credentials = [], | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
Oops, something went wrong.