Skip to content

Commit 21c9cbd

Browse files
committed
Refactor MarkupManagerTest with helpers
1 parent 9b020c9 commit 21c9cbd

File tree

1 file changed

+49
-75
lines changed

1 file changed

+49
-75
lines changed

modules/system/tests/classes/MarkupManagerTest.php

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ public static function notCallableExceptionMessage(string $type, string $details
5959
return sprintf('The markup %s (%s) for %s is not callable.', $type, $details, $name);
6060
}
6161

62+
private function expectExceptionForEmptyCallable(string $type)
63+
{
64+
$this->expectException(SystemException::class);
65+
$this->expectExceptionMessage(self::notCallableExceptionMessage($type, '[]', 'emptyCallable'));
66+
if ($type === TwigFunction::class) {
67+
$this->manager->registerFunctions(['emptyCallable' => []]);
68+
self::callProtectedMethod($this->manager, 'makeTwigFunctions', []);
69+
} else {
70+
$this->manager->registerFilters(['emptyCallable' => []]);
71+
self::callProtectedMethod($this->manager, 'makeTwigFilters', []);
72+
}
73+
}
74+
75+
private function expectExceptionForInvalidCallable(string $type)
76+
{
77+
$this->expectException(SystemException::class);
78+
$this->expectExceptionMessage(self::notCallableExceptionMessage($type, '{"callable":"not_a_callable"}', 'invalidCallable'));
79+
if ($type === TwigFunction::class) {
80+
$this->manager->registerFunctions(['invalidCallable' => ['callable' => 'not_a_callable']]);
81+
self::callProtectedMethod($this->manager, 'makeTwigFunctions', []);
82+
} else {
83+
$this->manager->registerFilters(['invalidCallable' => ['callable' => 'not_a_callable']]);
84+
self::callProtectedMethod($this->manager, 'makeTwigFilters', []);
85+
}
86+
}
87+
6288
private function registerAndGetTwigFunctions(array $functions)
6389
{
6490
$this->manager->registerFunctions($functions);
@@ -71,26 +97,11 @@ private function registerAndGetTwigFilters(array $filters)
7197
return self::callProtectedMethod($this->manager, 'makeTwigFilters', []);
7298
}
7399

74-
private function assertAllTwigFunctions(array $functions, string $message = '')
100+
private function assertAllTwigInstances(array $items, string $class, string $message = '')
75101
{
76-
$this->assertIsArray($functions);
77-
$this->assertNotEmpty($functions);
78-
$this->assertContainsOnlyInstancesOf(
79-
TwigFunction::class,
80-
$functions,
81-
$message ?: "All elements must be TwigFunction instances"
82-
);
83-
}
84-
85-
private function assertAllTwigFilters(array $filters, string $message = '')
86-
{
87-
$this->assertIsArray($filters);
88-
$this->assertNotEmpty($filters);
89-
$this->assertContainsOnlyInstancesOf(
90-
TwigFilter::class,
91-
$filters,
92-
$message ?: "All elements must be TwigFunction instances"
93-
);
102+
$this->assertIsArray($items);
103+
$this->assertNotEmpty($items);
104+
$this->assertContainsOnlyInstancesOf($class, $items, $message ?: "All elements must be instances of $class");
94105
}
95106

96107
//
@@ -102,27 +113,24 @@ public function testIsWildCallable()
102113
/*
103114
* Negatives
104115
*/
105-
$callable = 'something';
106-
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable]);
107-
$this->assertFalse($result);
108-
109-
$callable = ['Form', 'open'];
110-
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable]);
111-
$this->assertFalse($result);
112-
113-
$callable = function () {
114-
return 'O, Hai!';
115-
};
116-
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable]);
117-
$this->assertFalse($result);
116+
$negatives = [
117+
'something',
118+
['Form', 'open'],
119+
function () {
120+
return 'O, Hai!';
121+
},
122+
];
123+
foreach ($negatives as $callable) {
124+
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable]);
125+
$this->assertFalse($result);
126+
}
118127

119128
/*
120129
* String
121130
*/
122131
$callable = 'something_*';
123132
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable]);
124133
$this->assertTrue($result);
125-
126134
$result = self::callProtectedMethod($this->manager, 'isWildCallable', [$callable, 'delicious']);
127135
$this->assertEquals('something_delicious', $result);
128136

