Skip to content

Commit 8a0ef3b

Browse files
committed
Updated assertion traits logic
1 parent 3263370 commit 8a0ef3b

10 files changed

+192
-283
lines changed

src/Codeception/Module/Laravel/InteractsWithAuthentication.php

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,70 @@
55
namespace Codeception\Module\Laravel;
66

77
use Illuminate\Contracts\Auth\Authenticatable;
8-
use Illuminate\Contracts\Auth\Factory as AuthContract;
98

109
trait InteractsWithAuthentication
1110
{
1211
/**
1312
* 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.
13+
* Unlike 'amActingAs', this method does update the session, fire the login events
14+
* and remember the user as it assigns the corresponding Cookie.
1615
*
17-
* ``` php
16+
* ```php
1817
* <?php
1918
* // provide array of credentials
2019
* $I->amLoggedAs(['username' => 'jane@example.com', 'password' => 'password']);
2120
*
22-
* // provide User object
21+
* // provide User object that implements the User interface
2322
* $I->amLoggedAs( new User );
2423
*
2524
* // can be verified with $I->seeAuthentication();
2625
* ```
2726
* @param Authenticatable|array $user
28-
* @param string|null $guardName The guard name
27+
* @param string|null $guardName
2928
*/
30-
public function amLoggedAs($user, ?string $guardName = null): void
29+
public function amLoggedAs($user, string $guardName = null): void
3130
{
32-
/** @var AuthContract $auth */
33-
$auth = $this->app['auth'];
34-
35-
$guard = $auth->guard($guardName);
36-
3731
if ($user instanceof Authenticatable) {
38-
$guard->login($user);
32+
$this->getAuth()->login($user);
3933
return;
4034
}
4135

42-
$this->assertTrue($guard->attempt($user), 'Failed to login with credentials ' . json_encode($user));
36+
$guard = $this->getAuth()->guard($guardName);
37+
$this->assertTrue(
38+
$guard->attempt($user)
39+
, 'Failed to login with credentials ' . json_encode($user)
40+
);
4341
}
4442

4543
/**
4644
* 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
5045
*/
51-
public function dontSeeAuthentication(?string $guard = null): void
46+
public function dontSeeAuthentication(string $guardName = null): void
5247
{
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');
48+
$this->assertFalse($this->isAuthenticated($guardName), 'The user is authenticated');
6149
}
6250

6351
/**
6452
* 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
6853
*/
69-
public function seeAuthentication($guard = null): void
54+
public function seeAuthentication(string $guardName = null): void
7055
{
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');
56+
$this->assertTrue($this->isAuthenticated($guardName), 'The user is not authenticated');
7757
}
7858

7959
/**
8060
* Logout user.
8161
*/
8262
public function logout(): void
8363
{
84-
$this->app['auth']->logout();
64+
$this->getAuth()->logout();
65+
}
66+
67+
/**
68+
* Return true if the user is authenticated, false otherwise.
69+
*/
70+
protected function isAuthenticated(?string $guardName): bool
71+
{
72+
return $this->getAuth()->guard($guardName)->check();
8573
}
8674
}

src/Codeception/Module/Laravel/InteractsWithConsole.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,25 @@
44

55
namespace Codeception\Module\Laravel;
66

7-
use Illuminate\Contracts\Console\Kernel;
87
use Symfony\Component\Console\Output\OutputInterface;
98

