@@ -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,22 @@ 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 () { return 'O, Hai! ' ; },
120+ ];
121+ foreach ($ negatives as $ callable ) {
122+ $ result = self ::callProtectedMethod ($ this ->manager , 'isWildCallable ' , [$ callable ]);
123+ $ this ->assertFalse ($ result );
124+ }
118125
119126 /*
120127 * String
121128 */
122129 $ callable = 'something_* ' ;
123130 $ result = self ::callProtectedMethod ($ this ->manager , 'isWildCallable ' , [$ callable ]);
124131 $ this ->assertTrue ($ result );
125-
126132 $ result = self ::callProtectedMethod ($ this ->manager , 'isWildCallable ' , [$ callable , 'delicious ' ]);
127133 $ this ->assertEquals ('something_delicious ' , $ result );
128134
@@ -163,20 +169,19 @@ public function testIsWildCallable()
163169 public function testMakeTwigFunctionsHandlesCallableArrayWithOptions ()
164170 {
165171 $ functions = $ this ->registerAndGetTwigFunctions ($ this ->testCallableDataWithOptions );
166- $ this ->assertAllTwigFunctions ($ functions );
172+ $ this ->assertAllTwigInstances ($ functions, TwigFunction::class );
167173 }
168174
169175 public function testMakeTwigFiltersHandlesCallableArrayWithOptions ()
170176 {
171177 $ filters = $ this ->registerAndGetTwigFilters ($ this ->testCallableDataWithOptions );
172- $ this ->assertAllTwigFilters ($ filters );
178+ $ this ->assertAllTwigInstances ($ filters, TwigFilter::class );
173179 }
174180
175181 public function testMakeTwigFunctionsAppliesDefaultOptions ()
176182 {
177183 $ functions = $ this ->registerAndGetTwigFunctions ($ this ->testCallableData );
178- $ this ->assertAllTwigFunctions ($ functions );
179-
184+ $ this ->assertAllTwigInstances ($ functions , TwigFunction::class);
180185 foreach ($ functions as $ function ) {
181186 $ options = self ::getProtectedProperty ($ function , 'options ' );
182187 $ this ->assertArrayHasKey ('is_safe ' , $ options );
@@ -187,8 +192,7 @@ public function testMakeTwigFunctionsAppliesDefaultOptions()
187192 public function testMakeTwigFiltersAppliesDefaultOptions ()
188193 {
189194 $ filters = $ this ->registerAndGetTwigFilters ($ this ->testCallableData );
190- $ this ->assertAllTwigFilters ($ filters );
191-
195+ $ this ->assertAllTwigInstances ($ filters , TwigFilter::class);
192196 foreach ($ filters as $ filter ) {
193197 $ options = self ::getProtectedProperty ($ filter , 'options ' );
194198 $ this ->assertArrayHasKey ('is_safe ' , $ options );
@@ -198,53 +202,21 @@ public function testMakeTwigFiltersAppliesDefaultOptions()
198202
199203 public function testMakeTwigFunctionsHandlesEmptyCallableArray ()
200204 {
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 ' , []);
205+ $ this ->expectExceptionForEmptyCallable (TwigFunction::class);
209206 }
210207
211208 public function testMakeTwigFiltersHandlesEmptyCallableArray ()
212209 {
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 ' , []);
210+ $ this ->expectExceptionForEmptyCallable (TwigFilter::class);
221211 }
222212
223213 public function testMakeTwigFunctionsHandlesInvalidCallable ()
224214 {
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 ' , []);
215+ $ this ->expectExceptionForInvalidCallable (TwigFunction::class);
235216 }
236217
237218 public function testMakeTwigFiltersHandlesInvalidCallable ()
238219 {
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 ' , []);
220+ $ this ->expectExceptionForInvalidCallable (TwigFilter::class);
249221 }
250222}
0 commit comments