-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPartialExpanderTest.php
156 lines (138 loc) · 5.38 KB
/
PartialExpanderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Gt\DomTemplate\Test;
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\CyclicRecursionException;
use Gt\DomTemplate\DocumentBinder;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use Gt\DomTemplate\PartialExpander;
use Gt\DomTemplate\PartialInjectionMultiplePointException;
use Gt\DomTemplate\PartialInjectionPointNotFoundException;
use Gt\DomTemplate\Test\TestFactory\DocumentTestFactory;
class PartialExpanderTest extends PartialContentTestCase {
public function testExpand_noMatchingPartial():void {
$partialContent = self::mockPartialContent(
"_partial", [
"nothing" => "this doesn't exist",
]
);
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$sut = new PartialExpander(
$document,
$partialContent
);
self::expectException(PartialContentFileNotFoundException::class);
self::assertEmpty($sut->expand());
}
public function testExpand():void {
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_PARTIAL_VIEW
]
);
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$mainElement = $document->querySelector("body>main");
self::assertNull($mainElement);
$sut = new PartialExpander(
$document,
$partialContent
);
$expandedPartials = $sut->expand();
$mainElement = $document->querySelector("body>main");
self::assertNotNull($mainElement);
self::assertCount(1, $expandedPartials);
self::assertSame("base-page", $expandedPartials[0]);
self::assertSame("My website!", $document->querySelector("body>header>h1")->textContent);
self::assertStringContainsString(
"The page content will go in here.",
$mainElement->firstChild->textContent,
"The original content of the partial element should not be removed."
);
self::assertSame("Hello from within a sub-template!", $mainElement->querySelector("h1")->textContent);
}
public function testExpand_commentVarsBound():void {
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_PARTIAL_VIEW
]
);
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$binder = self::createMock(DocumentBinder::class);
$binder->expects(self::once())
->method("bindKeyValue")
->with("title", "My website, extended...");
$sut = new PartialExpander(
$document,
$partialContent
);
$sut->expand(binder: $binder);
}
public function testExpand_noExtendsSectionOfCommentIni():void {
$document = new HTMLDocument();
$partialContent = self::createMock(PartialContent::class);
$sut = new PartialExpander($document, $partialContent);
self::assertEmpty($sut->expand());
}
public function testExpand_recursive():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE);
$partialContent = self::mockPartialContent(
"_partial", [
"extended-page" => DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE_BASE,
"partial-base" => DocumentTestFactory::HTML_PARTIAL_VIEW,
]
);
$sut = new PartialExpander($document, $partialContent);
$sut->expand();
$body = $document->body;
$main = $body->querySelector("main");
$outer = $main->querySelector(".outer");
$inner = $outer->querySelector(".inner");
self::assertStringContainsString(
"This is an inner DIV",
$inner->querySelector("p")->textContent
);
self::assertStringContainsString(
"This is the outer DIV",
$outer->querySelector("p")->textContent
);
self::assertSame("This title was set in the inner partial view.", $document->title);
}
public function testExpand_noDataPartialElement():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$partialContent = self::mockPartialContent(
"_partial", [
// Here, the HTML_COMPONENT isn't expected, because there is no data-partial element.
"base-page" => DocumentTestFactory::HTML_COMPONENT,
]
);
$sut = new PartialExpander($document, $partialContent);
self::expectException(PartialInjectionPointNotFoundException::class);
self::expectExceptionMessage("The current view extends the partial \"base-page\", but there is no element marked with `data-partial`.");
$sut->expand();
}
public function testExpand_multipleDataPartialElements():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_INCORRECT_PARTIAL_VIEW,
]
);
$sut = new PartialExpander($document, $partialContent);
self::expectException(PartialInjectionMultiplePointException::class);
self::expectExceptionMessage("The current view extends the partial \"base-page\", but there is more than one element marked with `data-partial`.");
$sut->expand();
}
public function testExpand_detectCyclicRecursion():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_CYCLIC_RECURSION);
$partialContent = self::mockPartialContent(
"_partial", [
"extended-page-1" => DocumentTestFactory::HTML_EXTENDS_PARTIAL_CYCLIC_RECURSION_1,
"extended-page-2" => DocumentTestFactory::HTML_EXTENDS_PARTIAL_CYCLIC_RECURSION_2,
"partial-base" => DocumentTestFactory::HTML_PARTIAL_VIEW,
]
);
$sut = new PartialExpander($document, $partialContent);
self::expectException(CyclicRecursionException::class);
$sut->expand();
}
}