| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Test\Michel\Session;
- use Michel\Session\Michel\Package\MichelSessionPackage;
- use Michel\Session\Storage\NativeSessionStorage;
- use Michel\Session\Storage\SessionStorageInterface;
- use Michel\UniTester\TestCase;
- use Psr\Container\ContainerInterface;
- use RuntimeException;
- final class MichelSessionPackageTest extends TestCase
- {
- protected function setUp(): void
- {
- $_SESSION = [];
- }
- protected function tearDown(): void
- {
- $_SESSION = [];
- session_destroy();
- foreach (glob( __DIR__ . '/var/session/*') as $file) {
- unlink($file);
- }
- }
- protected function execute(): void
- {
- $container = new class implements ContainerInterface {
- private array $definitions = [];
- private array $values = [];
- public function __construct()
- {
- $package = new MichelSessionPackage();
- $definitions = $package->getDefinitions();
- $parameters = $package->getParameters();
- $this->definitions = $definitions + $parameters + [
- ContainerInterface::class => $this,
- 'michel.project_dir' => __DIR__,
- 'session.save_path' => 'var/session',
- ];
- }
- public function get(string $id)
- {
- if (!$this->has($id)) {
- throw new RuntimeException('Unknown definition: ' . $id);
- }
- if (isset($this->values[$id])) {
- return $this->values[$id];
- }
- $value = $this->definitions[$id];
- if (is_callable($value)) {
- $value = $value($this);
- }
- $this->values[$id] = $value;
- return $value;
- }
- public function has(string $id): bool
- {
- return isset($this->definitions[$id]);
- }
- };
- $sessionStorage = $container->get(SessionStorageInterface::class);
- $this->assertInstanceOf(NativeSessionStorage::class, $sessionStorage);
- $sessionStorage->put('username', 'myName');
- $this->assertTrue($sessionStorage->has('username'));
- $this->assertTrue(!empty(glob( __DIR__ . '/var/session/*')));
- }
- }
|