|
<?php |
|
|
|
namespace Tests\Support; |
|
|
|
use App\Support\KeyedSession; |
|
use Illuminate\Foundation\Testing\TestCase; |
|
use Illuminate\Foundation\Testing\WithFaker; |
|
use Illuminate\Session\Store; |
|
|
|
class KeyedSessionTest extends TestCase |
|
{ |
|
use WithFaker; |
|
|
|
public function testShouldWorkWithSimpleGetSetOperations(): void |
|
{ |
|
$this->startSession(); |
|
|
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$this->assertFalse($session->has(), 'Has should be empty initially'); |
|
$session->put('foo'); |
|
$this->assertTrue($session->has(), 'Has should not be empty after putting'); |
|
|
|
$this->assertEquals('foo', $session->get(), 'Get should match the previous inserted value'); |
|
$this->assertEquals('foo', $this->app['session']->get($key), 'The session must have the value assigned to the key'); |
|
|
|
$this->assertEquals('foo', $session->pull('bar'), 'Pulling the value returns it'); |
|
$this->assertFalse($session->has(), 'Pulling the value removed it'); |
|
} |
|
|
|
public function testShouldWorkWithExistingData(): void |
|
{ |
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
|
|
$this->withSession([ |
|
$key => 'foobar', |
|
]); |
|
|
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$this->assertTrue($session->has(), 'Has should not be empty'); |
|
|
|
$this->assertEquals('foobar', $session->get(), 'Get should match the existing value'); |
|
$this->assertEquals('foobar', $this->app['session']->get($key), 'The session must have the value assigned to the key'); |
|
} |
|
|
|
/** |
|
* @dataProvider keyedSessionMethodsDataProvider |
|
*/ |
|
public function testVariousMethods(mixed $initialValue, callable $method, mixed $expected): void |
|
{ |
|
// Arrange |
|
$this->startSession(); |
|
|
|
$key = $this->faker->bothify('test_session_###???'); |
|
|
|
if (!is_null($initialValue)) { |
|
$this->withSession([ |
|
$key => $initialValue, |
|
]); |
|
} |
|
|
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act |
|
$value = $method($session); |
|
|
|
// Assert |
|
$this->assertEquals($expected, $value); |
|
} |
|
|
|
public function keyedSessionMethodsDataProvider(): array |
|
{ |
|
return [ |
|
'exists on existing key' => [ |
|
'initialValue' => '', |
|
'method' => fn (KeyedSession $session) => $session->exists(), |
|
'expected' => true, |
|
], |
|
'exists on new key' => [ |
|
'initialValue' => null, |
|
'method' => fn (KeyedSession $session) => $session->exists(), |
|
'expected' => false, |
|
], |
|
'has on empty key' => [ |
|
'initialValue' => '', |
|
'method' => fn (KeyedSession $session) => $session->has(), |
|
'expected' => true, |
|
], |
|
'has on non-existing key' => [ |
|
'initialValue' => null, |
|
'method' => fn (KeyedSession $session) => $session->has(), |
|
'expected' => false, |
|
], |
|
'has on set key' => [ |
|
'initialValue' => 'foo', |
|
'method' => fn (KeyedSession $session) => $session->has(), |
|
'expected' => true, |
|
], |
|
'missing on existing key' => [ |
|
'initialValue' => '', |
|
'method' => fn (KeyedSession $session) => $session->missing(), |
|
'expected' => false, |
|
], |
|
'missing on new key' => [ |
|
'initialValue' => null, |
|
'method' => fn (KeyedSession $session) => $session->missing(), |
|
'expected' => true, |
|
], |
|
'get' => [ |
|
'initialValue' => ['foo' => 'bar'], |
|
'method' => fn (KeyedSession $session) => $session->get(), |
|
'expected' => ['foo' => 'bar'], |
|
], |
|
'remember on new value' => [ |
|
'initialValue' => null, |
|
'method' => fn (KeyedSession $session) => $session->remember(fn () => 'foo'), |
|
'expected' => 'foo', |
|
], |
|
'remember on existing value' => [ |
|
'initialValue' => 'bar', |
|
'method' => fn (KeyedSession $session) => $session->remember(fn () => 'foo'), |
|
'expected' => 'bar', |
|
], |
|
]; |
|
} |
|
|
|
public function testShouldPullItems(): void |
|
{ |
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$session->put('foo'); |
|
$this->assertEquals('foo', $session->pull()); |
|
$this->assertEmpty($session->get()); |
|
|
|
$session->put(['foo', 'bar']); |
|
$this->assertEquals(['foo', 'bar'], $session->pull()); |
|
$this->assertEmpty($session->get()); |
|
} |
|
|
|
public function testShouldPushItems(): void |
|
{ |
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$session->put(['foo', 'bar']); |
|
$session->push('foobar'); |
|
$this->assertEquals(['foo', 'bar', 'foobar'], $session->get()); |
|
} |
|
|
|
public function testShouldRemoveItems(): void |
|
{ |
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$session->put('foo'); |
|
$this->assertEquals('foo', $session->get()); |
|
$this->assertEquals('foo', $session->remove()); |
|
$this->assertNotEquals('foo', $session->get()); |
|
$this->assertFalse($session->has()); |
|
} |
|
|
|
public function testShouldForgetItems(): void |
|
{ |
|
// Arrange |
|
$key = $this->faker->bothify('test_session_###???'); |
|
$session = new KeyedSession($this->app->make(Store::class), $key); |
|
|
|
// Act + Assert |
|
$session->put('foo'); |
|
$this->assertEquals('foo', $session->get()); |
|
$this->assertNull($session->forget()); |
|
$this->assertNotEquals('foo', $session->get()); |
|
$this->assertFalse($session->has()); |
|
} |
|
} |