1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Views; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Spiral\Core\Container; |
16
|
|
|
use Spiral\Views\Context\ValueDependency; |
17
|
|
|
use Spiral\Views\ContextInterface; |
18
|
|
|
use Spiral\Views\Engine\Native\NativeEngine; |
19
|
|
|
use Spiral\Views\Exception\CacheException; |
20
|
|
|
use Spiral\Views\ViewCache; |
21
|
|
|
use Spiral\Views\ViewContext; |
22
|
|
|
use Spiral\Views\ViewInterface; |
23
|
|
|
use Spiral\Views\ViewLoader; |
24
|
|
|
|
25
|
|
|
class CacheTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testSimpleCache(): void |
28
|
|
|
{ |
29
|
|
|
$ctx = new ViewContext(); |
30
|
|
|
$cache = new ViewCache(); |
31
|
|
|
|
32
|
|
|
$view = $this->getView($ctx, 'default:view'); |
33
|
|
|
|
34
|
|
|
$cache->set($ctx, 'default:view', $view); |
35
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
36
|
|
|
$this->assertSame($view, $cache->get($ctx, 'default:view')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGet(): void |
40
|
|
|
{ |
41
|
|
|
$this->expectException(CacheException::class); |
42
|
|
|
|
43
|
|
|
$cache = new ViewCache(); |
44
|
|
|
$cache->get(new ViewContext(), 'default:view'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testReset(): void |
48
|
|
|
{ |
49
|
|
|
$ctx = new ViewContext(); |
50
|
|
|
$ctx2 = $ctx->withDependency(new ValueDependency('test', 'value')); |
51
|
|
|
|
52
|
|
|
$cache = new ViewCache(); |
53
|
|
|
|
54
|
|
|
$view = $this->getView($ctx, 'default:view'); |
55
|
|
|
$view2 = $this->getView($ctx2, 'other:view'); |
56
|
|
|
|
57
|
|
|
$cache->set($ctx, 'default:view', $view); |
58
|
|
|
$cache->set($ctx2, 'other:view', $view2); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
61
|
|
|
$this->assertTrue($cache->has($ctx2, 'other:view')); |
62
|
|
|
|
63
|
|
|
$cache->reset($ctx); |
64
|
|
|
|
65
|
|
|
$this->assertFalse($cache->has($ctx, 'default:view')); |
66
|
|
|
$this->assertTrue($cache->has($ctx2, 'other:view')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testResetAll(): void |
70
|
|
|
{ |
71
|
|
|
$ctx = new ViewContext(); |
72
|
|
|
$ctx2 = $ctx->withDependency(new ValueDependency('test', 'value')); |
73
|
|
|
|
74
|
|
|
$cache = new ViewCache(); |
75
|
|
|
|
76
|
|
|
$view = $this->getView($ctx, 'default:view'); |
77
|
|
|
$view2 = $this->getView($ctx2, 'other:view'); |
78
|
|
|
|
79
|
|
|
$cache->set($ctx, 'default:view', $view); |
80
|
|
|
$cache->set($ctx2, 'other:view', $view2); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
83
|
|
|
$this->assertTrue($cache->has($ctx2, 'other:view')); |
84
|
|
|
|
85
|
|
|
$cache->reset(); |
86
|
|
|
|
87
|
|
|
$this->assertFalse($cache->has($ctx, 'default:view')); |
88
|
|
|
$this->assertFalse($cache->has($ctx2, 'other:view')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testResetPath(): void |
92
|
|
|
{ |
93
|
|
|
$ctx = new ViewContext(); |
94
|
|
|
$cache = new ViewCache(); |
95
|
|
|
|
96
|
|
|
$view = $this->getView($ctx, 'default:view'); |
97
|
|
|
$view2 = $this->getView($ctx, 'other:view'); |
98
|
|
|
|
99
|
|
|
$cache->set($ctx, 'default:view', $view); |
100
|
|
|
$cache->set($ctx, 'other:view', $view2); |
101
|
|
|
|
102
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
103
|
|
|
$this->assertTrue($cache->has($ctx, 'other:view')); |
104
|
|
|
|
105
|
|
|
$cache->resetPath('other:view'); |
106
|
|
|
|
107
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
108
|
|
|
$this->assertFalse($cache->has($ctx, 'other:view')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testContextValue(): void |
112
|
|
|
{ |
113
|
|
|
$ctx = new ViewContext(); |
114
|
|
|
$ctx = $ctx->withDependency(new ValueDependency('test', 'value')); |
115
|
|
|
|
116
|
|
|
$cache = new ViewCache(); |
117
|
|
|
|
118
|
|
|
$view = $this->getView($ctx, 'default:view'); |
119
|
|
|
|
120
|
|
|
$cache->set($ctx, 'default:view', $view); |
121
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
122
|
|
|
|
123
|
|
|
$ctx = $ctx->withDependency(new ValueDependency('test', 'another')); |
124
|
|
|
$this->assertFalse($cache->has($ctx, 'default:view')); |
125
|
|
|
|
126
|
|
|
$ctx = $ctx->withDependency(new ValueDependency('test', 'value')); |
127
|
|
|
$this->assertTrue($cache->has($ctx, 'default:view')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getView(ContextInterface $context, string $path): ViewInterface |
131
|
|
|
{ |
132
|
|
|
$loader = new ViewLoader([ |
133
|
|
|
'default' => __DIR__ . '/fixtures/default', |
134
|
|
|
'other' => __DIR__ . '/fixtures/other', |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
$engine = new NativeEngine(new Container()); |
138
|
|
|
$engine = $engine->withLoader($loader->withExtension('php')); |
139
|
|
|
|
140
|
|
|
return $engine->get($path, $context); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|