EncryptedSessionStoreTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Illuminate\Tests\Session;
  3. use Illuminate\Contracts\Encryption\Encrypter;
  4. use Illuminate\Session\EncryptedStore;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. use ReflectionClass;
  8. use SessionHandlerInterface;
  9. class EncryptedSessionStoreTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testSessionIsProperlyEncrypted()
  16. {
  17. $session = $this->getSession();
  18. $session->getEncrypter()->shouldReceive('decrypt')->once()->with(serialize([]))->andReturn(serialize([]));
  19. $session->getHandler()->shouldReceive('read')->once()->andReturn(serialize([]));
  20. $session->start();
  21. $session->put('foo', 'bar');
  22. $session->flash('baz', 'boom');
  23. $session->now('qux', 'norf');
  24. $serialized = serialize([
  25. '_token' => $session->token(),
  26. 'foo' => 'bar',
  27. 'baz' => 'boom',
  28. '_flash' => [
  29. 'new' => [],
  30. 'old' => ['baz'],
  31. ],
  32. ]);
  33. $session->getEncrypter()->shouldReceive('encrypt')->once()->with($serialized)->andReturn($serialized);
  34. $session->getHandler()->shouldReceive('write')->once()->with(
  35. $this->getSessionId(),
  36. $serialized
  37. );
  38. $session->save();
  39. $this->assertFalse($session->isStarted());
  40. }
  41. public function getSession()
  42. {
  43. $reflection = new ReflectionClass(EncryptedStore::class);
  44. return $reflection->newInstanceArgs($this->getMocks());
  45. }
  46. public function getMocks()
  47. {
  48. return [
  49. $this->getSessionName(),
  50. m::mock(SessionHandlerInterface::class),
  51. m::mock(Encrypter::class),
  52. $this->getSessionId(),
  53. ];
  54. }
  55. public function getSessionId()
  56. {
  57. return 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
  58. }
  59. public function getSessionName()
  60. {
  61. return 'name';
  62. }
  63. }