Skip to content

Commit d68ee8d

Browse files
committed
Split authentication methods
1 parent 9f7aa17 commit d68ee8d

File tree

2 files changed

+89
-78
lines changed

2 files changed

+89
-78
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
use Closure;
88
use Codeception\Configuration;
99
use Codeception\Exception\ModuleConfigException;
10-
use Codeception\Exception\ModuleException;
1110
use Codeception\Lib\Connector\Laravel as LaravelConnector;
1211
use Codeception\Lib\Framework;
1312
use Codeception\Lib\Interfaces\ActiveRecord;
1413
use Codeception\Lib\Interfaces\PartedModule;
1514
use Codeception\Lib\ModuleContainer;
15+
use Codeception\Module\Laravel\InteractsWithAuthentication;
1616
use Codeception\Subscriber\ErrorHandler;
1717
use Codeception\TestInterface;
1818
use Codeception\Util\ReflectionHelper;
1919
use Exception;
20-
use Illuminate\Contracts\Auth\Authenticatable;
21-
use Illuminate\Contracts\Auth\Factory as AuthContract;
2220
use Illuminate\Contracts\Console\Kernel;
2321
use Illuminate\Contracts\Routing\UrlGenerator;
2422
use Illuminate\Contracts\Session\Session;
@@ -132,6 +130,8 @@
132130
*/
133131
class Laravel extends Framework implements ActiveRecord, PartedModule
134132
{
133+
use InteractsWithAuthentication;
134+
135135
/**
136136
* @var Application
137137
*/
@@ -791,81 +791,6 @@ public function seeFormErrorMessage(string $field, $errorMessage = null): void
791791
}
792792
}
793793

794-
/**
795-
* Set the currently logged in user for the application.
796-
* Takes either an object that implements the User interface or
797-
* an array of credentials.
798-
*
799-
* ``` php
800-
* <?php
801-
* // provide array of credentials
802-
* $I->amLoggedAs(['username' => 'jane@example.com', 'password' => 'password']);
803-
*
804-
* // provide User object
805-
* $I->amLoggedAs( new User );
806-
*
807-
* // can be verified with $I->seeAuthentication();
808-
* ```
809-
* @param Authenticatable|array $user
810-
* @param string|null $guardName The guard name
811-
*/
812-
public function amLoggedAs($user, ?string $guardName = null): void
813-
{
814-
/** @var AuthContract $auth */
815-
$auth = $this->app['auth'];
816-
817-
$guard = $auth->guard($guardName);
818-
819-
if ($user instanceof Authenticatable) {
820-
$guard->login($user);
821-
return;
822-
}
823-
824-
$this->assertTrue($guard->attempt($user), 'Failed to login with credentials ' . json_encode($user));
825-
}
826-
827-
/**
828-
* Logout user.
829-
*/
830-
public function logout(): void
831-
{
832-
$this->app['auth']->logout();
833-
}
834-
835-
/**
836-
* Checks that a user is authenticated.
837-
* You can specify the guard that should be use as second parameter.
838-
*
839-
* @param string|null $guard
840-
*/
841-
public function seeAuthentication($guard = null): void
842-
{
843-
/** @var AuthContract $auth */
844-
$auth = $this->app['auth'];
845-
846-
$auth = $auth->guard($guard);
847-
848-
$this->assertTrue($auth->check(), 'There is no authenticated user');
849-
}
850-
851-
/**
852-
* Check that user is not authenticated.
853-
* You can specify the guard that should be use as second parameter.
854-
*
855-
* @param string|null $guard
856-
*/
857-
public function dontSeeAuthentication(?string $guard = null): void
858-
{
859-
/** @var AuthContract $auth */
860-
$auth = $this->app['auth'];
861-
862-
if (is_string($guard)) {
863-
$auth = $auth->guard($guard);
864-
}
865-
866-
$this->assertNotTrue($auth->check(), 'There is an user authenticated');
867-
}
868-
869794
/**
870795
* Return an instance of a class from the Laravel service container.
871796
* (https://laravel.com/docs/master/container)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Laravel;
6+
7+
use Illuminate\Contracts\Auth\Authenticatable;
8+
use Illuminate\Contracts\Auth\Factory as AuthContract;
9+
10+
trait InteractsWithAuthentication
11+
{
12+
/**
13+
* Set the currently logged in user for the application.
14+
* Takes either an object that implements the User interface or
15+
* an array of credentials.
16+
*
17+
* ``` php
18+
* <?php
19+
* // provide array of credentials
20+
* $I->amLoggedAs(['username' => 'jane@example.com', 'password' => 'password']);
21+
*
22+
* // provide User object
23+
* $I->amLoggedAs( new User );
24+
*
25+
* // can be verified with $I->seeAuthentication();
26+
* ```
27+
* @param Authenticatable|array $user
28+
* @param string|null $guardName The guard name
29+
*/
30+
public function amLoggedAs($user, ?string $guardName = null): void
31+
{
32+
/** @var AuthContract $auth */
33+
$auth = $this->app['auth'];
34+
35+
$guard = $auth->guard($guardName);
36+
37+
if ($user instanceof Authenticatable) {
38+
$guard->login($user);
39+
return;
40+
}
41+
42+
$this->assertTrue($guard->attempt($user), 'Failed to login with credentials ' . json_encode($user));
43+
}
44+
45+
/**
46+
* Check that user is not authenticated.
47+
* You can specify the guard that should be use as second parameter.
48+
*
49+
* @param string|null $guard
50+
*/
51+
public function dontSeeAuthentication(?string $guard = null): void
52+
{
53+
/** @var AuthContract $auth */
54+
$auth = $this->app['auth'];
55+
56+
if (is_string($guard)) {
57+
$auth = $auth->guard($guard);
58+
}
59+
60+
$this->assertNotTrue($auth->check(), 'There is an user authenticated');
61+
}
62+
63+
/**
64+
* Checks that a user is authenticated.
65+
* You can specify the guard that should be use as second parameter.
66+
*
67+
* @param string|null $guard
68+
*/
69+
public function seeAuthentication($guard = null): void
70+
{
71+
/** @var AuthContract $auth */
72+
$auth = $this->app['auth'];
73+
74+
$auth = $auth->guard($guard);
75+
76+
$this->assertTrue($auth->check(), 'There is no authenticated user');
77+
}
78+
79+
/**
80+
* Logout user.
81+
*/
82+
public function logout(): void
83+
{
84+
$this->app['auth']->logout();
85+
}
86+
}

0 commit comments

Comments
 (0)