Skip to content

Commit

Permalink
Merge pull request #615 from hydephp/add-fake-session-handler
Browse files Browse the repository at this point in the history
Add a simple session handler
  • Loading branch information
caendesilva authored Oct 28, 2022
2 parents 700547f + 87a9b42 commit b54a0e6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/framework/src/Framework/Features/Session/Session.php
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);
}
}
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()
{
//
}
}
2 changes: 2 additions & 0 deletions packages/framework/src/Framework/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Concerns\RegistersFileLocations;
use Hyde\Framework\Features\DataCollections\DataCollectionServiceProvider;
use Hyde\Framework\Features\Session\SessionServiceProvider;
use Hyde\Framework\Services\AssetService;
use Hyde\Framework\Services\YamlConfigurationService;
use Hyde\Framework\Views\Components\LinkComponent;
Expand Down Expand Up @@ -109,6 +110,7 @@ protected function initializeConfiguration()
*/
protected function registerModuleServiceProviders(): void
{
$this->app->register(SessionServiceProvider::class);
$this->app->register(HydeConsoleServiceProvider::class);
$this->app->register(DataCollectionServiceProvider::class);
}
Expand Down
45 changes: 45 additions & 0 deletions packages/framework/tests/Framework/Feature/SessionTest.php
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());
}
}

0 comments on commit b54a0e6

Please sign in to comment.