Skip to content

Commit 591c2a6

Browse files
committed
Add tests
1 parent c577ff4 commit 591c2a6

13 files changed

Lines changed: 666 additions & 2 deletions

File tree

.github/workflows/tests.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- '**.md'
7+
pull_request:
8+
paths-ignore:
9+
- '**.md'
10+
11+
jobs:
12+
tests:
13+
runs-on: ubuntu-latest
14+
15+
services:
16+
memcached:
17+
image: memcached:1.6-alpine
18+
ports:
19+
- 11211:11211
20+
redis:
21+
image: redis:5.0
22+
ports:
23+
- 6379:6379
24+
options: --entrypoint redis-server
25+
26+
strategy:
27+
fail-fast: true
28+
matrix:
29+
php: [ '7.4', '8.0', '8.1' ]
30+
stability: [ prefer-stable ]
31+
32+
name: PHP ${{ matrix.php }} tests
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v2
36+
37+
- name: Setup PHP
38+
uses: shivammathur/setup-php@v2
39+
with:
40+
php-version: ${{ matrix.php }}
41+
extensions: apcu, memcached, redis
42+
ini-values: apc.enable_cli=1
43+
coverage: none
44+
env:
45+
REDIS_CONFIGURE_OPTS: --enable-redis --enable-redis-igbinary --enable-redis-msgpack --enable-redis-lzf --with-liblzf --enable-redis-zstd --with-libzstd --enable-redis-lz4 --with-liblz4
46+
REDIS_LIBS: liblz4-dev, liblzf-dev, libzstd-dev
47+
48+
- name: Install dependencies
49+
run: composer update --prefer-dist --no-interaction --no-progress
50+
- name: Execute tests
51+
run: vendor/bin/phpunit --verbose

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.lock
66
node_modules
77
package-lock.json
88
/config.php
9+
/.phpunit.result.cache

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ To add another server, add the same environment variables, but change 0 to 1 (2
9898
- PHP >= 7.4
9999
- redis, memcache(d), opcache or apcu php extensions (if none of them is installed, only the Server tab will be available)
100100

101+
## Testing
102+
103+
PHPUnit
104+
105+
```
106+
composer test
107+
```
108+
109+
PHPStan
110+
111+
```
112+
composer phpstan
113+
```
114+
101115
## Development
102116
103117
For compiling Tailwind CSS run `npm install` and then

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
},
2828
"require-dev": {
2929
"clue/phar-composer": "^1.4",
30-
"phpstan/phpstan": "^1.8"
30+
"phpstan/phpstan": "^1.8",
31+
"phpunit/phpunit": "^9.5"
3132
},
3233
"suggest": {
3334
"ext-apcu": "Required for the APCu dashboard.",
@@ -42,8 +43,14 @@
4243
"RobiNN\\Pca\\": "src/"
4344
}
4445
},
46+
"autoload-dev": {
47+
"psr-4": {
48+
"Tests\\": "tests/"
49+
}
50+
},
4551
"scripts": {
4652
"phar": "phar-composer build twig/twig",
47-
"phpstan": "phpstan --ansi"
53+
"phpstan": "phpstan --ansi",
54+
"test": "phpunit --colors=always"
4855
}
4956
}

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php" colors="true">
2+
<testsuites>
3+
<testsuite name="phpCacheAdmin">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

