SessionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace think\tests;
  3. use Mockery as m;
  4. use Mockery\MockInterface;
  5. use org\bovigo\vfs\vfsStream;
  6. use PHPUnit\Framework\TestCase;
  7. use think\App;
  8. use think\cache\Driver;
  9. use think\Config;
  10. use think\Container;
  11. use think\contract\SessionHandlerInterface;
  12. use think\helper\Str;
  13. use think\Session;
  14. use think\session\driver\Cache;
  15. use think\session\driver\File;
  16. class SessionTest extends TestCase
  17. {
  18. /** @var App|MockInterface */
  19. protected $app;
  20. /** @var Session|MockInterface */
  21. protected $session;
  22. /** @var Config|MockInterface */
  23. protected $config;
  24. protected $handler;
  25. protected function tearDown(): void
  26. {
  27. m::close();
  28. }
  29. protected function setUp()
  30. {
  31. $this->app = m::mock(App::class)->makePartial();
  32. Container::setInstance($this->app);
  33. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  34. $this->config = m::mock(Config::class)->makePartial();
  35. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  36. $handlerClass = "\\think\\session\\driver\\Test" . Str::random(10);
  37. $this->config->shouldReceive("get")->with("session.type", "file")->andReturn($handlerClass);
  38. $this->session = new Session($this->app);
  39. $this->handler = m::mock('overload:' . $handlerClass, SessionHandlerInterface::class);
  40. }
  41. public function testLoadData()
  42. {
  43. $data = [
  44. "bar" => 'foo',
  45. ];
  46. $id = md5(uniqid());
  47. $this->handler->shouldReceive("read")->once()->with($id)->andReturn(serialize($data));
  48. $this->session->setId($id);
  49. $this->session->init();
  50. $this->assertEquals('foo', $this->session->get('bar'));
  51. $this->assertTrue($this->session->has('bar'));
  52. $this->assertFalse($this->session->has('foo'));
  53. $this->session->set('foo', 'bar');
  54. $this->assertTrue($this->session->has('foo'));
  55. $this->assertEquals('bar', $this->session->pull('foo'));
  56. $this->assertFalse($this->session->has('foo'));
  57. }
  58. public function testSave()
  59. {
  60. $id = md5(uniqid());
  61. $this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
  62. $this->handler->shouldReceive('write')->once()->with($id, serialize([
  63. "bar" => 'foo',
  64. ]))->andReturnTrue();
  65. $this->session->setId($id);
  66. $this->session->init();
  67. $this->session->set('bar', 'foo');
  68. $this->session->save();
  69. }
  70. public function testFlash()
  71. {
  72. $this->session->flash('foo', 'bar');
  73. $this->session->flash('bar', 0);
  74. $this->session->flash('baz', true);
  75. $this->assertTrue($this->session->has('foo'));
  76. $this->assertEquals('bar', $this->session->get('foo'));
  77. $this->assertEquals(0, $this->session->get('bar'));
  78. $this->assertTrue($this->session->get('baz'));
  79. $this->session->clearFlashData();
  80. $this->assertTrue($this->session->has('foo'));
  81. $this->assertEquals('bar', $this->session->get('foo'));
  82. $this->assertEquals(0, $this->session->get('bar'));
  83. $this->session->clearFlashData();
  84. $this->assertFalse($this->session->has('foo'));
  85. $this->assertNull($this->session->get('foo'));
  86. $this->session->flash('foo', 'bar');
  87. $this->assertTrue($this->session->has('foo'));
  88. $this->session->clearFlashData();
  89. $this->session->reflash();
  90. $this->session->clearFlashData();
  91. $this->assertTrue($this->session->has('foo'));
  92. }
  93. public function testClear()
  94. {
  95. $this->session->set('bar', 'foo');
  96. $this->assertEquals('foo', $this->session->get('bar'));
  97. $this->session->clear();
  98. $this->assertFalse($this->session->has('foo'));
  99. }
  100. public function testSetName()
  101. {
  102. $this->session->setName('foo');
  103. $this->assertEquals('foo', $this->session->getName());
  104. }
  105. public function testDestroy()
  106. {
  107. $id = md5(uniqid());
  108. $this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
  109. $this->handler->shouldReceive('delete')->once()->with($id)->andReturnTrue();
  110. $this->session->setId($id);
  111. $this->session->init();
  112. $this->session->set('bar', 'foo');
  113. $this->session->destroy();
  114. $this->assertFalse($this->session->has('bar'));
  115. $this->assertNotEquals($id, $this->session->getId());
  116. }
  117. public function testFileHandler()
  118. {
  119. $root = vfsStream::setup();
  120. vfsStream::newFile('bar')
  121. ->at($root)
  122. ->lastModified(time());
  123. vfsStream::newFile('bar')
  124. ->at(vfsStream::newDirectory("foo")->at($root))
  125. ->lastModified(100);
  126. $this->assertTrue($root->hasChild("bar"));
  127. $this->assertTrue($root->hasChild("foo/bar"));
  128. $handler = new TestFileHandle($this->app, [
  129. 'path' => $root->url(),
  130. 'gc_probability' => 1,
  131. 'gc_divisor' => 1,
  132. ]);
  133. $this->assertTrue($root->hasChild("bar"));
  134. $this->assertFalse($root->hasChild("foo/bar"));
  135. $id = md5(uniqid());
  136. $handler->write($id, "bar");
  137. $this->assertTrue($root->hasChild("sess_{$id}"));
  138. $this->assertEquals("bar", $handler->read($id));
  139. $handler->delete($id);
  140. $this->assertFalse($root->hasChild("sess_{$id}"));
  141. }
  142. public function testCacheHandler()
  143. {
  144. $id = md5(uniqid());
  145. $cache = m::mock(\think\Cache::class);
  146. $store = m::mock(Driver::class);
  147. $cache->shouldReceive('store')->once()->with('redis')->andReturn($store);
  148. $handler = new Cache($cache, ['store' => 'redis']);
  149. $store->shouldReceive("set")->with($id, "bar", 1440)->once()->andReturnTrue();
  150. $handler->write($id, "bar");
  151. $store->shouldReceive("get")->with($id)->once()->andReturn("bar");
  152. $this->assertEquals("bar", $handler->read($id));
  153. $store->shouldReceive("delete")->with($id)->once()->andReturnTrue();
  154. $handler->delete($id);
  155. }
  156. }
  157. class TestFileHandle extends File
  158. {
  159. protected function writeFile($path, $content): bool
  160. {
  161. return (bool) file_put_contents($path, $content);
  162. }
  163. }