@@ -163,20 +171,19 @@ public function testIsWildCallable()
163171
public function testMakeTwigFunctionsHandlesCallableArrayWithOptions()
164172
{
165173
$functions = $this->registerAndGetTwigFunctions($this->testCallableDataWithOptions);
166-
$this->assertAllTwigFunctions($functions);
174+
$this->assertAllTwigInstances($functions, TwigFunction::class);
167175
}
168176

169177
public function testMakeTwigFiltersHandlesCallableArrayWithOptions()
170178
{
171179
$filters = $this->registerAndGetTwigFilters($this->testCallableDataWithOptions);
172-
$this->assertAllTwigFilters($filters);
180+
$this->assertAllTwigInstances($filters, TwigFilter::class);
173181
}
174182

175183
public function testMakeTwigFunctionsAppliesDefaultOptions()
176184
{
177185
$functions = $this->registerAndGetTwigFunctions($this->testCallableData);
178-
$this->assertAllTwigFunctions($functions);
179-
186+
$this->assertAllTwigInstances($functions, TwigFunction::class);
180187
foreach ($functions as $function) {
181188
$options = self::getProtectedProperty($function, 'options');
182189
$this->assertArrayHasKey('is_safe', $options);
@@ -187,8 +194,7 @@ public function testMakeTwigFunctionsAppliesDefaultOptions()
187194
public function testMakeTwigFiltersAppliesDefaultOptions()
188195
{
189196
$filters = $this->registerAndGetTwigFilters($this->testCallableData);
190-
$this->assertAllTwigFilters($filters);
191-
197+
$this->assertAllTwigInstances($filters, TwigFilter::class);
192198
foreach ($filters as $filter) {
193199
$options = self::getProtectedProperty($filter, 'options');
194200
$this->assertArrayHasKey('is_safe', $options);
@@ -198,53 +204,21 @@ public function testMakeTwigFiltersAppliesDefaultOptions()
198204

199205
public function testMakeTwigFunctionsHandlesEmptyCallableArray()
200206
{
201-
$this->expectException(SystemException::class);
202-
$this->expectExceptionMessage(self::notCallableExceptionMessage(TwigFunction::class, '[]', 'emptyCallable'));
203-
204-
$this->manager->registerFunctions([
205-
'emptyCallable' => []
206-
]);
207-
208-
self::callProtectedMethod($this->manager, 'makeTwigFunctions', []);
207+
$this->expectExceptionForEmptyCallable(TwigFunction::class);
209208
}
210209

211210
public function testMakeTwigFiltersHandlesEmptyCallableArray()
212211
{
213-
$this->expectException(SystemException::class);
214-
$this->expectExceptionMessage(self::notCallableExceptionMessage(TwigFilter::class, '[]', 'emptyCallable'));
215-
216-
$this->manager->registerFilters([
217-
'emptyCallable' => []
218-
]);
219-
220-
self::callProtectedMethod($this->manager, 'makeTwigFilters', []);
212+
$this->expectExceptionForEmptyCallable(TwigFilter::class);
221213
}
222214

223215
public function testMakeTwigFunctionsHandlesInvalidCallable()
224216
{
225-
$this->expectException(SystemException::class);
226-
$this->expectExceptionMessage(self::notCallableExceptionMessage(TwigFunction::class, '{"callable":"not_a_callable"}', 'invalidCallable'));
227-
228-
$this->manager->registerFunctions([
229-
'invalidCallable' => [
230-
'callable' => 'not_a_callable'
231-
]
232-
]);
233-
234-
self::callProtectedMethod($this->manager, 'makeTwigFunctions', []);
217+
$this->expectExceptionForInvalidCallable(TwigFunction::class);
235218
}
236219

237220
public function testMakeTwigFiltersHandlesInvalidCallable()
238221
{
239-
$this->expectException(SystemException::class);
240-
$this->expectExceptionMessage(self::notCallableExceptionMessage(TwigFilter::class, '{"callable":"not_a_callable"}', 'invalidCallable'));
241-
242-
$this->manager->registerFilters([
243-
'invalidCallable' => [
244-
'callable' => 'not_a_callable'
245-
]
246-
]);
247-
248-
self::callProtectedMethod($this->manager, 'makeTwigFilters', []);
222+
$this->expectExceptionForInvalidCallable(TwigFilter::class);
249223
}
250224
}

0 commit comments

Comments
 (0)