|
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\Context\ValueDependency; |
|
16
|
|
|
use Spiral\Views\Exception\ContextException; |
|
17
|
|
|
use Spiral\Views\Processor\ContextProcessor; |
|
18
|
|
|
use Spiral\Views\Traits\ProcessorTrait; |
|
19
|
|
|
use Spiral\Views\ViewContext; |
|
20
|
|
|
use Spiral\Views\ViewLoader; |
|
21
|
|
|
use Spiral\Views\ViewSource; |
|
22
|
|
|
|
|
23
|
|
|
class ContextProcessorTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
use ProcessorTrait; |
|
26
|
|
|
|
|
27
|
|
|
public function testProcessContext(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->processors[] = new ContextProcessor(); |
|
30
|
|
|
|
|
31
|
|
|
$source = $this->getSource('other:inject'); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertSame('hello @{name|default}', $source->getCode()); |
|
34
|
|
|
|
|
35
|
|
|
$ctx = new ViewContext(); |
|
36
|
|
|
$source2 = $this->process($source, $ctx->withDependency(new ValueDependency('name', 'Bobby'))); |
|
37
|
|
|
$this->assertSame('hello Bobby', $source2->getCode()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testProcessContextException(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->expectException(ContextException::class); |
|
43
|
|
|
|
|
44
|
|
|
$this->processors[] = new ContextProcessor(); |
|
45
|
|
|
|
|
46
|
|
|
$source = $this->getSource('other:inject'); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertSame('hello @{name|default}', $source->getCode()); |
|
49
|
|
|
|
|
50
|
|
|
$ctx = new ViewContext(); |
|
51
|
|
|
$this->process($source, $ctx); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getSource(string $path): ViewSource |
|
55
|
|
|
{ |
|
56
|
|
|
$loader = new ViewLoader([ |
|
57
|
|
|
'default' => __DIR__ . '/fixtures/default', |
|
58
|
|
|
'other' => __DIR__ . '/fixtures/other', |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
return $loader->withExtension('php')->load($path); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|