1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
5
|
|
|
use Silviooosilva\CacheerPhp\Config\Option\Builder\OptionBuilder; |
6
|
|
|
|
7
|
|
|
class OptionBuildTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $cache; |
11
|
|
|
private $cacheDir; |
12
|
|
|
|
13
|
|
|
protected function setUp(): void |
14
|
|
|
{ |
15
|
|
|
$this->cacheDir = __DIR__ . '/cache'; |
16
|
|
|
if (!file_exists($this->cacheDir) || !is_dir($this->cacheDir)) { |
17
|
|
|
mkdir($this->cacheDir, 0755, true); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$this->cache = new Cacheer(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function tearDown(): void |
24
|
|
|
{ |
25
|
|
|
$this->cache->flushCache(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_it_can_set_cache_directory() |
29
|
|
|
{ |
30
|
|
|
$cacheDir = __DIR__ . "/cache"; |
31
|
|
|
|
32
|
|
|
$options = OptionBuilder::forFile() |
33
|
|
|
->dir($cacheDir) |
34
|
|
|
->build(); |
35
|
|
|
|
36
|
|
|
$this->assertArrayHasKey('cacheDir', $options); |
37
|
|
|
$this->assertEquals($cacheDir, $options['cacheDir']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
public function test_it_can_set_expiration_time() |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
$options = OptionBuilder::forFile() |
45
|
|
|
->expirationTime('2 hours') |
46
|
|
|
->build(); |
47
|
|
|
|
48
|
|
|
$this->assertArrayHasKey('expirationTime', $options); |
49
|
|
|
$this->assertEquals('2 hours', $options['expirationTime']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_it_can_set_flush_after() |
53
|
|
|
{ |
54
|
|
|
$options = OptionBuilder::forFile() |
55
|
|
|
->flushAfter('11 seconds') |
56
|
|
|
->build(); |
57
|
|
|
|
58
|
|
|
$this->assertArrayHasKey('flushAfter', $options); |
59
|
|
|
$this->assertEquals('11 seconds', $options['flushAfter']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_it_can_set_multiple_options_together() |
63
|
|
|
{ |
64
|
|
|
$cacheDir = __DIR__ . "/cache"; |
65
|
|
|
|
66
|
|
|
$options = OptionBuilder::forFile() |
67
|
|
|
->dir($cacheDir) |
68
|
|
|
->expirationTime('1 day') |
69
|
|
|
->flushAfter('30 minutes') |
70
|
|
|
->build(); |
71
|
|
|
|
72
|
|
|
$this->assertEquals([ |
73
|
|
|
'cacheDir' => $cacheDir, |
74
|
|
|
'expirationTime' => '1 day', |
75
|
|
|
'flushAfter' => '30 minutes', |
76
|
|
|
], $options); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function test_it_allows_setting_expiration_time_with_timebuilder() |
80
|
|
|
{ |
81
|
|
|
$options = OptionBuilder::forFile()->expirationTime()->week(1)->build(); |
82
|
|
|
$this->assertArrayHasKey('expirationTime', $options); |
83
|
|
|
$this->assertEquals('1 weeks', $options['expirationTime']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test_it_allows_setting_flush_after_with_timebuilder() |
87
|
|
|
{ |
88
|
|
|
$options = OptionBuilder::forFile()->flushAfter()->second(10)->build(); |
89
|
|
|
$this->assertArrayHasKey('flushAfter', $options); |
90
|
|
|
$this->assertEquals('10 seconds', $options['flushAfter']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function test_it_can_set_multiple_options_together_with_timebuilder() |
94
|
|
|
{ |
95
|
|
|
$cacheDir = __DIR__ . "/cache"; |
96
|
|
|
$options = OptionBuilder::forFile() |
97
|
|
|
->dir($cacheDir) |
98
|
|
|
->expirationTime()->week(1) |
99
|
|
|
->flushAfter()->minute(10) |
100
|
|
|
->build(); |
101
|
|
|
|
102
|
|
|
$this->assertEquals([ |
103
|
|
|
'cacheDir' => $cacheDir, |
104
|
|
|
'expirationTime' => '1 weeks', |
105
|
|
|
'flushAfter' => '10 minutes', |
106
|
|
|
], $options); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function test_it_returns_empty_array_when_no_options_are_set() |
110
|
|
|
{ |
111
|
|
|
$options = OptionBuilder::forFile()->build(); |
112
|
|
|
$this->assertIsArray($options); |
113
|
|
|
$this->assertEmpty($options); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|