|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Template; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class EngineTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Engine - System Under Test */ |
|
13
|
|
|
protected Engine $sut; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Renderer|MockObject */ |
|
16
|
|
|
protected $rendererMock; |
|
17
|
|
|
|
|
18
|
|
|
/** @var CacheManager|MockObject */ |
|
19
|
|
|
protected $cacheManagerMock; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
$this->rendererMock = $this->createMock(Renderer::class); |
|
26
|
|
|
|
|
27
|
|
|
$this->cacheManagerMock = $this->createMock(CacheManager::class); |
|
28
|
|
|
|
|
29
|
|
|
$this->sut = new Engine($this->rendererMock, $this->cacheManagerMock, true); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array[] |
|
34
|
|
|
*/ |
|
35
|
|
|
public function runProvider(): array |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'empty' => [[], [], [], ''], |
|
39
|
|
|
'rendered-is-returned' => [['foo' => ''], [], ['rendered'], 'rendered'], |
|
40
|
|
|
'last-rendered-is-returned' => [['foo' => '', 'bar' => ''], [], ['baz', 'rendered'], 'rendered'], |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @dataProvider runProvider |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $templates |
|
48
|
|
|
* @param array $vars |
|
49
|
|
|
* @param array $renderStubs |
|
50
|
|
|
* @param string $expectedResult |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testRun(array $templates, array $vars, array $renderStubs, string $expectedResult): void |
|
53
|
|
|
{ |
|
54
|
|
|
$type = 'foo'; |
|
55
|
|
|
$documentId = 'foo0'; |
|
56
|
|
|
|
|
57
|
|
|
$this->cacheManagerMock->expects($this->any())->method('storeCacheData')->willReturn(true); |
|
58
|
|
|
$this->cacheManagerMock->expects($this->any())->method('storeDocument')->willReturn(true); |
|
59
|
|
|
|
|
60
|
|
|
$this->rendererMock |
|
61
|
|
|
->expects($this->exactly(count($renderStubs))) |
|
62
|
|
|
->method('render') |
|
63
|
|
|
->willReturnOnConsecutiveCalls(...$renderStubs); |
|
64
|
|
|
|
|
65
|
|
|
$actualResult = $this->sut->run($type, $documentId, $templates, $vars); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testRunThrowsExceptionIfDocumentStorageFails(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(Exception::class); |
|
73
|
|
|
|
|
74
|
|
|
$templates = []; |
|
75
|
|
|
$vars = []; |
|
76
|
|
|
|
|
77
|
|
|
$type = 'foo'; |
|
78
|
|
|
$documentId = 'foo0'; |
|
79
|
|
|
|
|
80
|
|
|
$this->cacheManagerMock->expects($this->any())->method('storeCacheData')->willReturn(false); |
|
81
|
|
|
$this->cacheManagerMock->expects($this->never())->method('storeDocument'); |
|
82
|
|
|
|
|
83
|
|
|
$this->sut->run($type, $documentId, $templates, $vars); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testRunThrowsExceptionIfCacheDataStorageFails(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->expectException(Exception::class); |
|
89
|
|
|
|
|
90
|
|
|
$templates = []; |
|
91
|
|
|
$vars = []; |
|
92
|
|
|
|
|
93
|
|
|
$type = 'foo'; |
|
94
|
|
|
$documentId = 'foo0'; |
|
95
|
|
|
|
|
96
|
|
|
$this->cacheManagerMock->expects($this->any())->method('storeCacheData')->willReturn(true); |
|
97
|
|
|
$this->cacheManagerMock->expects($this->any())->method('storeDocument')->willReturn(false); |
|
98
|
|
|
|
|
99
|
|
|
$this->sut->run($type, $documentId, $templates, $vars); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testRunWithValidCache(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$expectedResult = 'bar'; |
|
105
|
|
|
|
|
106
|
|
|
$type = 'foo'; |
|
107
|
|
|
$documentId = 'foo0'; |
|
108
|
|
|
$templates = []; |
|
109
|
|
|
$vars = []; |
|
110
|
|
|
|
|
111
|
|
|
$this->cacheManagerMock->expects($this->any())->method('getCacheData')->willReturn(new CacheData()); |
|
112
|
|
|
$this->rendererMock->expects($this->any())->method('hasAllValidLoaders')->willReturn(true); |
|
113
|
|
|
$this->cacheManagerMock->expects($this->once())->method('getDocument')->willReturn($expectedResult); |
|
114
|
|
|
$this->cacheManagerMock->expects($this->never())->method('storeCacheData'); |
|
115
|
|
|
|
|
116
|
|
|
$this->sut->run($type, $documentId, $templates, $vars); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testRunWithoutValidCache(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$sut = new Engine($this->rendererMock, $this->cacheManagerMock, false); |
|
122
|
|
|
|
|
123
|
|
|
$type = 'foo'; |
|
124
|
|
|
$documentId = 'foo0'; |
|
125
|
|
|
$templates = []; |
|
126
|
|
|
$vars = []; |
|
127
|
|
|
|
|
128
|
|
|
$this->cacheManagerMock->expects($this->never())->method('getCacheData'); |
|
129
|
|
|
$this->rendererMock->expects($this->any())->method('hasAllValidLoaders')->willReturn(true); |
|
130
|
|
|
$this->cacheManagerMock->expects($this->never())->method('getDocument'); |
|
131
|
|
|
$this->cacheManagerMock->expects($this->never())->method('storeCacheData'); |
|
132
|
|
|
|
|
133
|
|
|
$sut->run($type, $documentId, $templates, $vars); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testGetRendererReturnsRenderer(): void |
|
137
|
|
|
{ |
|
138
|
|
|
$actualResult = $this->sut->getRenderer(); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertInstanceOf(Renderer::class, $actualResult); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|