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\Config\ViewsConfig; |
17
|
|
|
use Spiral\Views\Context\ValueDependency; |
18
|
|
|
use Spiral\Views\Engine\Native\NativeEngine; |
19
|
|
|
use Spiral\Views\Exception\ViewException; |
20
|
|
|
use Spiral\Views\ViewCache; |
21
|
|
|
use Spiral\Views\ViewManager; |
22
|
|
|
|
23
|
|
|
class ManagerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
protected $container; |
26
|
|
|
|
27
|
|
|
public function setUp(): void |
28
|
|
|
{ |
29
|
|
|
$this->container = new Container(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testMultipleEngines(): void |
33
|
|
|
{ |
34
|
|
|
$manager = $this->makeManager(); |
35
|
|
|
|
36
|
|
|
$manager->addEngine(new NativeEngine($this->container, 'dark.php')); |
37
|
|
|
|
38
|
|
|
$ext = []; |
39
|
|
|
foreach ($manager->getEngines() as $e) { |
40
|
|
|
$ext[] = $e->getLoader()->getExtension(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->assertSame(['dark.php', 'php'], $ext); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testRender(): void |
47
|
|
|
{ |
48
|
|
|
$manager = $this->makeManager(); |
49
|
|
|
$manager->addDependency(new ValueDependency('name', 'hello')); |
50
|
|
|
|
51
|
|
|
$this->assertSame('hello', $manager->render('other:var', ['value' => 'hello'])); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGet(): void |
55
|
|
|
{ |
56
|
|
|
$manager = $this->makeManager(); |
57
|
|
|
$manager->addDependency(new ValueDependency('name', 'hello')); |
58
|
|
|
|
59
|
|
|
$view = $manager->get('other:var'); |
60
|
|
|
$this->assertSame($view, $manager->get('other:var')); |
61
|
|
|
|
62
|
|
|
$manager->reset('other:var'); |
63
|
|
|
$this->assertNotSame($view, $manager->get('other:var')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCompile(): void |
67
|
|
|
{ |
68
|
|
|
$manager = $this->makeManager(); |
69
|
|
|
|
70
|
|
|
$r = new \ReflectionObject($manager); |
71
|
|
|
$p = $r->getProperty('cache'); |
72
|
|
|
$p->setAccessible(true); |
73
|
|
|
/** @var ViewCache $cache */ |
74
|
|
|
$cache = $p->getValue($manager); |
75
|
|
|
|
76
|
|
|
$manager->addDependency(new ValueDependency('name', 'hello')); |
77
|
|
|
$manager->render('other:var', ['value' => 'hello']); |
78
|
|
|
$this->assertTrue($cache->has($manager->getContext(), 'other:var')); |
79
|
|
|
|
80
|
|
|
$manager->compile('other:var'); |
81
|
|
|
$this->assertFalse($cache->has($manager->getContext(), 'other:var')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testReset(): void |
85
|
|
|
{ |
86
|
|
|
$manager = $this->makeManager(); |
87
|
|
|
|
88
|
|
|
$r = new \ReflectionObject($manager); |
89
|
|
|
$p = $r->getProperty('cache'); |
90
|
|
|
$p->setAccessible(true); |
91
|
|
|
/** @var ViewCache $cache */ |
92
|
|
|
$cache = $p->getValue($manager); |
93
|
|
|
|
94
|
|
|
$manager->addDependency(new ValueDependency('name', 'hello')); |
95
|
|
|
$manager->render('other:var', ['value' => 'hello']); |
96
|
|
|
|
97
|
|
|
$this->assertTrue($cache->has($manager->getContext(), 'other:var')); |
98
|
|
|
$manager->reset('other:var'); |
99
|
|
|
|
100
|
|
|
$this->assertFalse($cache->has($manager->getContext(), 'other:var')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testEngines(): void |
104
|
|
|
{ |
105
|
|
|
$manager = $this->makeManager(); |
106
|
|
|
$this->assertInstanceOf(NativeEngine::class, $manager->getEngines()[0]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testNotFound(): void |
110
|
|
|
{ |
111
|
|
|
$this->expectException(ViewException::class); |
112
|
|
|
|
113
|
|
|
$manager = $this->makeManager(); |
114
|
|
|
$manager->render('hell-world'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function makeManager(array $config = []): ViewManager |
118
|
|
|
{ |
119
|
|
|
return new ViewManager( |
120
|
|
|
new ViewsConfig([ |
121
|
|
|
'cache' => [ |
122
|
|
|
'enable' => true, |
123
|
|
|
'memory' => true, |
124
|
|
|
'directory' => '/tmp', |
125
|
|
|
], |
126
|
|
|
'namespaces' => [ |
127
|
|
|
'default' => __DIR__ . '/fixtures/default', |
128
|
|
|
'other' => __DIR__ . '/fixtures/other', |
129
|
|
|
], |
130
|
|
|
'dependencies' => [ |
131
|
|
|
|
132
|
|
|
], |
133
|
|
|
'engines' => [ |
134
|
|
|
NativeEngine::class, |
135
|
|
|
], |
136
|
|
|
] + $config), |
137
|
|
|
$this->container |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|