MarshallingSessionHandlerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  14. use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
  15. use Symfony\Component\HttpFoundation\Session\Storage\Handler\MarshallingSessionHandler;
  16. /**
  17. * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
  18. */
  19. class MarshallingSessionHandlerTest extends TestCase
  20. {
  21. /**
  22. * @var MockObject|\SessionHandlerInterface
  23. */
  24. protected $handler;
  25. /**
  26. * @var MockObject|MarshallerInterface
  27. */
  28. protected $marshaller;
  29. protected function setUp(): void
  30. {
  31. $this->marshaller = $this->createMock(MarshallerInterface::class);
  32. $this->handler = $this->createMock(AbstractSessionHandler::class);
  33. }
  34. public function testOpen()
  35. {
  36. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  37. $this->handler->expects($this->once())->method('open')
  38. ->with('path', 'name')->willReturn(true);
  39. $marshallingSessionHandler->open('path', 'name');
  40. }
  41. public function testClose()
  42. {
  43. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  44. $this->handler->expects($this->once())->method('close')->willReturn(true);
  45. $this->assertTrue($marshallingSessionHandler->close());
  46. }
  47. public function testDestroy()
  48. {
  49. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  50. $this->handler->expects($this->once())->method('destroy')
  51. ->with('session_id')->willReturn(true);
  52. $marshallingSessionHandler->destroy('session_id');
  53. }
  54. public function testGc()
  55. {
  56. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  57. $this->handler->expects($this->once())->method('gc')
  58. ->with(4711)->willReturn(1);
  59. $marshallingSessionHandler->gc(4711);
  60. }
  61. public function testRead()
  62. {
  63. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  64. $this->handler->expects($this->once())->method('read')->with('session_id')
  65. ->willReturn('data');
  66. $this->marshaller->expects($this->once())->method('unmarshall')->with('data')
  67. ->willReturn('unmarshalled_data')
  68. ;
  69. $result = $marshallingSessionHandler->read('session_id');
  70. $this->assertEquals('unmarshalled_data', $result);
  71. }
  72. public function testWrite()
  73. {
  74. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  75. $this->marshaller->expects($this->once())->method('marshall')
  76. ->with(['data' => 'data'], [])
  77. ->willReturn(['data' => 'marshalled_data']);
  78. $this->handler->expects($this->once())->method('write')
  79. ->with('session_id', 'marshalled_data')
  80. ;
  81. $marshallingSessionHandler->write('session_id', 'data');
  82. }
  83. public function testValidateId()
  84. {
  85. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  86. $this->handler->expects($this->once())->method('validateId')
  87. ->with('session_id')->willReturn(true);
  88. $marshallingSessionHandler->validateId('session_id');
  89. }
  90. public function testUpdateTimestamp()
  91. {
  92. $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
  93. $this->handler->expects($this->once())->method('updateTimestamp')
  94. ->with('session_id', 'data')->willReturn(true);
  95. $marshallingSessionHandler->updateTimestamp('session_id', 'data');
  96. }
  97. }