|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Session\Handlers\Database; |
| 13 | + |
| 14 | +use CodeIgniter\Session\Handlers\RedisHandler; |
| 15 | +use CodeIgniter\Test\CIUnitTestCase; |
| 16 | +use Config\App as AppConfig; |
| 17 | +use Redis; |
| 18 | + |
| 19 | +/** |
| 20 | + * @requires extension redis |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +final class RedisHandlerTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + private string $sessionName = 'ci_session'; |
| 27 | + private string $sessionSavePath = 'tcp://127.0.0.1:6379'; |
| 28 | + private string $userIpAddress = '127.0.0.1'; |
| 29 | + |
| 30 | + private function getInstance($options = []) |
| 31 | + { |
| 32 | + $defaults = [ |
| 33 | + 'sessionDriver' => RedisHandler::class, |
| 34 | + 'sessionCookieName' => $this->sessionName, |
| 35 | + 'sessionExpiration' => 7200, |
| 36 | + 'sessionSavePath' => $this->sessionSavePath, |
| 37 | + 'sessionMatchIP' => false, |
| 38 | + 'sessionTimeToUpdate' => 300, |
| 39 | + 'sessionRegenerateDestroy' => false, |
| 40 | + 'cookieDomain' => '', |
| 41 | + 'cookiePrefix' => '', |
| 42 | + 'cookiePath' => '/', |
| 43 | + 'cookieSecure' => false, |
| 44 | + 'cookieSameSite' => 'Lax', |
| 45 | + ]; |
| 46 | + |
| 47 | + $config = array_merge($defaults, $options); |
| 48 | + $appConfig = new AppConfig(); |
| 49 | + |
| 50 | + foreach ($config as $key => $c) { |
| 51 | + $appConfig->{$key} = $c; |
| 52 | + } |
| 53 | + |
| 54 | + return new RedisHandler($appConfig, $this->userIpAddress); |
| 55 | + } |
| 56 | + |
| 57 | + public function testSavePathTimeoutFloat() |
| 58 | + { |
| 59 | + $handler = $this->getInstance( |
| 60 | + ['sessionSavePath' => 'tcp://127.0.0.1:6379?timeout=2.5'] |
| 61 | + ); |
| 62 | + |
| 63 | + $savePath = $this->getPrivateProperty($handler, 'savePath'); |
| 64 | + |
| 65 | + $this->assertSame(2.5, $savePath['timeout']); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSavePathTimeoutInt() |
| 69 | + { |
| 70 | + $handler = $this->getInstance( |
| 71 | + ['sessionSavePath' => 'tcp://127.0.0.1:6379?timeout=10'] |
| 72 | + ); |
| 73 | + |
| 74 | + $savePath = $this->getPrivateProperty($handler, 'savePath'); |
| 75 | + |
| 76 | + $this->assertSame(10.0, $savePath['timeout']); |
| 77 | + } |
| 78 | + |
| 79 | + public function testOpen() |
| 80 | + { |
| 81 | + $handler = $this->getInstance(); |
| 82 | + $this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testWrite() |
| 86 | + { |
| 87 | + $handler = $this->getInstance(); |
| 88 | + $handler->open($this->sessionSavePath, $this->sessionName); |
| 89 | + $handler->read('555556b43phsnnf8if6bo33b635e4447'); |
| 90 | + |
| 91 | + $data = <<<'DATA' |
| 92 | + __ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value"; |
| 93 | + DATA; |
| 94 | + $this->assertTrue($handler->write('555556b43phsnnf8if6bo33b635e4447', $data)); |
| 95 | + |
| 96 | + $handler->close(); |
| 97 | + } |
| 98 | + |
| 99 | + public function testReadSuccess() |
| 100 | + { |
| 101 | + $handler = $this->getInstance(); |
| 102 | + $handler->open($this->sessionSavePath, $this->sessionName); |
| 103 | + |
| 104 | + $expected = <<<'DATA' |
| 105 | + __ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value"; |
| 106 | + DATA; |
| 107 | + $this->assertSame($expected, $handler->read('555556b43phsnnf8if6bo33b635e4447')); |
| 108 | + |
| 109 | + $handler->close(); |
| 110 | + } |
| 111 | + |
| 112 | + public function testReadFailure() |
| 113 | + { |
| 114 | + $handler = $this->getInstance(); |
| 115 | + $handler->open($this->sessionSavePath, $this->sessionName); |
| 116 | + |
| 117 | + $this->assertSame('', $handler->read('123456b43phsnnf8if6bo33b635e4321')); |
| 118 | + |
| 119 | + $handler->close(); |
| 120 | + } |
| 121 | + |
| 122 | + public function testGC() |
| 123 | + { |
| 124 | + $handler = $this->getInstance(); |
| 125 | + $this->assertSame(1, $handler->gc(3600)); |
| 126 | + } |
| 127 | +} |
0 commit comments