14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Bridge \Twig \Extension \CodeExtension ;
16
16
use Symfony \Component \HttpKernel \Debug \FileLinkFormatter ;
17
+ use Twig \Environment ;
18
+ use Twig \Loader \ArrayLoader ;
17
19
18
20
class CodeExtensionTest extends TestCase
19
21
{
@@ -28,42 +30,136 @@ public function testFileRelative()
28
30
$ this ->assertEquals ('file.txt ' , $ this ->getExtension ()->getFileRelative (\DIRECTORY_SEPARATOR .'project ' .\DIRECTORY_SEPARATOR .'file.txt ' ));
29
31
}
30
32
31
- /**
32
- * @dataProvider getClassNameProvider
33
- */
34
- public function testGettingClassAbbreviation ($ class , $ abbr )
33
+ public function testClassAbbreviationIntegration ()
35
34
{
36
- $ this ->assertEquals ($ this ->getExtension ()->abbrClass ($ class ), $ abbr );
35
+ $ data = [
36
+ 'fqcn ' => 'F\Q\N\Foo ' ,
37
+ 'xss ' => '<script> ' ,
38
+ ];
39
+
40
+ $ template = <<<'TWIG'
41
+ {{ 'Bare'|abbr_class }}
42
+ {{ fqcn|abbr_class }}
43
+ {{ xss|abbr_class }}
44
+ TWIG;
45
+
46
+ $ expected = <<<'HTML'
47
+ <abbr title="Bare">Bare</abbr>
48
+ <abbr title="F\Q\N\Foo">Foo</abbr>
49
+ <abbr title="<script>"><script></abbr>
50
+ HTML;
51
+
52
+ $ this ->assertEquals ($ expected , $ this ->render ($ template , $ data ));
37
53
}
38
54
39
- /**
40
- * @dataProvider getMethodNameProvider
41
- */
42
- public function testGettingMethodAbbreviation ($ method , $ abbr )
55
+ public function testMethodAbbreviationIntegration ()
43
56
{
44
- $ this ->assertEquals ($ this ->getExtension ()->abbrMethod ($ method ), $ abbr );
57
+ $ data = [
58
+ 'fqcn ' => 'F\Q\N\Foo::Method ' ,
59
+ 'xss ' => '<script> ' ,
60
+ ];
61
+
62
+ $ template = <<<'TWIG'
63
+ {{ 'Bare::Method'|abbr_method }}
64
+ {{ fqcn|abbr_method }}
65
+ {{ 'Closure'|abbr_method }}
66
+ {{ 'Method'|abbr_method }}
67
+ {{ xss|abbr_method }}
68
+ TWIG;
69
+
70
+ $ expected = <<<'HTML'
71
+ <abbr title="Bare">Bare</abbr>::Method()
72
+ <abbr title="F\Q\N\Foo">Foo</abbr>::Method()
73
+ <abbr title="Closure">Closure</abbr>
74
+ <abbr title="Method">Method</abbr>()
75
+ <abbr title="<script>"><script></abbr>()
76
+ HTML;
77
+
78
+ $ this ->assertEquals ($ expected , $ this ->render ($ template , $ data ));
45
79
}
46
80
47
- public static function getClassNameProvider (): array
81
+ public function testFormatArgsIntegration ()
48
82
{
49
- return [
50
- ['F\Q\N\Foo ' , '<abbr title="F\Q\N\Foo">Foo</abbr> ' ],
51
- ['Bare ' , '<abbr title="Bare">Bare</abbr> ' ],
83
+ $ data = [
84
+ 'args ' => [
85
+ ['object ' , 'Foo ' ],
86
+ ['array ' , [['string ' , 'foo ' ], ['null ' ]]],
87
+ ['resource ' ],
88
+ ['string ' , 'bar ' ],
89
+ ['int ' , 123 ],
90
+ ['bool ' , true ],
91
+ ],
92
+ 'xss ' => [
93
+ ['object ' , '<Foo> ' ],
94
+ ['array ' , [['string ' , '<foo> ' ]]],
95
+ ['string ' , '<bar> ' ],
96
+ ['int ' , 123 ],
97
+ ['bool ' , true ],
98
+ ['<xss> ' , '<script> ' ],
99
+ ],
52
100
];
101
+
102
+ $ template = <<<'TWIG'
103
+ {{ args|format_args }}
104
+ {{ xss|format_args }}
105
+ {{ args|format_args_as_text }}
106
+ {{ xss|format_args_as_text }}
107
+ TWIG;
108
+
109
+ $ expected = <<<'HTML'
110
+ <em>object</em>(<abbr title="Foo">Foo</abbr>), <em>array</em>('foo', <em>null</em>), <em>resource</em>, 'bar', 123, true
111
+ <em>object</em>(<abbr title="<Foo>"><Foo></abbr>), <em>array</em>('<foo>'), '<bar>', 123, true, '<script>'
112
+ object(Foo), array('foo', null), resource, 'bar', 123, true
113
+ object(&lt;Foo&gt;), array('&lt;foo&gt;'), '&lt;bar&gt;', 123, true, '&lt;script&gt;'
114
+ HTML;
115
+
116
+ $ this ->assertEquals ($ expected , $ this ->render ($ template , $ data ));
117
+ }
118
+
119
+
120
+ public function testFormatFileIntegration ()
121
+ {
122
+ $ template = <<<'TWIG'
123
+ {{ 'foo/bar/baz.php'|format_file(21) }}
124
+ TWIG;
125
+
126
+ $ expected = <<<'HTML'
127
+ <a href="proto://foo/bar/baz.php#&line=21" title="Click to open this file" class="file_link">foo/bar/baz.php at line 21</a>
128
+ HTML;
129
+
130
+ $ this ->assertEquals ($ expected , $ this ->render ($ template ));
53
131
}
54
132
55
- public static function getMethodNameProvider (): array
133
+ public function testFormatFileFromTextIntegration ()
56
134
{
57
- return [
58
- ['F\Q\N\Foo::Method ' , '<abbr title="F\Q\N\Foo">Foo</abbr>::Method() ' ],
59
- ['Bare::Method ' , '<abbr title="Bare">Bare</abbr>::Method() ' ],
60
- ['Closure ' , '<abbr title="Closure">Closure</abbr> ' ],
61
- ['Method ' , '<abbr title="Method">Method</abbr>() ' ],
62
- ];
135
+ $ template = <<<'TWIG'
136
+ {{ 'in "foo/bar/baz.php" at line 21'|format_file_from_text }}
137
+ {{ 'in "foo/bar/baz.php" on line 21'|format_file_from_text }}
138
+ {{ 'in "<script>" on line 21'|format_file_from_text }}
139
+ TWIG;
140
+
141
+ $ expected = <<<'HTML'
142
+ in <a href="proto://foo/bar/baz.php#&line=21" title="Click to open this file" class="file_link">foo/bar/baz.php at line 21</a>
143
+ in <a href="proto://foo/bar/baz.php#&line=21" title="Click to open this file" class="file_link">foo/bar/baz.php at line 21</a>
144
+ in <a href="proto://<script>#&line=21" title="Click to open this file" class="file_link"><script> at line 21</a>
145
+ HTML;
146
+
147
+ $ this ->assertEquals ($ expected , $ this ->render ($ template ));
63
148
}
64
149
65
150
protected function getExtension (): CodeExtension
66
151
{
67
152
return new CodeExtension (new FileLinkFormatter ('proto://%f#&line=%l& ' .substr (__FILE__ , 0 , 5 ).'>foobar ' ), \DIRECTORY_SEPARATOR .'project ' , 'UTF-8 ' );
68
153
}
154
+
155
+ private function render (string $ template , array $ context = [])
156
+ {
157
+ $ twig = new Environment (
158
+ new ArrayLoader (['index ' => $ template ]),
159
+ ['debug ' => true ]
160
+ );
161
+ $ twig ->addExtension ($ this ->getExtension ());
162
+
163
+ return $ twig ->render ('index ' , $ context );
164
+ }
69
165
}
0 commit comments