Failed Conditions
Push — master ( 05acd6...202119 )
by Arnold
07:31
created

TestHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 56
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jasny\Twig;
4
5
/**
6
 * Helper methods for unit tests
7
 */
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