MichelSessionPackageTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Test\Michel\Session;
  3. use Michel\Session\Michel\Package\MichelSessionPackage;
  4. use Michel\Session\Storage\NativeSessionStorage;
  5. use Michel\Session\Storage\SessionStorageInterface;
  6. use Michel\UniTester\TestCase;
  7. use Psr\Container\ContainerInterface;
  8. use RuntimeException;
  9. final class MichelSessionPackageTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. $_SESSION = [];
  14. }
  15. protected function tearDown(): void
  16. {
  17. $_SESSION = [];
  18. session_destroy();
  19. foreach (glob( __DIR__ . '/var/session/*') as $file) {
  20. unlink($file);
  21. }
  22. }
  23. protected function execute(): void
  24. {
  25. $container = new class implements ContainerInterface {
  26. private array $definitions = [];
  27. private array $values = [];
  28. public function __construct()
  29. {
  30. $package = new MichelSessionPackage();
  31. $definitions = $package->getDefinitions();
  32. $parameters = $package->getParameters();
  33. $this->definitions = $definitions + $parameters + [
  34. ContainerInterface::class => $this,
  35. 'michel.project_dir' => __DIR__,
  36. 'session.save_path' => 'var/session',
  37. ];
  38. }
  39. public function get(string $id)
  40. {
  41. if (!$this->has($id)) {
  42. throw new RuntimeException('Unknown definition: ' . $id);
  43. }
  44. if (isset($this->values[$id])) {
  45. return $this->values[$id];
  46. }
  47. $value = $this->definitions[$id];
  48. if (is_callable($value)) {
  49. $value = $value($this);
  50. }
  51. $this->values[$id] = $value;
  52. return $value;
  53. }
  54. public function has(string $id): bool
  55. {
  56. return isset($this->definitions[$id]);
  57. }
  58. };
  59. $sessionStorage = $container->get(SessionStorageInterface::class);
  60. $this->assertInstanceOf(NativeSessionStorage::class, $sessionStorage);
  61. $sessionStorage->put('username', 'myName');
  62. $this->assertTrue($sessionStorage->has('username'));
  63. $this->assertTrue(!empty(glob( __DIR__ . '/var/session/*')));
  64. }
  65. }