Skip to content

Commit 892a77e

Browse files
committed
Split session methods
1 parent 8836efb commit 892a77e

File tree

2 files changed

+65
-53
lines changed

2 files changed

+65
-53
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Codeception\Module\Laravel\InteractsWithEvents;
1919
use Codeception\Module\Laravel\InteractsWithExceptionHandling;
2020
use Codeception\Module\Laravel\InteractsWithRouting;
21+
use Codeception\Module\Laravel\InteractsWithSession;
2122
use Codeception\Subscriber\ErrorHandler;
2223
use Codeception\TestInterface;
2324
use Codeception\Util\ReflectionHelper;
@@ -132,6 +133,7 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
132133
use InteractsWithEvents;
133134
use InteractsWithExceptionHandling;
134135
use InteractsWithRouting;
136+
use InteractsWithSession;
135137

136138
/**
137139
* @var Application
@@ -306,59 +308,6 @@ public function disableMiddleware()
306308
$this->client->disableMiddleware();
307309
}
308310

309-
/**
310-
* Assert that a session variable exists.
311-
*
312-
* ``` php
313-
* <?php
314-
* $I->seeInSession('key');
315-
* $I->seeInSession('key', 'value');
316-
* ```
317-
*
318-
* @param string|array $key
319-
* @param mixed|null $value
320-
*/
321-
public function seeInSession($key, $value = null): void
322-
{
323-
if (is_array($key)) {
324-
$this->seeSessionHasValues($key);
325-
return;
326-
}
327-
328-
/** @var Session $session */
329-
$session = $this->app['session'];
330-
331-
if (!$session->has($key)) {
332-
$this->fail("No session variable with key '$key'");
333-
}
334-
335-
if (! is_null($value)) {
336-
$this->assertEquals($value, $session->get($key));
337-
}
338-
}
339-
340-
/**
341-
* Assert that the session has a given list of values.
342-
*
343-
* ``` php
344-
* <?php
345-
* $I->seeSessionHasValues(['key1', 'key2']);
346-
* $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']);
347-
* ```
348-
*
349-
* @param array $bindings
350-
*/
351-
public function seeSessionHasValues(array $bindings): void
352-
{
353-
foreach ($bindings as $key => $value) {
354-
if (is_int($key)) {
355-
$this->seeInSession($value);
356-
} else {
357-
$this->seeInSession($key, $value);
358-
}
359-
}
360-
}
361-
362311
/**
363312
* Assert that form errors are bound to the View.
364313
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Laravel;
6+
7+
use Illuminate\Contracts\Session\Session;
8+
9+
trait InteractsWithSession
10+
{
11+
/**
12+
* Assert that a session variable exists.
13+
*
14+
* ``` php
15+
* <?php
16+
* $I->seeInSession('key');
17+
* $I->seeInSession('key', 'value');
18+
* ```
19+
*
20+
* @param string|array $key
21+
* @param mixed|null $value
22+
*/
23+
public function seeInSession($key, $value = null): void
24+
{
25+
if (is_array($key)) {
26+
$this->seeSessionHasValues($key);
27+
return;
28+
}
29+
30+
/** @var Session $session */
31+
$session = $this->app['session'];
32+
33+
if (!$session->has($key)) {
34+
$this->fail("No session variable with key '$key'");
35+
}
36+
37+
if (! is_null($value)) {
38+
$this->assertEquals($value, $session->get($key));
39+
}
40+
}
41+
42+
/**
43+
* Assert that the session has a given list of values.
44+
*
45+
* ``` php
46+
* <?php
47+
* $I->seeSessionHasValues(['key1', 'key2']);
48+
* $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']);
49+
* ```
50+
*
51+
* @param array $bindings
52+
*/
53+
public function seeSessionHasValues(array $bindings): void
54+
{
55+
foreach ($bindings as $key => $value) {
56+
if (is_int($key)) {
57+
$this->seeInSession($value);
58+
} else {
59+
$this->seeInSession($key, $value);
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)