Skip to content

Commit 17a8a0b

Browse files
committed
Add file cache tests
1 parent 4083c7c commit 17a8a0b

File tree

4 files changed

+57
-38
lines changed

4 files changed

+57
-38
lines changed

src/Cache/Adapter/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct() {
2525
$this->path = APP_PATH . substr($this->path, 5);
2626
}
2727
if (!is_dir($this->path)) {
28-
mkdir($this->path);
28+
mkdir($this->path, 0777, true);
2929
}
3030
if (substr($this->path, -1) !== '/') {
3131
$this->path .= '/';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace YesfTest\Cache\Adapter;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Yesf\Yesf;
6+
use Yesf\Cache\Adapter\File as YesfFile;
7+
use YesfTest\Cache\TestUtils;
8+
9+
class FileTest extends TestCase {
10+
public function testFile() {
11+
$handler = new YesfFile();
12+
TestUtils::single($this, $handler);
13+
TestUtils::multi($this, $handler);
14+
}
15+
}

tests/Cases/Cache/Adapter/YacTest.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,15 @@
44
use PHPUnit\Framework\TestCase;
55
use Yesf\Yesf;
66
use Yesf\Cache\Adapter\Yac as YesfYac;
7+
use YesfTest\Cache\TestUtils;
78

89
class YacTest extends TestCase {
9-
public static $handler;
10-
public static function setUpBeforeClass() {
11-
self::$handler = new YesfYac();
12-
}
13-
/**
14-
* @requires extension yac
15-
*/
16-
public function testSingle() {
17-
$arr = [1, 2, 3];
18-
$key = uniqid();
19-
self::$handler->set($key, $arr);
20-
$this->assertSame($arr, self::$handler->get($key));
21-
$this->assertNull(self::$handler->get(uniqid()));
22-
$default = rand(1, 999);
23-
$this->assertSame($default, self::$handler->get(uniqid(), $default));
24-
self::$handler->delete($key);
25-
$this->assertNull(self::$handler->get($key));
26-
}
2710
/**
2811
* @requires extension yac
2912
*/
30-
public function testMulti() {
31-
$arr = [
32-
'key1' => 123,
33-
'key2' => "string",
34-
'key3' => [1, 2, 3]
35-
];
36-
self::$handler->setMultiple($arr);
37-
$this->assertSame($arr['key1'], self::$handler->get('key1'));
38-
$this->assertSame($arr['key2'], self::$handler->get('key2'));
39-
$this->assertSame($arr['key3'], self::$handler->get('key3'));
40-
$this->assertSame($arr, self::$handler->getMultiple(array_keys($arr)));
41-
$arr['key1'] = rand(1, 999);
42-
self::$handler->set('key1', $arr['key1']);
43-
$this->assertSame($arr, self::$handler->getMultiple(array_keys($arr)));
44-
$this->assertSame([
45-
'not_exists' => 0,
46-
'key1' => $arr['key1'],
47-
'not_exists_2' => 0
48-
], self::$handler->getMultiple(['not_exists', 'key1', 'not_exists_2'], 0));
13+
public function testYac() {
14+
$handler = new YesfYac();
15+
TestUtils::single($this, $handler);
16+
TestUtils::multi($this, $handler);
4917
}
5018
}

tests/Cases/Cache/TestUtils.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace YesfTest\Cache;
3+
4+
class TestUtils {
5+
public static function single($that, $handler) {
6+
$arr = [1, 2, 3];
7+
$key = uniqid();
8+
$handler->set($key, $arr);
9+
$that->assertSame($arr, $handler->get($key));
10+
$that->assertNull($handler->get(uniqid()));
11+
$default = rand(1, 999);
12+
$that->assertSame($default, $handler->get(uniqid(), $default));
13+
$handler->delete($key);
14+
$that->assertNull($handler->get($key));
15+
}
16+
public static function multi($that, $handler) {
17+
$arr = [
18+
'key1' => 123,
19+
'key2' => "string",
20+
'key3' => [1, 2, 3]
21+
];
22+
$handler->setMultiple($arr);
23+
$that->assertSame($arr['key1'], $handler->get('key1'));
24+
$that->assertSame($arr['key2'], $handler->get('key2'));
25+
$that->assertSame($arr['key3'], $handler->get('key3'));
26+
$that->assertSame($arr, $handler->getMultiple(array_keys($arr)));
27+
$arr['key1'] = rand(1, 999);
28+
$handler->set('key1', $arr['key1']);
29+
$that->assertSame($arr, $handler->getMultiple(array_keys($arr)));
30+
$that->assertSame([
31+
'not_exists' => 0,
32+
'key1' => $arr['key1'],
33+
'not_exists_2' => 0
34+
], $handler->getMultiple(['not_exists', 'key1', 'not_exists_2'], 0));
35+
}
36+
}

0 commit comments

Comments
 (0)