Skip to content

Commit d230eb3

Browse files
committed
Update tests
1 parent 52e7be0 commit d230eb3

5 files changed

Lines changed: 159 additions & 2 deletions

File tree

.github/workflows/static-analysis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ jobs:
2626

2727
- name: Install dependencies
2828
run: composer update --prefer-dist --no-interaction --no-progress
29+
- name: Install Predis
30+
run: composer require predis/predis
2931
- name: Execute static analysis
3032
run: vendor/bin/phpstan

.github/workflows/tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ jobs:
4747

4848
- name: Install dependencies
4949
run: composer update --prefer-dist --no-interaction --no-progress
50+
- name: Install Predis
51+
run: composer require predis/predis
5052
- name: Execute tests
5153
run: vendor/bin/phpunit --verbose

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ parameters:
22
level: 6
33
paths:
44
- src
5-
excludePaths:
6-
- src/Dashboards/Redis/Compatibility/Predis.php

tests/Clients/PHPMemTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

tests/Clients/PredisTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\DashboardException;
16+
use RobiNN\Pca\Dashboards\Redis\Compatibility\Predis;
17+
use Tests\TestCase;
18+
19+
final class PredisTest extends TestCase {
20+
private Predis $predis;
21+
22+
protected function setUp(): void {
23+
$this->predis = new Predis(['host' => '127.0.0.1']);
24+
$this->predis->connect();
25+
}
26+
27+
/**
28+
* @throws DashboardException
29+
*/
30+
public function testGetType(): void {
31+
$this->predis->set('pu-pred-test-string', 'value');
32+
$this->predis->sadd('pu-pred-test-set', 'value1', 'value2', 'value3');
33+
$this->predis->rpush('pu-pred-test-list', 'value1', 'value2', 'value3');
34+
$this->predis->zadd('pu-pred-test-zset', 0, 'value1', 1, 'value2', 2, 'value3');
35+
$this->predis->hset('pu-pred-test-hash', 'hashkey1', 'value1');
36+
$this->predis->hset('pu-pred-test-hash', 'hashkey2', 'value2');
37+
$this->predis->xAdd('pu-pred-test-stream', '*', ['field1' => 'value1', 'field2' => 'value2']);
38+
$this->predis->xAdd('pu-pred-test-stream', '*', ['field3' => 'value3']);
39+
40+
$this->assertSame('string', $this->predis->getType('pu-pred-test-string'));
41+
$this->assertSame('set', $this->predis->getType('pu-pred-test-set'));
42+
$this->assertSame('list', $this->predis->getType('pu-pred-test-list'));
43+
$this->assertSame('zset', $this->predis->getType('pu-pred-test-zset'));
44+
$this->assertSame('hash', $this->predis->getType('pu-pred-test-hash'));
45+
$this->assertSame('stream', $this->predis->getType('pu-pred-test-stream'));
46+
}
47+
48+
public function testDelete(): void {
49+
$this->predis->del('pu-pred-test-string');
50+
$this->predis->del('pu-pred-test-set');
51+
$this->predis->del('pu-pred-test-list');
52+
$this->predis->del('pu-pred-test-zset');
53+
$this->predis->del('pu-pred-test-hash');
54+
$this->predis->del('pu-pred-test-stream');
55+
56+
$this->assertSame(0, $this->predis->exists('pu-pred-test-string'));
57+
$this->assertSame(0, $this->predis->exists('pu-pred-test-set'));
58+
$this->assertSame(0, $this->predis->exists('pu-pred-test-list'));
59+
$this->assertSame(0, $this->predis->exists('pu-pred-test-zset'));
60+
$this->assertSame(0, $this->predis->exists('pu-pred-test-hash'));
61+
$this->assertSame(0, $this->predis->exists('pu-pred-test-stream'));
62+
}
63+
}

0 commit comments

Comments
 (0)