109
trait InteractsWithConsole
1110
{
1211
/**
1312
* Call an Artisan command.
1413
*
15-
* ``` php
14+
* ```php
1615
* <?php
1716
* $I->callArtisan('command:name');
1817
* $I->callArtisan('command:name', ['parameter' => 'value']);
1918
* ```
2019
* Use 3rd parameter to pass in custom `OutputInterface`
2120
*
22-
* @param string $command
23-
* @param array $parameters
24-
* @param OutputInterface|null $output
2521
* @return string|void
2622
*/
27-
public function callArtisan(string $command, $parameters = [], OutputInterface $output = null)
23+
public function callArtisan(string $command, array $parameters = [], OutputInterface $output = null)
2824
{
29-
$console = $this->app->make(Kernel::class);
25+
$console = $this->getConsoleKernel();
3026
if (!$output) {
3127
$console->call($command, $parameters);
3228
$output = trim($console->output());

src/Codeception/Module/Laravel/InteractsWithContainer.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Codeception\Module\Laravel;
66

7+
use Illuminate\Contracts\Foundation\Application;
8+
79
trait InteractsWithContainer
810
{
911
/**
1012
* Clear the registered application handlers.
1113
*
12-
* ``` php
14+
* ```php
1315
* <?php
1416
* $I->clearApplicationHandlers();
1517
* ```
@@ -21,19 +23,17 @@ public function clearApplicationHandlers(): void
2123

2224
/**
2325
* Provides access the Laravel application object.
24-
*
25-
* @return \Illuminate\Contracts\Foundation\Application
2626
*/
27-
public function getApplication()
27+
public function getApplication(): Application
2828
{
2929
return $this->app;
3030
}
3131

3232
/**
3333
* Return an instance of a class from the Laravel service container.
34-
* (https://laravel.com/docs/master/container)
34+
* (https://laravel.com/docs/7.x/container)
3535
*
36-
* ``` php
36+
* ```php
3737
* <?php
3838
* // In Laravel
3939
* App::bind('foo', function($app) {
@@ -46,7 +46,6 @@ public function getApplication()
4646
* // Will return an instance of FooBar, also works for singletons.
4747
* ```
4848
*
49-
* @param string $class
5049
* @return mixed
5150
*/
5251
public function grabService(string $class)
@@ -58,14 +57,12 @@ public function grabService(string $class)
5857
* Register a handler than can be used to modify the Laravel application object after it is initialized.
5958
* The Laravel application object will be passed as an argument to the handler.
6059
*
61-
* ``` php
60+
* ```php
6261
* <?php
6362
* $I->haveApplicationHandler(function($app) {
6463
* $app->make('config')->set(['test_value' => '10']);
6564
* });
6665
* ```
67-
*
68-
* @param callable $handler
6966
*/
7067
public function haveApplicationHandler(callable $handler): void
7168
{
@@ -74,9 +71,9 @@ public function haveApplicationHandler(callable $handler): void
7471

7572
/**
7673
* Add a binding to the Laravel service container.
77-
* (https://laravel.com/docs/master/container)
74+
* (https://laravel.com/docs/7.x/container)
7875
*
79-
* ``` php
76+
* ```php
8077
* <?php
8178
* $I->haveBinding('My\Interface', 'My\Implementation');
8279
* ```
@@ -92,9 +89,9 @@ public function haveBinding(string $abstract, $concrete = null, bool $shared = f
9289

9390
/**
9491
* Add a contextual binding to the Laravel service container.
95-
* (https://laravel.com/docs/master/container)
92+
* (https://laravel.com/docs/7.x/container)
9693
*
97-
* ``` php
94+
* ```php
9895
* <?php
9996
* $I->haveContextualBinding('My\Class', '$variable', 'value');
10097
*
@@ -115,26 +112,23 @@ public function haveContextualBinding(string $concrete, string $abstract, $imple
115112

116113
/**
117114
* Add an instance binding to the Laravel service container.
118-
* (https://laravel.com/docs/master/container)
115+
* (https://laravel.com/docs/7.x/container)
119116
*
120-
* ``` php
117+
* ```php
121118
* <?php
122119
* $I->haveInstance('App\MyClass', new App\MyClass());
123120
* ```
124-
*
125-
* @param string $abstract
126-
* @param mixed $instance
127121
*/
128-
public function haveInstance(string $abstract, $instance): void
122+
public function haveInstance(string $abstract, object $instance): void
129123
{
130124
$this->client->haveInstance($abstract, $instance);
131125
}
132126

133127
/**
134128
* Add a singleton binding to the Laravel service container.
135-
* (https://laravel.com/docs/master/container)
129+
* (https://laravel.com/docs/7.x/container)
136130
*
137-
* ``` php
131+
* ```php
138132
* <?php
139133
* $I->haveSingleton('App\MyInterface', 'App\MySingleton');
140134
* ```
@@ -147,10 +141,7 @@ public function haveSingleton(string $abstract, $concrete): void
147141
$this->client->haveBinding($abstract, $concrete, true);
148142
}
149143

150-
/**
151-
* @param \Illuminate\Contracts\Foundation\Application $app
152-
*/
153-
public function setApplication($app): void
144+
public function setApplication(Application $app): void
154145
{
155146
$this->app = $app;
156147
}

0 commit comments

Comments
 (0)