| 1 | <?php |
||
| 8 | trait TestHelper |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Get the tested extension |
||
| 12 | * |
||
| 13 | * @return \Twig_Extension |
||
| 14 | */ |
||
| 15 | abstract protected function getExtension(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Build the Twig environment for the template |
||
| 19 | * |
||
| 20 | * @param string $template |
||
| 21 | * @return \Twig_Environment |
||
| 22 | */ |
||
| 23 | protected function buildEnv($template) |
||
| 24 | { |
||
| 25 | $loader = new \Twig_Loader_Array([ |
||
| 26 | 'template' => $template, |
||
| 27 | ]); |
||
| 28 | $twig = new \Twig_Environment($loader); |
||
| 29 | |||
| 30 | $twig->addExtension($this->getExtension()); |
||
| 31 | |||
| 32 | return $twig; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Render template |
||
| 37 | * |
||
| 38 | * @param string $template |
||
| 39 | * @param array $data |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | protected function render($template, array $data = []) |
||
| 43 | { |
||
| 44 | $twig = $this->buildEnv($template); |
||
| 45 | $result = $twig->render('template', $data); |
||
| 46 | |||
| 47 | return $result; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Render template and assert equals |
||
| 52 | * |
||
| 53 | * @param string $expected |
||
| 54 | * @param string $template |
||
| 55 | * @param array $data |
||
| 56 | */ |
||
| 57 | protected function assertRender($expected, $template, array $data = []) |
||
| 58 | { |
||
| 59 | $result = $this->render($template, $data); |
||
| 60 | |||
| 61 | $this->assertEquals($expected, (string)$result); |
||
| 62 | } |
||
| 63 | } |
||
| 64 |