|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Template\Loader; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Exception\Config; |
|
8
|
|
|
use AbterPhp\Framework\Template\IBuilder; |
|
9
|
|
|
use AbterPhp\Framework\Template\IData; |
|
10
|
|
|
use AbterPhp\Framework\Template\ParsedTemplate; |
|
11
|
|
|
use AbterPhp\Website\Databases\Queries\PageCategoryCache; |
|
12
|
|
|
use AbterPhp\Website\Domain\Entities\Page; |
|
13
|
|
|
use AbterPhp\Website\Domain\Entities\PageCategory as PageCategoryEntity; |
|
14
|
|
|
use AbterPhp\Website\Orm\PageRepo; |
|
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
class PageCategoryTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var PageCategory - System Under Test */ |
|
21
|
|
|
protected $sut; |
|
22
|
|
|
|
|
23
|
|
|
/** @var PageRepo|MockObject */ |
|
24
|
|
|
protected $pageRepoMock; |
|
25
|
|
|
|
|
26
|
|
|
/** @var PageCategoryCache|MockObject */ |
|
27
|
|
|
protected $cacheMock; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp(): void |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
|
|
33
|
|
|
$this->pageRepoMock = $this->createMock(PageRepo::class); |
|
34
|
|
|
$this->cacheMock = $this->createMock(PageCategoryCache::class); |
|
35
|
|
|
|
|
36
|
|
|
$this->sut = new PageCategory($this->pageRepoMock, $this->cacheMock, []); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testLoadZero() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->pageRepoMock |
|
42
|
|
|
->expects($this->any()) |
|
43
|
|
|
->method('getByCategoryIdentifiers') |
|
44
|
|
|
->willReturn([]); |
|
45
|
|
|
|
|
46
|
|
|
$parsedTemplates = []; |
|
47
|
|
|
|
|
48
|
|
|
$actualResult = $this->sut->load($parsedTemplates); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals([], $actualResult); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testLoadOneWithMatchingBuilder() |
|
54
|
|
|
{ |
|
55
|
|
|
$identifier = 'pc-1'; |
|
56
|
|
|
|
|
57
|
|
|
$category = new PageCategoryEntity('', 'PC #1', $identifier); |
|
58
|
|
|
$page = new Page('', $identifier, '', '', '', '', false, $category); |
|
59
|
|
|
|
|
60
|
|
|
$this->pageRepoMock |
|
61
|
|
|
->expects($this->any()) |
|
62
|
|
|
->method('getByCategoryIdentifiers') |
|
63
|
|
|
->willReturn([$page]); |
|
64
|
|
|
|
|
65
|
|
|
$dataStub = $this->createMock(IData::class); |
|
66
|
|
|
|
|
67
|
|
|
/** @var IBuilder|MockObject $builderMock */ |
|
68
|
|
|
$builderMock = $this->createMock(IBuilder::class); |
|
69
|
|
|
$builderMock->expects($this->once())->method('build')->willReturn($dataStub); |
|
70
|
|
|
|
|
71
|
|
|
$this->sut->addBuilder('detailed', $builderMock); |
|
72
|
|
|
|
|
73
|
|
|
$parsedTemplates = [ |
|
74
|
|
|
$identifier => [ |
|
75
|
|
|
new ParsedTemplate('pagecategory', $identifier, ['builder' => 'detailed']), |
|
76
|
|
|
], |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$actualResult = $this->sut->load($parsedTemplates); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals([$dataStub], $actualResult); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testLoadMultipleWithMatchingBuilders() |
|
85
|
|
|
{ |
|
86
|
|
|
$identifier0 = 'pc-1'; |
|
87
|
|
|
$identifier1 = 'pc-2'; |
|
88
|
|
|
|
|
89
|
|
|
$category0 = new PageCategoryEntity('', 'PC #0', $identifier0); |
|
90
|
|
|
$page00 = new Page('', $identifier0, '', '', '', '', false, $category0); |
|
91
|
|
|
$page01 = new Page('', $identifier0, '', '', '', '', false, $category0); |
|
92
|
|
|
$category1 = new PageCategoryEntity('', 'PC #1', $identifier1); |
|
93
|
|
|
$page10 = new Page('', $identifier1, '', '', '', '', false, $category1); |
|
94
|
|
|
|
|
95
|
|
|
$this->pageRepoMock |
|
96
|
|
|
->expects($this->any()) |
|
97
|
|
|
->method('getByCategoryIdentifiers') |
|
98
|
|
|
->willReturn([$page00, $page01, $page10]); |
|
99
|
|
|
|
|
100
|
|
|
$dataStub = $this->createMock(IData::class); |
|
101
|
|
|
|
|
102
|
|
|
/** @var IBuilder|MockObject $builderMock */ |
|
103
|
|
|
$builderMock0 = $this->createMock(IBuilder::class); |
|
104
|
|
|
$builderMock0->expects($this->exactly(2))->method('build')->willReturn($dataStub); |
|
105
|
|
|
$builderMock1 = $this->createMock(IBuilder::class); |
|
106
|
|
|
$builderMock1->expects($this->once())->method('build')->willReturn($dataStub); |
|
107
|
|
|
|
|
108
|
|
|
$this->sut->addBuilder('detailed', $builderMock0)->addBuilder('simple', $builderMock1); |
|
109
|
|
|
|
|
110
|
|
|
$parsedTemplates = [ |
|
111
|
|
|
$identifier0 => [ |
|
112
|
|
|
new ParsedTemplate('pagecategory', $identifier0, ['builder' => 'detailed']), |
|
113
|
|
|
new ParsedTemplate('pagecategory', $identifier1, ['builder' => 'simple']), |
|
114
|
|
|
], |
|
115
|
|
|
$identifier1 => [ |
|
116
|
|
|
new ParsedTemplate('pagecategory', $identifier1, ['builder' => 'detailed']), |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
$actualResult = $this->sut->load($parsedTemplates); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertEquals([$dataStub, $dataStub, $dataStub], $actualResult); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testLoadThrowsExceptionOnInvalidLoader() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->expectException(Config::class); |
|
128
|
|
|
|
|
129
|
|
|
$identifier = 'pc-1'; |
|
130
|
|
|
|
|
131
|
|
|
$category = new PageCategoryEntity('', 'PC #1', $identifier); |
|
132
|
|
|
$page = new Page('', $identifier, '', '', '', '', false, $category); |
|
133
|
|
|
|
|
134
|
|
|
$this->pageRepoMock |
|
135
|
|
|
->expects($this->any()) |
|
136
|
|
|
->method('getByCategoryIdentifiers') |
|
137
|
|
|
->willReturn([$page]); |
|
138
|
|
|
|
|
139
|
|
|
$parsedTemplates = [ |
|
140
|
|
|
$identifier => [new ParsedTemplate('pagecategory', $identifier, ['builder' => 'nope'])], |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
|
|
$this->sut->load($parsedTemplates); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testLoadZeroPageForBuilder() |
|
147
|
|
|
{ |
|
148
|
|
|
$identifier = 'pc-1'; |
|
149
|
|
|
|
|
150
|
|
|
$this->pageRepoMock |
|
151
|
|
|
->expects($this->any()) |
|
152
|
|
|
->method('getByCategoryIdentifiers') |
|
153
|
|
|
->willReturn([]); |
|
154
|
|
|
|
|
155
|
|
|
/** @var IBuilder|MockObject $builderMock */ |
|
156
|
|
|
$builderMock = $this->createMock(IBuilder::class); |
|
157
|
|
|
$builderMock->expects($this->never())->method('build'); |
|
158
|
|
|
|
|
159
|
|
|
$this->sut->addBuilder('detailed', $builderMock); |
|
160
|
|
|
|
|
161
|
|
|
$parsedTemplates = [ |
|
162
|
|
|
$identifier => [new ParsedTemplate('pagecategory', $identifier, ['builder' => 'detailed'])], |
|
163
|
|
|
]; |
|
164
|
|
|
|
|
165
|
|
|
$actualResult = $this->sut->load($parsedTemplates); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertEquals([], $actualResult); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testHasAnyChangedSinceCallsBlockCache() |
|
171
|
|
|
{ |
|
172
|
|
|
$identifiers = ['foo', 'bar', 'baz']; |
|
173
|
|
|
$cacheTime = 'foo'; |
|
174
|
|
|
$expectedResult = true; |
|
175
|
|
|
|
|
176
|
|
|
$this->cacheMock |
|
177
|
|
|
->expects($this->once()) |
|
178
|
|
|
->method('hasAnyChangedSince') |
|
179
|
|
|
->with($identifiers, $cacheTime) |
|
180
|
|
|
->willReturn($expectedResult); |
|
181
|
|
|
|
|
182
|
|
|
$actualResult = $this->sut->hasAnyChangedSince($identifiers, $cacheTime); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|