master
ブランチ master
LittleBeast / src / Std / Test / LibCache.php
6.6 KB、147行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Test;
39
40require_once __DIR__.'/../../../autoload.php';
41
42use Std\Lib\Tester;
43use Std\Lib\Cache;
44
45$test = new Tester([
46 'colorOutput' => true,
47 'verboseOutput' => true
48]);
49
50$base = sys_get_temp_dir().'/lbcache_'.bin2hex(random_bytes(6));
51
52$test->describe('キャッシュ', function($test) use ($base): void {
53 $test->it('保存して受け取れるはず(ディレクトリも作るはず)', function($test) use ($base): void {
54 $cache = new Cache('test', $base);
55 $res = $cache->set('domainlist', ['a' => 1, 'b' => ['日本語']]);
56 $test->assertTrue($res->isSuccess);
57 $test->assertTrue(is_file("{$base}/test/domainlist.json"));
58
59 $data = $cache->get('domainlist');
60 $test->assertEquals(1, $data['a']);
61 $test->assertEquals(['日本語'], $data['b']);
62 $test->assertTrue(is_int($data['lifespan']));
63 });
64
65 $test->it('無いキャッシュは空の配列のはず', function($test) use ($base): void {
66 $test->assertEquals([], (new Cache('test', $base))->get('nothing'));
67 $test->assertEquals([], (new Cache('nodir', $base))->get('x'));
68 });
69
70 $test->it('壊れたJSONは空の配列のはず', function($test) use ($base): void {
71 file_put_contents("{$base}/test/broken.json", '{"a":');
72 $test->assertEquals([], (new Cache('test', $base))->get('broken'));
73 });
74
75 $test->it('有効期限を確認出来るはず', function($test) use ($base): void {
76 $cache = new Cache('test', $base);
77 $cache->set('q', ['query' => ['page' => 1]]);
78 $data = $cache->get('q');
79 $test->assertTrue($cache->resurrect($data));
80 $test->assertTrue($cache->resurrect($data, ['page' => 1]));
81 $test->assertFalse($cache->resurrect($data, ['page' => 2]));
82 $test->assertFalse($cache->resurrect([]));
83
84 $expired = $data;
85 $expired['lifespan'] = time() - 3600;
86 $test->assertFalse($cache->resurrect($expired));
87
88 // 有効期限を変えられる(protected)
89 $short = new class('test', $base) extends Cache {
90 protected int $dataDuration = 0;
91 };
92 $test->assertFalse($short->resurrect($data));
93 });
94
95 $test->it('削除出来るはず', function($test) use ($base): void {
96 $cache = new Cache('test', $base);
97 $test->assertTrue($cache->murder('domainlist')->isSuccess);
98 $test->assertEquals([], $cache->get('domainlist'));
99 $test->assertFalse($cache->murder('domainlist')->isSuccess);
100 });
101
102 $test->it('名前の始まりでまとめて削除出来るはず', function($test) use ($base): void {
103 $cache = new Cache('prefix', $base);
104 foreach (['dnszone-list', 'dnszone-076.moe', 'dnsrecords-076.moe', 'login'] as $n) $cache->set($n, ['x' => 1]);
105 $test->assertEquals(2, $cache->murderPrefix('dnszone-'));
106 $test->assertEquals([], $cache->get('dnszone-list'));
107 $test->assertEquals([], $cache->get('dnszone-076.moe'));
108 $test->assertEquals(1, $cache->get('dnsrecords-076.moe')['x'] ?? null, '他の名前は残る');
109 $test->assertEquals(1, $cache->get('login')['x'] ?? null);
110 $test->assertEquals(0, $cache->murderPrefix(''), '空は何も消さない');
111 $test->assertEquals(0, $cache->murderPrefix('../'));
112 $test->assertEquals(1, $cache->get('login')['x'] ?? null);
113 });
114
115 $test->it('種類と中身の一覧を出し、全部削除出来るはず', function($test) use ($base): void {
116 $test->assertEquals([], Cache::kinds($base.'/nothing'), 'ディレクトリが無ければ空');
117 $cache = new Cache('list', $base);
118 $cache->set('b', ['x' => 1]);
119 $cache->set('a', ['y' => 2]);
120 $test->assertTrue(in_array('list', Cache::kinds($base), true));
121 $entries = $cache->entries();
122 $test->assertEquals(['a', 'b'], array_column($entries, 'name'));
123 $test->assertTrue($entries[0]['fresh']);
124 $test->assertTrue($entries[0]['size'] > 0);
125 $test->assertTrue(abs($entries[0]['created'] - time()) < 5);
126 $test->assertEquals(2, $cache->murderAll());
127 $test->assertEquals([], $cache->entries());
128 $test->assertFalse(is_dir("{$base}/list"), '空のディレクトリも消す');
129 $test->assertFalse(in_array('list', Cache::kinds($base), true));
130 });
131
132 $test->it('ディレクトリの外のファイル名を拒否するはず', function($test) use ($base): void {
133 $cache = new Cache('test', $base);
134 foreach (['../x', '..', 'a/b', 'a\\b', '', str_repeat('a', 201)] as $bad) {
135 $test->assertFalse($cache->set($bad, [])->isSuccess, "「{$bad}」が拒否されなかった");
136 $test->assertEquals([], $cache->get($bad));
137 $test->assertFalse($cache->murder($bad)->isSuccess);
138 }
139 $test->assertThrows(fn() => new Cache('../../etc', $base), \InvalidArgumentException::class);
140 });
141});
142
143foreach (glob("{$base}/*/*") ?: [] as $f) @unlink($f);
144foreach (glob("{$base}/*") ?: [] as $d) @rmdir($d);
145@rmdir($base);
146
147$test->printSummary();