1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Twig; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @covers Jasny\Twig\DateExtension |
7
|
|
|
*/ |
8
|
|
|
class DateExtensionTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
public static function setUpBeforeClass() |
11
|
|
|
{ |
12
|
|
|
date_default_timezone_set('UTC'); |
13
|
|
|
\Locale::setDefault("en_EN"); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
View Code Duplication |
protected function buildEnv($template) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$loader = new \Twig_Loader_Array(array( |
19
|
|
|
'template' => $template, |
20
|
|
|
)); |
21
|
|
|
|
22
|
|
|
$twig = new \Twig_Environment($loader); |
23
|
|
|
$twig->addExtension(new DateExtension()); |
24
|
|
|
return $twig; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function process($template, $data = array()) |
28
|
|
|
{ |
29
|
|
|
$twig = $this->buildEnv($template); |
30
|
|
|
$result = $twig->render('template', $data); |
31
|
|
|
|
32
|
|
|
return $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function localDateTimeProvider() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
['9/20/2015', "{{ '20-09-2015'|localdate() }}"], |
39
|
|
|
['September 20, 2015', "{{ '20-09-2015'|localdate('long') }}"], |
40
|
|
|
['9/20/15', "{{ '20-09-2015'|localdate('short') }}"], |
41
|
|
|
['11:14 PM', "{{ '23:14:12'|localtime() }}"], |
42
|
|
|
['11:14:12 PM GMT', "{{ '23:14:12'|localtime('long') }}"], |
43
|
|
|
['11:14 PM', "{{ '23:14:12'|localtime('short') }}"] |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider localDateTimeProvider |
49
|
|
|
* |
50
|
|
|
* @param string $expected |
51
|
|
|
* @param string $template |
52
|
|
|
*/ |
53
|
|
|
public function testLocalDateTime($expected, $template) |
54
|
|
|
{ |
55
|
|
|
$result = $this->process($template); |
56
|
|
|
|
57
|
|
|
$this->assertEquals($expected, $result); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
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.