|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Session; |
| 4 | + |
| 5 | +use Illuminate\Filesystem\Filesystem; |
| 6 | +use Illuminate\Session\FileSessionHandler; |
| 7 | +use Illuminate\Support\Carbon; |
| 8 | +use Mockery; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +use function Illuminate\Filesystem\join_paths; |
| 12 | + |
| 13 | +class FileSessionHandlerTest extends TestCase |
| 14 | +{ |
| 15 | + protected $files; |
| 16 | + |
| 17 | + protected $sessionHandler; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + // Create a mock for the Filesystem class |
| 22 | + $this->files = Mockery::mock(Filesystem::class); |
| 23 | + |
| 24 | + // Initialize the FileSessionHandler with the mocked Filesystem |
| 25 | + $this->sessionHandler = new FileSessionHandler($this->files, '/path/to/sessions', 30); |
| 26 | + } |
| 27 | + |
| 28 | + protected function tearDown(): void |
| 29 | + { |
| 30 | + Mockery::close(); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_open() |
| 34 | + { |
| 35 | + $this->assertTrue($this->sessionHandler->open('/path/to/sessions', 'session_name')); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_close() |
| 39 | + { |
| 40 | + $this->assertTrue($this->sessionHandler->close()); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_read_returns_data_when_file_exists_and_is_valid() |
| 44 | + { |
| 45 | + $sessionId = 'session_id'; |
| 46 | + $path = '/path/to/sessions/'.$sessionId; |
| 47 | + Carbon::setTestNow(Carbon::parse('2025-02-02 01:30:00')); |
| 48 | + // Set up expectations |
| 49 | + $this->files->shouldReceive('isFile')->with($path)->andReturn(true); |
| 50 | + |
| 51 | + $minutesAgo30 = Carbon::parse('2025-02-02 01:00:00')->getTimestamp(); |
| 52 | + $this->files->shouldReceive('lastModified')->with($path)->andReturn($minutesAgo30); |
| 53 | + $this->files->shouldReceive('sharedGet')->with($path)->once()->andReturn('session_data'); |
| 54 | + |
| 55 | + $result = $this->sessionHandler->read($sessionId); |
| 56 | + |
| 57 | + $this->assertEquals('session_data', $result); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_read_returns_data_when_file_exists_but_expired() |
| 61 | + { |
| 62 | + $sessionId = 'session_id'; |
| 63 | + $path = '/path/to/sessions/'.$sessionId; |
| 64 | + Carbon::setTestNow(Carbon::parse('2025-02-02 01:30:01')); |
| 65 | + // Set up expectations |
| 66 | + $this->files->shouldReceive('isFile')->with($path)->andReturn(true); |
| 67 | + |
| 68 | + $minutesAgo30 = Carbon::parse('2025-02-02 01:00:00')->getTimestamp(); |
| 69 | + $this->files->shouldReceive('lastModified')->with($path)->andReturn($minutesAgo30); |
| 70 | + $this->files->shouldReceive('sharedGet')->never(); |
| 71 | + |
| 72 | + $result = $this->sessionHandler->read($sessionId); |
| 73 | + |
| 74 | + $this->assertEquals('', $result); |
| 75 | + } |
| 76 | + |
| 77 | + public function test_read_returns_empty_string_when_file_does_not_exist() |
| 78 | + { |
| 79 | + $sessionId = 'non_existing_session_id'; |
| 80 | + $path = '/path/to/sessions/'.$sessionId; |
| 81 | + |
| 82 | + // Set up expectations |
| 83 | + $this->files->shouldReceive('isFile')->with($path)->andReturn(false); |
| 84 | + |
| 85 | + $result = $this->sessionHandler->read($sessionId); |
| 86 | + |
| 87 | + $this->assertEquals('', $result); |
| 88 | + } |
| 89 | + |
| 90 | + public function test_write_stores_data() |
| 91 | + { |
| 92 | + $sessionId = 'session_id'; |
| 93 | + $data = 'session_data'; |
| 94 | + |
| 95 | + // Set up expectations |
| 96 | + $this->files->shouldReceive('put')->with('/path/to/sessions/'.$sessionId, $data, true)->once()->andReturn(null); |
| 97 | + |
| 98 | + $result = $this->sessionHandler->write($sessionId, $data); |
| 99 | + |
| 100 | + $this->assertTrue($result); |
| 101 | + } |
| 102 | + |
| 103 | + public function test_destroy_deletes_session_file() |
| 104 | + { |
| 105 | + $sessionId = 'session_id'; |
| 106 | + |
| 107 | + // Set up expectations |
| 108 | + $this->files->shouldReceive('delete')->with('/path/to/sessions/'.$sessionId)->once()->andReturn(null); |
| 109 | + |
| 110 | + $result = $this->sessionHandler->destroy($sessionId); |
| 111 | + |
| 112 | + $this->assertTrue($result); |
| 113 | + } |
| 114 | + |
| 115 | + public function test_gc_deletes_old_session_files() |
| 116 | + { |
| 117 | + $session = new FileSessionHandler($this->files, join_paths(__DIR__, 'tmp'), 30); |
| 118 | + // Set up expectations for Filesystem |
| 119 | + $this->files->shouldReceive('delete')->with(join_paths(__DIR__, 'tmp', 'a2'))->once()->andReturn(false); |
| 120 | + $this->files->shouldReceive('delete')->with(join_paths(__DIR__, 'tmp', 'a3'))->once()->andReturn(true); |
| 121 | + |
| 122 | + mkdir(__DIR__.'/tmp'); |
| 123 | + touch(__DIR__.'/tmp/a1', time() - 3); // last modified: 3 sec ago |
| 124 | + touch(__DIR__.'/tmp/a2', time() - 5); // last modified: 5 sec ago |
| 125 | + touch(__DIR__.'/tmp/a3', time() - 7); // last modified: 7 sec ago |
| 126 | + |
| 127 | + // act: |
| 128 | + $count = $session->gc(5); |
| 129 | + |
| 130 | + $this->assertEquals(2, $count); |
| 131 | + |
| 132 | + unlink(__DIR__.'/tmp/a1'); |
| 133 | + unlink(__DIR__.'/tmp/a2'); |
| 134 | + unlink(__DIR__.'/tmp/a3'); |
| 135 | + |
| 136 | + rmdir(__DIR__.'/tmp'); |
| 137 | + } |
| 138 | +} |
0 commit comments