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\Views\Exception\LoaderException; |
16
|
|
|
use Spiral\Views\Exception\PathException; |
17
|
|
|
use Spiral\Views\Loader\PathParser; |
18
|
|
|
use Spiral\Views\ViewLoader; |
19
|
|
|
|
20
|
|
|
class LoaderTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testExistsException(): void |
23
|
|
|
{ |
24
|
|
|
$this->expectException(LoaderException::class); |
25
|
|
|
|
26
|
|
|
$loader = new ViewLoader([ |
27
|
|
|
'default' => __DIR__ . '/fixtures/default', |
28
|
|
|
]); |
29
|
|
|
|
30
|
|
|
$loader->exists('view'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testListException(): void |
34
|
|
|
{ |
35
|
|
|
$this->expectException(LoaderException::class); |
36
|
|
|
|
37
|
|
|
$loader = new ViewLoader([ |
38
|
|
|
'default' => __DIR__ . '/fixtures/default', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$loader->list(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testLoadException(): void |
45
|
|
|
{ |
46
|
|
|
$this->expectException(LoaderException::class); |
47
|
|
|
|
48
|
|
|
$loader = new ViewLoader([ |
49
|
|
|
'default' => __DIR__ . '/fixtures/default', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$loader->load('view'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testExists(): void |
56
|
|
|
{ |
57
|
|
|
$loader = new ViewLoader([ |
58
|
|
|
'default' => __DIR__ . '/fixtures/default', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$loader = $loader->withExtension('php'); |
62
|
|
|
|
63
|
|
|
$this->assertFalse($loader->exists('another')); |
64
|
|
|
$this->assertFalse($loader->exists('inner/file.twig')); |
65
|
|
|
$this->assertFalse($loader->exists('inner/file')); |
66
|
|
|
|
67
|
|
|
$this->assertTrue($loader->exists('view')); |
68
|
|
|
$this->assertTrue($loader->exists('inner/view')); |
69
|
|
|
$this->assertTrue($loader->exists('inner/partial/view')); |
70
|
|
|
|
71
|
|
|
$this->assertTrue($loader->exists('view.php')); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($loader->exists('default:view')); |
74
|
|
|
$this->assertTrue($loader->exists('default:view.php')); |
75
|
|
|
|
76
|
|
|
$this->assertTrue($loader->exists('@default/view')); |
77
|
|
|
$this->assertTrue($loader->exists('@default/view.php')); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($loader->exists('default:inner/partial/view')); |
80
|
|
|
$this->assertTrue($loader->exists('default:inner/partial/view.php')); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($loader->exists('@default/inner/partial/view')); |
83
|
|
|
$this->assertTrue($loader->exists('@default/inner/partial/view.php')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testList(): void |
87
|
|
|
{ |
88
|
|
|
$loader = new ViewLoader([ |
89
|
|
|
'default' => __DIR__ . '/fixtures/default', |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
$loader = $loader->withExtension('php'); |
93
|
|
|
$files = $loader->list(); |
94
|
|
|
|
95
|
|
|
$this->assertContains('default:view', $files); |
96
|
|
|
$this->assertContains('default:inner/view', $files); |
97
|
|
|
$this->assertContains('default:inner/partial/view', $files); |
98
|
|
|
$this->assertNotContains('default:inner/file', $files); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testLoadNotFound(): void |
102
|
|
|
{ |
103
|
|
|
$this->expectException(LoaderException::class); |
104
|
|
|
|
105
|
|
|
$loader = new ViewLoader([ |
106
|
|
|
'default' => __DIR__ . '/fixtures/default', |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
$loader = $loader->withExtension('php'); |
110
|
|
|
|
111
|
|
|
$loader->load('inner/file'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testBadPath(): void |
115
|
|
|
{ |
116
|
|
|
$this->expectException(PathException::class); |
117
|
|
|
|
118
|
|
|
$parser = new PathParser('default', 'php'); |
119
|
|
|
$parser->parse('@namespace'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testEmptyPath(): void |
123
|
|
|
{ |
124
|
|
|
$this->expectException(PathException::class); |
125
|
|
|
|
126
|
|
|
$parser = new PathParser('default', 'php'); |
127
|
|
|
$parser->parse(''); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testInvalidPath(): void |
131
|
|
|
{ |
132
|
|
|
$this->expectException(PathException::class); |
133
|
|
|
|
134
|
|
|
$parser = new PathParser('default', 'php'); |
135
|
|
|
$parser->parse("hello\0"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testExternalPath(): void |
139
|
|
|
{ |
140
|
|
|
$this->expectException(PathException::class); |
141
|
|
|
|
142
|
|
|
$parser = new PathParser('default', 'php'); |
143
|
|
|
$parser->parse('../../index.php'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testLoad(): void |
147
|
|
|
{ |
148
|
|
|
$loader = new ViewLoader([ |
149
|
|
|
'default' => __DIR__ . '/fixtures/default', |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
$loader = $loader->withExtension('php'); |
153
|
|
|
|
154
|
|
|
$source = $loader->load('inner/partial/view'); |
155
|
|
|
$this->assertNotNull($source); |
156
|
|
|
|
157
|
|
|
$this->assertSame('inner/partial/view', $source->getName()); |
158
|
|
|
$this->assertSame('default', $source->getNamespace()); |
159
|
|
|
$this->assertFileExists($source->getFilename()); |
160
|
|
|
|
161
|
|
|
$this->assertSame('hello inner partial world', $source->getCode()); |
162
|
|
|
|
163
|
|
|
$newSource = $source->withCode('new code'); |
164
|
|
|
|
165
|
|
|
$this->assertSame('new code', $newSource->getCode()); |
166
|
|
|
$this->assertSame('hello inner partial world', $source->getCode()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testMultipleNamespaces(): void |
170
|
|
|
{ |
171
|
|
|
$loader = new ViewLoader([ |
172
|
|
|
'default' => __DIR__ . '/fixtures/default', |
173
|
|
|
'other' => __DIR__ . '/fixtures/other', |
174
|
|
|
|
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$loader = $loader->withExtension('php'); |
178
|
|
|
|
179
|
|
|
$this->assertTrue($loader->exists('other:view')); |
180
|
|
|
$this->assertFalse($loader->exists('non-existed:view')); |
181
|
|
|
|
182
|
|
|
$files = $loader->list(); |
183
|
|
|
$this->assertContains('default:view', $files); |
184
|
|
|
$this->assertContains('other:view', $files); |
185
|
|
|
|
186
|
|
|
$files = $loader->list('other'); |
187
|
|
|
$this->assertCount(4, $files); |
188
|
|
|
$this->assertContains('other:view', $files); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|