Skip to content

Commit

Permalink
feat(Testing): Add guard assert
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Jan 20, 2023
1 parent 6639dc4 commit 6c8d651
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 0 deletions.
154 changes: 154 additions & 0 deletions src/Testing/Laravel/Contracts/Auth/GuardAssert.php
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 src/Testing/Laravel/Contracts/Auth/GuardCheckExpectation.php
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 src/Testing/Laravel/Contracts/Auth/GuardGuestExpectation.php
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 src/Testing/Laravel/Contracts/Auth/GuardHasUserExpectation.php
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,
) {
}
}
19 changes: 19 additions & 0 deletions src/Testing/Laravel/Contracts/Auth/GuardIdExpectation.php
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 src/Testing/Laravel/Contracts/Auth/GuardSetUserExpectation.php
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 src/Testing/Laravel/Contracts/Auth/GuardUserExpectation.php
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 src/Testing/Laravel/Contracts/Auth/GuardValidateExpectation.php
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,
) {
}
}
Loading

0 comments on commit 6c8d651

Please sign in to comment.