Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Split authentication methods
  • Loading branch information
TavoNiievez committed Sep 6, 2021
commit 448d9854f8ef6c40e376170ef7e18edcbe6ac619
81 changes: 3 additions & 78 deletions src/Codeception/Module/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
use Closure;
use Codeception\Configuration;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Connector\Laravel as LaravelConnector;
use Codeception\Lib\Framework;
use Codeception\Lib\Interfaces\ActiveRecord;
use Codeception\Lib\Interfaces\PartedModule;
use Codeception\Lib\ModuleContainer;
use Codeception\Module\Laravel\InteractsWithAuthentication;
use Codeception\Subscriber\ErrorHandler;
use Codeception\TestInterface;
use Codeception\Util\ReflectionHelper;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Factory as AuthContract;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Session\Session;
Expand Down Expand Up @@ -132,6 +130,8 @@
*/
class Laravel extends Framework implements ActiveRecord, PartedModule
{
use InteractsWithAuthentication;

/**
* @var Application
*/
Expand Down Expand Up @@ -791,81 +791,6 @@ public function seeFormErrorMessage(string $field, $errorMessage = null): void
}
}

/**
* Set the currently logged in user for the application.
* Takes either an object that implements the User interface or
* an array of credentials.
*
* ``` php
* <?php
* // provide array of credentials
* $I->amLoggedAs(['username' => 'jane@example.com', 'password' => 'password']);
*
* // provide User object
* $I->amLoggedAs( new User );
*
* // can be verified with $I->seeAuthentication();
* ```
* @param Authenticatable|array $user
* @param string|null $guardName The guard name
*/
public function amLoggedAs($user, ?string $guardName = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

$guard = $auth->guard($guardName);

if ($user instanceof Authenticatable) {
$guard->login($user);
return;
}

$this->assertTrue($guard->attempt($user), 'Failed to login with credentials ' . json_encode($user));
}

/**
* Logout user.
*/
public function logout(): void
{
$this->app['auth']->logout();
}

/**
* Checks that a user is authenticated.
* You can specify the guard that should be use as second parameter.
*
* @param string|null $guard
*/
public function seeAuthentication($guard = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

$auth = $auth->guard($guard);

$this->assertTrue($auth->check(), 'There is no authenticated user');
}

/**
* Check that user is not authenticated.
* You can specify the guard that should be use as second parameter.
*
* @param string|null $guard
*/
public function dontSeeAuthentication(?string $guard = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

if (is_string($guard)) {
$auth = $auth->guard($guard);
}

$this->assertNotTrue($auth->check(), 'There is an user authenticated');
}

/**
* Return an instance of a class from the Laravel service container.
* (https://laravel.com/docs/master/container)
Expand Down
86 changes: 86 additions & 0 deletions src/Codeception/Module/Laravel/InteractsWithAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Codeception\Module\Laravel;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Factory as AuthContract;

trait InteractsWithAuthentication
{
/**
* Set the currently logged in user for the application.
* Takes either an object that implements the User interface or
* an array of credentials.
*
* ``` php
* <?php
* // provide array of credentials
* $I->amLoggedAs(['username' => 'jane@example.com', 'password' => 'password']);
*
* // provide User object
* $I->amLoggedAs( new User );
*
* // can be verified with $I->seeAuthentication();
* ```
* @param Authenticatable|array $user
* @param string|null $guardName The guard name
*/
public function amLoggedAs($user, ?string $guardName = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

$guard = $auth->guard($guardName);

if ($user instanceof Authenticatable) {
$guard->login($user);
return;
}

$this->assertTrue($guard->attempt($user), 'Failed to login with credentials ' . json_encode($user));
}

/**
* Check that user is not authenticated.
* You can specify the guard that should be use as second parameter.
*
* @param string|null $guard
*/
public function dontSeeAuthentication(?string $guard = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

if (is_string($guard)) {
$auth = $auth->guard($guard);
}

$this->assertNotTrue($auth->check(), 'There is an user authenticated');
}

/**
* Checks that a user is authenticated.
* You can specify the guard that should be use as second parameter.
*
* @param string|null $guard
*/
public function seeAuthentication($guard = null): void
{
/** @var AuthContract $auth */
$auth = $this->app['auth'];

$auth = $auth->guard($guard);

$this->assertTrue($auth->check(), 'There is no authenticated user');
}

/**
* Logout user.
*/
public function logout(): void
{
$this->app['auth']->logout();
}
}