-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from hydephp/add-fake-session-handler
Add a simple session handler
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/framework/src/Framework/Features/Session/Session.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Session; | ||
|
||
/** | ||
* Adds a simple session handler, arguably most useful to defer messages, for example, | ||
* to asynchronously add warnings to be used at a later point in the request lifecycle. | ||
* | ||
* It's bound into the service container as a singleton and is not persisted. | ||
* | ||
* @example app(Session::class)->addWarning('warning'); | ||
*/ | ||
class Session | ||
{ | ||
protected array $warnings = []; | ||
|
||
public function addWarning(string $warning): void | ||
{ | ||
$this->warnings[] = $warning; | ||
} | ||
|
||
public function getWarnings(): array | ||
{ | ||
return $this->warnings; | ||
} | ||
|
||
public function hasWarnings(): bool | ||
{ | ||
return ! empty($this->warnings); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/framework/src/Framework/Features/Session/SessionServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Session; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class SessionServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->app->singleton(Session::class, Session::class); | ||
} | ||
|
||
public function boot() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/framework/tests/Framework/Feature/SessionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Framework\Feature; | ||
|
||
use function app; | ||
use Hyde\Framework\Features\Session\Session; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Features\Session\Session | ||
* @covers \Hyde\Framework\Features\Session\SessionServiceProvider | ||
*/ | ||
class SessionTest extends TestCase | ||
{ | ||
public function test_session_is_bound_to_service_container_as_singleton() | ||
{ | ||
$this->assertInstanceOf(Session::class, $this->app->make(Session::class)); | ||
$this->assertSame(app(Session::class), $this->app->make(Session::class)); | ||
} | ||
|
||
public function test_session_can_add_warning() | ||
{ | ||
app(Session::class)->addWarning('warning'); | ||
|
||
$this->assertSame(['warning'], app(Session::class)->getWarnings()); | ||
} | ||
|
||
public function test_session_is_not_persisted() | ||
{ | ||
$this->assertSame([], app(Session::class)->getWarnings()); | ||
} | ||
|
||
public function test_session_can_check_if_warnings_are_present() | ||
{ | ||
$session = app(Session::class); | ||
|
||
$this->assertFalse($session->hasWarnings()); | ||
|
||
$session->addWarning('warning'); | ||
|
||
$this->assertTrue($session->hasWarnings()); | ||
} | ||
} |