tests/Dashboards/APCuTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Dashboards;
14+
15+
use RobiNN\Pca\Dashboards\APCu\APCuDashboard;
16+
use RobiNN\Pca\Template;
17+
use Tests\TestCase;
18+
19+
final class APCuTest extends TestCase {
20+
private Template $template;
21+
22+
private APCuDashboard $apcu;
23+
24+
protected function setUp(): void {
25+
$this->template = new Template();
26+
$this->apcu = new APCuDashboard($this->template);
27+
}
28+
29+
public function testDeleteKey(): void {
30+
$key = 'pu-test-delete-key';
31+
32+
apcu_store($key, 'data');
33+
34+
$_GET['delete'] = $key;
35+
36+
$this->assertSame(
37+
$this->template->render('components/alert', ['message' => 'Key "'.$key.'" has been deleted.']),
38+
self::callMethod($this->apcu, 'deleteKey')
39+
);
40+
$this->assertFalse(apcu_exists($key));
41+
}
42+
43+
public function testDeleteKeys(): void {
44+
$key1 = 'pu-test-delete-key1';
45+
$key2 = 'pu-test-delete-key2';
46+
$key3 = 'pu-test-delete-key3';
47+
48+
apcu_store($key1, 'data1');
49+
apcu_store($key2, 'data2');
50+
apcu_store($key3, 'data3');
51+
52+
$_GET['delete'] = implode(',', [$key1, $key2, $key3]);
53+
54+
$this->assertSame(
55+
$this->template->render('components/alert', ['message' => 'Keys has been deleted.']),
56+
self::callMethod($this->apcu, 'deleteKey')
57+
);
58+
$this->assertFalse(apcu_exists($key1));
59+
$this->assertFalse(apcu_exists($key2));
60+
$this->assertFalse(apcu_exists($key3));
61+
}
62+
63+
public function testGetKey(): void {
64+
$keys = [
65+
'string' => ['original' => 'phpCacheAdmin', 'expected' => 'phpCacheAdmin'],
66+
'int' => ['original' => 23, 'expected' => '23'],
67+
'float' => ['original' => 23.99, 'expected' => '23.99'],
68+
'bool' => ['original' => true, 'expected' => '1'],
69+
'null' => ['original' => null, 'expected' => ''],
70+
'array' => [
71+
'original' => ['key1', 'key2'],
72+
'expected' => 'a:2:{i:0;s:4:"key1";i:1;s:4:"key2";}',
73+
],
74+
'object' => [
75+
'original' => (object) ['key1', 'key2'],
76+
'expected' => 'O:8:"stdClass":2:{s:1:"0";s:4:"key1";s:1:"1";s:4:"key2";}',
77+
],
78+
];
79+
80+
foreach ($keys as $key => $value) {
81+
apcu_store('pu-test-'.$key, $value['original']);
82+
}
83+
84+
$this->assertSame($keys['string']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-string'));
85+
$this->assertSame($keys['int']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-int'));
86+
$this->assertSame($keys['float']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-float'));
87+
$this->assertSame($keys['bool']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-bool'));
88+
$this->assertSame($keys['null']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-null'));
89+
$this->assertSame($keys['array']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-array'));
90+
$this->assertSame($keys['object']['expected'], self::callMethod($this->apcu, 'getKey', 'pu-test-object'));
91+
92+
foreach ($keys as $key => $value) {
93+
apcu_delete('pu-test-'.$key);
94+
}
95+
}
96+
}

tests/Dashboards/MemcachedTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Dashboards;
14+
15+
use RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility\Memcache;
16+
use RobiNN\Pca\Dashboards\Memcached\MemcacheCompatibility\Memcached;
17+
use RobiNN\Pca\Dashboards\Memcached\MemcachedDashboard;
18+
use RobiNN\Pca\Template;
19+
use Tests\TestCase;
20+
21+
final class MemcachedTest extends TestCase {
22+
private Template $template;
23+
24+
private MemcachedDashboard $dashboard;
25+
26+
/**
27+
* @var Memcache|Memcached
28+
*/
29+
private $memcached;
30+
31+
protected function setUp(): void {
32+
$this->template = new Template();
33+
$this->dashboard = new MemcachedDashboard($this->template);
34+
$this->memcached = self::callMethod($this->dashboard, 'connect', ['host' => '127.0.0.1']);
35+
}
36+
37+
public function testDeleteKey(): void {
38+
$key = 'pu-test-delete-key';
39+
40+
$this->memcached->set($key, 'data');
41+
42+
$_GET['delete'] = $key;
43+
44+
$this->assertSame(
45+
$this->template->render('components/alert', ['message' => 'Key "'.$key.'" has been deleted.']),
46+
self::callMethod($this->dashboard, 'deleteKey', $this->memcached)
47+
);
48+
$this->assertFalse($this->memcached->exists($key));
49+
}
50+
51+
public function testDeleteKeys(): void {
52+
$key1 = 'pu-test-delete-key1';
53+
$key2 = 'pu-test-delete-key2';
54+
$key3 = 'pu-test-delete-key3';
55+
56+
$this->memcached->set($key1, 'data1');
57+
$this->memcached->set($key2, 'data2');
58+
$this->memcached->set($key3, 'data3');
59+
60+
$_GET['delete'] = implode(',', [$key1, $key2, $key3]);
61+
62+
$this->assertSame(
63+
$this->template->render('components/alert', ['message' => 'Keys has been deleted.']),
64+
self::callMethod($this->dashboard, 'deleteKey', $this->memcached)
65+
);
66+
$this->assertFalse($this->memcached->exists($key1));
67+
$this->assertFalse($this->memcached->exists($key2));
68+
$this->assertFalse($this->memcached->exists($key3));
69+
}
70+
71+
public function testGetKey(): void {
72+
$keys = [
73+
'string' => ['original' => 'phpCacheAdmin', 'expected' => 'phpCacheAdmin'],
74+
'int' => ['original' => 23, 'expected' => '23'],
75+
'float' => ['original' => 23.99, 'expected' => '23.99'],
76+
'bool' => ['original' => true, 'expected' => '1'],
77+
'null' => ['original' => null, 'expected' => ''],
78+
'array' => [
79+
'original' => ['key1', 'key2'],
80+
'expected' => 'a:2:{i:0;s:4:"key1";i:1;s:4:"key2";}',
81+
],
82+
'object' => [
83+
'original' => (object) ['key1', 'key2'],
84+
'expected' => 'O:8:"stdClass":2:{s:1:"0";s:4:"key1";s:1:"1";s:4:"key2";}',
85+
],
86+
];
87+
88+
foreach ($keys as $key => $value) {
89+
$this->memcached->set('pu-test-'.$key, $value['original']);
90+
}
91+
92+
$this->assertSame($keys['string']['expected'], $this->memcached->getKey('pu-test-string'));
93+
$this->assertSame($keys['int']['expected'], $this->memcached->getKey('pu-test-int'));
94+
$this->assertSame($keys['float']['expected'], $this->memcached->getKey('pu-test-float'));
95+
$this->assertSame($keys['bool']['expected'], $this->memcached->getKey('pu-test-bool'));
96+
$this->assertSame($keys['null']['expected'], $this->memcached->getKey('pu-test-null'));
97+
$this->assertSame($keys['array']['expected'], $this->memcached->getKey('pu-test-array'));
98+
$this->assertSame($keys['object']['expected'], $this->memcached->getKey('pu-test-object'));
99+
100+
foreach ($keys as $key => $value) {
101+
$this->memcached->delete('pu-test-'.$key);
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)