|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of phpCacheAdmin. |
| 4 | + * |
| 5 | + * Copyright (c) Róbert Kelčák (https://kelcak.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 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace Tests\Clients; |
| 14 | + |
| 15 | +use RobiNN\Pca\Dashboards\Memcached\Compatibility\PHPMem; |
| 16 | +use RobiNN\Pca\Dashboards\Memcached\MemcachedException; |
| 17 | +use Tests\TestCase; |
| 18 | + |
| 19 | +final class PHPMemTest extends TestCase { |
| 20 | + private PHPMem $phpmem; |
| 21 | + |
| 22 | + protected function setUp(): void { |
| 23 | + $server = ['host' => '127.0.0.1']; |
| 24 | + $this->phpmem = new PHPMem($server); |
| 25 | + $this->phpmem->addServer($server['host']); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @throws MemcachedException |
| 30 | + */ |
| 31 | + public function testIsConnected(): void { |
| 32 | + $this->assertTrue($this->phpmem->isConnected()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @throws MemcachedException |
| 37 | + */ |
| 38 | + public function testSetGetKey(): void { |
| 39 | + $keys = [ |
| 40 | + 'string' => ['original' => 'phpCacheAdmin', 'expected' => 'phpCacheAdmin'], |
| 41 | + 'int' => ['original' => 23, 'expected' => '23'], |
| 42 | + 'float' => ['original' => 23.99, 'expected' => '23.99'], |
| 43 | + 'bool' => ['original' => true, 'expected' => '1'], |
| 44 | + 'null' => ['original' => null, 'expected' => ''], |
| 45 | + 'array' => [ |
| 46 | + 'original' => ['key1', 'key2'], |
| 47 | + 'expected' => 'a:2:{i:0;s:4:"key1";i:1;s:4:"key2";}', |
| 48 | + ], |
| 49 | + 'object' => [ |
| 50 | + 'original' => (object) ['key1', 'key2'], |
| 51 | + 'expected' => 'O:8:"stdClass":2:{s:1:"0";s:4:"key1";s:1:"1";s:4:"key2";}', |
| 52 | + ], |
| 53 | + ]; |
| 54 | + |
| 55 | + foreach ($keys as $key => $value) { |
| 56 | + $this->phpmem->set('pu-pmem-test-'.$key, $value['original']); |
| 57 | + } |
| 58 | + |
| 59 | + $this->assertSame($keys['string']['expected'], $this->phpmem->getKey('pu-pmem-test-string')); |
| 60 | + $this->assertSame($keys['int']['expected'], $this->phpmem->getKey('pu-pmem-test-int')); |
| 61 | + $this->assertSame($keys['float']['expected'], $this->phpmem->getKey('pu-pmem-test-float')); |
| 62 | + $this->assertSame($keys['bool']['expected'], $this->phpmem->getKey('pu-pmem-test-bool')); |
| 63 | + $this->assertSame($keys['null']['expected'], $this->phpmem->getKey('pu-pmem-test-null')); |
| 64 | + $this->assertSame($keys['array']['expected'], $this->phpmem->getKey('pu-pmem-test-array')); |
| 65 | + $this->assertSame($keys['object']['expected'], $this->phpmem->getKey('pu-pmem-test-object')); |
| 66 | + |
| 67 | + foreach ($keys as $key => $value) { |
| 68 | + $this->phpmem->delete('pu-pmem-test-'.$key); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @throws MemcachedException |
| 74 | + */ |
| 75 | + public function testDeleteKey(): void { |
| 76 | + $key = 'pu-pmem-test-delete-key'; |
| 77 | + |
| 78 | + $this->phpmem->set($key, 'data'); |
| 79 | + |
| 80 | + $this->assertTrue($this->phpmem->delete($key)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @throws MemcachedException |
| 85 | + */ |
| 86 | + public function testGetServerStats(): void { |
| 87 | + $stats = $this->phpmem->getServerStats(); |
| 88 | + |
| 89 | + $this->assertIsArray($stats); |
| 90 | + $this->assertArrayHasKey('version', $stats); |
| 91 | + } |
| 92 | +} |
0 commit comments