vojtasvoboda /
oc-twigextensions-plugin
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace VojtaSvoboda\TwigExtensions\Tests; |
||
| 4 | |||
| 5 | use App; |
||
| 6 | use Carbon\Carbon; |
||
| 7 | use Config; |
||
| 8 | use PluginTestCase; |
||
| 9 | use Twig_Environment; |
||
| 10 | use VojtaSvoboda\TwigExtensions\Classes\TimeDiffTranslator; |
||
| 11 | |||
| 12 | require_once __DIR__ . '/../vendor/autoload.php'; |
||
| 13 | |||
| 14 | class PluginTest extends PluginTestCase |
||
| 15 | { |
||
| 16 | View Code Duplication | public function setUp() |
|
| 17 | { |
||
| 18 | parent::setUp(); |
||
| 19 | |||
| 20 | $this->app->setLocale('en'); |
||
| 21 | |||
| 22 | $this->app->singleton('time_diff_translator', function($app) { |
||
| 23 | $loader = $app->make('translation.loader'); |
||
| 24 | $locale = $app->config->get('app.locale'); |
||
| 25 | $translator = $app->make(TimeDiffTranslator::class, [$loader, $locale]); |
||
| 26 | $translator->setFallback($app->config->get('app.fallback_locale')); |
||
| 27 | |||
| 28 | return $translator; |
||
| 29 | }); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Return Twig environment |
||
| 34 | * |
||
| 35 | * @return Twig_Environment |
||
| 36 | */ |
||
| 37 | private function getTwig() |
||
| 38 | { |
||
| 39 | return App::make('twig.environment'); |
||
| 40 | } |
||
| 41 | |||
| 42 | View Code Duplication | public function testTemplateFromStringFunction() |
|
| 43 | { |
||
| 44 | $twig = $this->getTwig(); |
||
| 45 | |||
| 46 | $template = "{% set name = 'John' %}"; |
||
| 47 | $template .= '{{ include(template_from_string("Hello {{ name }}")) }}'; |
||
| 48 | |||
| 49 | $twigTemplate = $twig->createTemplate($template); |
||
| 50 | $this->assertEquals($twigTemplate->render([]), 'Hello John'); |
||
| 51 | } |
||
| 52 | |||
| 53 | View Code Duplication | public function testTruncateFilterForFive() |
|
| 54 | { |
||
| 55 | $twig = $this->getTwig(); |
||
| 56 | |||
| 57 | $template = "{{ 'Gordon Freeman' | truncate(5) }}"; |
||
| 58 | |||
| 59 | $twigTemplate = $twig->createTemplate($template); |
||
| 60 | $this->assertEquals($twigTemplate->render([]), 'Gordo...'); |
||
| 61 | } |
||
| 62 | |||
| 63 | View Code Duplication | public function testTruncateFilterForDefault() |
|
| 64 | { |
||
| 65 | $twig = $this->getTwig(); |
||
| 66 | |||
| 67 | $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}"; |
||
| 68 | |||
| 69 | $twigTemplate = $twig->createTemplate($template); |
||
| 70 | $this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...'); |
||
| 71 | } |
||
| 72 | |||
| 73 | View Code Duplication | public function testTruncateFilterWithSeparator() |
|
| 74 | { |
||
| 75 | $twig = $this->getTwig(); |
||
| 76 | |||
| 77 | $template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}"; |
||
| 78 | |||
| 79 | $twigTemplate = $twig->createTemplate($template); |
||
| 80 | $this->assertEquals($twigTemplate->render([]), 'Gordo-'); |
||
| 81 | } |
||
| 82 | |||
| 83 | View Code Duplication | public function testWordWrapFilter() |
|
| 84 | { |
||
| 85 | $twig = $this->getTwig(); |
||
| 86 | |||
| 87 | $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}"; |
||
| 88 | |||
| 89 | $twigTemplate = $twig->createTemplate($template); |
||
| 90 | $this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit"); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testShuffleFilter() |
||
| 94 | { |
||
| 95 | $twig = $this->getTwig(); |
||
| 96 | |||
| 97 | $template = "{{ [1, 2, 3] | shuffle }}"; |
||
| 98 | |||
| 99 | $twigTemplate = $twig->createTemplate($template); |
||
| 100 | $this->setExpectedException('Twig_Error_Runtime', 'Array to string conversion'); |
||
| 101 | $twigTemplate->render([]); |
||
| 102 | } |
||
| 103 | |||
| 104 | View Code Duplication | public function testShuffleFilterForeach() |
|
| 105 | { |
||
| 106 | $twig = $this->getTwig(); |
||
| 107 | |||
| 108 | $template = "{% for i in [1, 2, 3] | shuffle %}{{ i }}{% endfor %}"; |
||
| 109 | |||
| 110 | $twigTemplate = $twig->createTemplate($template); |
||
| 111 | $this->assertEquals(strlen($twigTemplate->render([])), 3); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function testTimeDiffFunction() |
||
| 115 | { |
||
| 116 | $twig = $this->getTwig(); |
||
| 117 | |||
| 118 | $now = Carbon::now()->subMinute(); |
||
| 119 | $template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}"; |
||
| 120 | |||
| 121 | // this test fails at TravisCI and I don't know why |
||
| 122 | $twigTemplate = $twig->createTemplate($template); |
||
| 123 | // $this->assertEquals($twigTemplate->render([]), '1 minute ago'); |
||
| 124 | } |
||
| 125 | |||
| 126 | View Code Duplication | public function testStrftimeFunction() |
|
| 127 | { |
||
| 128 | $twig = $this->getTwig(); |
||
| 129 | |||
| 130 | $template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}"; |
||
| 131 | |||
| 132 | $twigTemplate = $twig->createTemplate($template); |
||
| 133 | $this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00'); |
||
| 134 | } |
||
| 135 | |||
| 136 | View Code Duplication | public function testUppercaseFunction() |
|
| 137 | { |
||
| 138 | $twig = $this->getTwig(); |
||
| 139 | |||
| 140 | $template = "{{ 'Jack' | uppercase }}"; |
||
| 141 | |||
| 142 | $twigTemplate = $twig->createTemplate($template); |
||
| 143 | $this->assertEquals($twigTemplate->render([]), 'JACK'); |
||
| 144 | } |
||
| 145 | |||
| 146 | View Code Duplication | public function testLowercaseFunction() |
|
| 147 | { |
||
| 148 | $twig = $this->getTwig(); |
||
| 149 | |||
| 150 | $template = "{{ 'JACK' | lowercase }}"; |
||
| 151 | |||
| 152 | $twigTemplate = $twig->createTemplate($template); |
||
| 153 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
| 154 | } |
||
| 155 | |||
| 156 | View Code Duplication | public function testUcfirstFunction() |
|
| 157 | { |
||
| 158 | $twig = $this->getTwig(); |
||
| 159 | |||
| 160 | $template = "{{ 'jack' | ucfirst }}"; |
||
| 161 | |||
| 162 | $twigTemplate = $twig->createTemplate($template); |
||
| 163 | $this->assertEquals($twigTemplate->render([]), 'Jack'); |
||
| 164 | } |
||
| 165 | |||
| 166 | View Code Duplication | public function testLcfirstFunction() |
|
| 167 | { |
||
| 168 | $twig = $this->getTwig(); |
||
| 169 | |||
| 170 | $template = "{{ 'JACK' | lcfirst }}"; |
||
| 171 | |||
| 172 | $twigTemplate = $twig->createTemplate($template); |
||
| 173 | $this->assertEquals($twigTemplate->render([]), 'jACK'); |
||
| 174 | } |
||
| 175 | |||
| 176 | View Code Duplication | public function testLtrimFunction() |
|
| 177 | { |
||
| 178 | $twig = $this->getTwig(); |
||
| 179 | |||
| 180 | $template = "{{ ' jack' | ltrim }}"; |
||
| 181 | |||
| 182 | $twigTemplate = $twig->createTemplate($template); |
||
| 183 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
| 184 | } |
||
| 185 | |||
| 186 | View Code Duplication | public function testRtrimFunction() |
|
| 187 | { |
||
| 188 | $twig = $this->getTwig(); |
||
| 189 | |||
| 190 | $template = "{{ 'jack ' | rtrim }}"; |
||
| 191 | |||
| 192 | $twigTemplate = $twig->createTemplate($template); |
||
| 193 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
| 194 | } |
||
| 195 | |||
| 196 | View Code Duplication | public function testStrRepeatFunction() |
|
| 197 | { |
||
| 198 | $twig = $this->getTwig(); |
||
| 199 | |||
| 200 | $template = "{{ ' best' | str_repeat(3) }}"; |
||
| 201 | |||
| 202 | $twigTemplate = $twig->createTemplate($template); |
||
| 203 | $this->assertEquals($twigTemplate->render([]), ' best best best'); |
||
| 204 | } |
||
| 205 | |||
| 206 | View Code Duplication | public function testPluralFunction() |
|
| 207 | { |
||
| 208 | $twig = $this->getTwig(); |
||
| 209 | |||
| 210 | $template = "{{ 'mail' | plural(count) }}"; |
||
| 211 | |||
| 212 | $twigTemplate = $twig->createTemplate($template); |
||
| 213 | $this->assertEquals($twigTemplate->render([]), 'mails'); |
||
| 214 | } |
||
| 215 | |||
| 216 | View Code Duplication | public function testStrpadFunction() |
|
| 217 | { |
||
| 218 | $twig = $this->getTwig(); |
||
| 219 | |||
| 220 | $template = "{{ 'test' | strpad(10) }}"; |
||
| 221 | |||
| 222 | $twigTemplate = $twig->createTemplate($template); |
||
| 223 | $this->assertEquals($twigTemplate->render([]), ' test '); |
||
| 224 | } |
||
| 225 | |||
| 226 | View Code Duplication | public function testStripTagsFunction() |
|
| 227 | { |
||
| 228 | $twig = $this->getTwig(); |
||
| 229 | |||
| 230 | $template = "{{ '<p><b>text</b></p>' | strip_tags('<p>') }}"; |
||
| 231 | |||
| 232 | $twigTemplate = $twig->createTemplate($template); |
||
| 233 | $this->assertEquals($twigTemplate->render([]), '<p>text</p>'); |
||
| 234 | } |
||
| 235 | |||
| 236 | View Code Duplication | public function testLeftpadFunction() |
|
| 237 | { |
||
| 238 | $twig = $this->getTwig(); |
||
| 239 | |||
| 240 | $template = "{{ 'test' | leftpad(7) }}"; |
||
| 241 | |||
| 242 | $twigTemplate = $twig->createTemplate($template); |
||
| 243 | $this->assertEquals($twigTemplate->render([]), ' test'); |
||
| 244 | } |
||
| 245 | |||
| 246 | View Code Duplication | public function testRightpadFunction() |
|
| 247 | { |
||
| 248 | $twig = $this->getTwig(); |
||
| 249 | |||
| 250 | $template = "{{ 'test' | rightpad(7, 'o') }}"; |
||
| 251 | |||
| 252 | $twigTemplate = $twig->createTemplate($template); |
||
| 253 | $this->assertEquals($twigTemplate->render([]), 'testooo'); |
||
| 254 | } |
||
| 255 | |||
| 256 | View Code Duplication | public function testRtlFunction() |
|
| 257 | { |
||
| 258 | $twig = $this->getTwig(); |
||
| 259 | |||
| 260 | $template = "{{ 'Hello world!' | rtl }}"; |
||
| 261 | |||
| 262 | $twigTemplate = $twig->createTemplate($template); |
||
| 263 | $this->assertEquals($twigTemplate->render([]), '!dlrow olleH'); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function testMailtoFilter() |
||
| 267 | { |
||
| 268 | $twig = $this->getTwig(); |
||
| 269 | |||
| 270 | // same as mailto(true, true) |
||
| 271 | $template = "{{ '[email protected]' | mailto }}"; |
||
| 272 | $twigTemplate = $twig->createTemplate($template); |
||
| 273 | $this->assertNotContains('[email protected]', $twigTemplate->render([])); |
||
| 274 | $this->assertContains('mailto:', $twigTemplate->render([])); |
||
| 275 | |||
| 276 | // mailto(false, false) eg. without link and unprotected |
||
| 277 | $template = "{{ '[email protected]' | mailto(false, false) }}"; |
||
| 278 | $twigTemplate = $twig->createTemplate($template); |
||
| 279 | $this->assertContains('[email protected]', $twigTemplate->render([])); |
||
| 280 | $this->assertNotContains('mailto:', $twigTemplate->render([])); |
||
| 281 | |||
| 282 | // mailto(true, false) eg. with link but unprotected |
||
| 283 | $template = "{{ '[email protected]' | mailto(true, false) }}"; |
||
| 284 | $twigTemplate = $twig->createTemplate($template); |
||
| 285 | $this->assertContains('[email protected]', $twigTemplate->render([])); |
||
| 286 | $this->assertContains('mailto', $twigTemplate->render([])); |
||
| 287 | |||
| 288 | // mailto(false, true) eg. without link and protected |
||
| 289 | $template = "{{ '[email protected]' | mailto(false, true) }}"; |
||
| 290 | $twigTemplate = $twig->createTemplate($template); |
||
| 291 | $this->assertNotContains('[email protected]', $twigTemplate->render([])); |
||
| 292 | $this->assertNotContains('mailto', $twigTemplate->render([])); |
||
| 293 | |||
| 294 | // mailto(true, true, 'Let me know') eg. with link, protected and with non-crypted text |
||
| 295 | $template = "{{ '[email protected]' | mailto(false, true, 'Let me know') }}"; |
||
| 296 | $twigTemplate = $twig->createTemplate($template); |
||
| 297 | $this->assertNotContains('[email protected]', $twigTemplate->render([])); |
||
| 298 | $this->assertNotContains('mailto', $twigTemplate->render([])); |
||
| 299 | $this->assertContains('Let me know', $twigTemplate->render([])); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testVardumpFunction() |
||
| 303 | { |
||
| 304 | $twig = $this->getTwig(); |
||
| 305 | |||
| 306 | $template = "{{ var_dump('test') }}"; |
||
| 307 | |||
| 308 | $twigTemplate = $twig->createTemplate($template); |
||
| 309 | $this->assertContains('string(4) "test"', $twigTemplate->render([])); |
||
| 310 | } |
||
| 311 | |||
| 312 | View Code Duplication | public function testVardumpFilter() |
|
| 313 | { |
||
| 314 | $twig = $this->getTwig(); |
||
| 315 | |||
| 316 | $template = "{{ 'test' | var_dump }}"; |
||
| 317 | |||
| 318 | $twigTemplate = $twig->createTemplate($template); |
||
| 319 | $this->assertContains('string(4) "test"', $twigTemplate->render([])); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function testConfigFunction() |
||
| 323 | { |
||
| 324 | $twig = $this->getTwig(); |
||
| 325 | |||
| 326 | $key = 'app.custom.key'; |
||
| 327 | $value = 'test value'; |
||
| 328 | Config::set($key, $value); |
||
| 329 | $template = "{{ config('" . $key . "') }}"; |
||
| 330 | |||
| 331 | $twigTemplate = $twig->createTemplate($template); |
||
| 332 | $this->assertEquals($twigTemplate->render([]), $value); |
||
| 333 | } |
||
| 334 | |||
| 335 | View Code Duplication | public function testSessionFunction() |
|
| 336 | { |
||
| 337 | $twig = $this->getTwig(); |
||
| 338 | |||
| 339 | session(['my.session.key' => 'test value']); |
||
| 340 | |||
| 341 | $template = "{{ session('my.session.key') }}"; |
||
| 342 | |||
| 343 | $twigTemplate = $twig->createTemplate($template); |
||
| 344 | $this->assertEquals($twigTemplate->render([]), 'test value'); |
||
| 345 | } |
||
| 346 | |||
| 347 | View Code Duplication | public function testTransFunction() |
|
|
0 ignored issues
–
show
|
|||
| 348 | { |
||
| 349 | $twig = $this->getTwig(); |
||
| 350 | Config::set('app.locale', 'en'); |
||
| 351 | |||
| 352 | $template = "{{ trans('validation.accepted') }}"; |
||
| 353 | |||
| 354 | $twigTemplate = $twig->createTemplate($template); |
||
| 355 | $this->assertEquals($twigTemplate->render([]), 'The :attribute must be accepted.'); |
||
| 356 | } |
||
| 357 | } |
||
| 358 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.