1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Twig; |
4
|
|
|
|
5
|
|
|
use Jasny\Twig\ArrayExtension; |
6
|
|
|
|
7
|
|
|
class ArrayExtensionTest extends PHPUnit_Framework_TestCase { |
8
|
|
|
|
9
|
|
View Code Duplication |
private function buildEnv($template) { |
|
|
|
|
10
|
|
|
$loader = new Twig_Loader_Array(array( |
11
|
|
|
'template' => $template, |
12
|
|
|
)); |
13
|
|
|
$twig = new Twig_Environment($loader); |
14
|
|
|
$twig->addExtension(new ArrayExtension()); |
15
|
|
|
return $twig; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function process($template, $data = array()) { |
19
|
|
|
$twig = $this->buildEnv($template); |
20
|
|
|
$result = $twig->render('template', $data); |
21
|
|
|
return $result; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function check($expected, $template, $data = array()) { |
25
|
|
|
$result = $this->process($template, $data); |
26
|
|
|
$this->assertEquals($expected, $result); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testSum() { |
30
|
|
|
$data = array(1, 2, 3, 4); |
31
|
|
|
$this->check(10, '{{ data|sum() }}', array('data' => $data)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testProduct() { |
35
|
|
|
$data = array(1, 2, 3, 4); |
36
|
|
|
$this->check(24, '{{ data|product() }}', array('data' => $data)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testValues() { |
40
|
|
|
$data = array(1, 2, 3); |
41
|
|
|
$this->check('1-2-3-', '{% for v in data|values() %}{{v}}-{% endfor %}', array('data' => $data)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testHtmlAttr() { |
45
|
|
|
$data = array('href' => 'foo.html', 'class' => 'big small'); |
46
|
|
|
$this->check('href="foo.html" class="big small"', '{{ data|html_attr|raw }}', array('data' => $data)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
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.