1
|
|
|
<?php |
2
|
|
|
namespace Naneau\FileGen\Test\File; |
3
|
|
|
|
4
|
|
|
use Naneau\FileGen\Structure; |
5
|
|
|
use Naneau\FileGen\File\Contents\Twig as TwigContents; |
6
|
|
|
|
7
|
|
|
use \Twig_Loader_Filesystem as TwigFileLoader; |
8
|
|
|
use \Twig_Environment as TwigEnvironment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* testing twig |
12
|
|
|
*/ |
13
|
|
|
class TwigTest extends \Naneau\FileGen\Test\Generator\TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Simple render |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
**/ |
20
|
|
View Code Duplication |
public function testRender() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$generator = $this->createGenerator(); |
23
|
|
|
|
24
|
|
|
$structure = new Structure; |
25
|
|
|
$structure->file('foo', new TwigContents( |
|
|
|
|
26
|
|
|
$this->createTwig()->load('template_one.twig') |
27
|
|
|
)); |
28
|
|
|
|
29
|
|
|
$generator->generate($structure); |
30
|
|
|
|
31
|
|
|
// See if structure was generated |
32
|
|
|
$this->assertEquals( |
33
|
|
|
file_get_contents($generator->getRoot() . '/foo'), |
34
|
|
|
"foo bar baz\n" // Twig generates a newline at EOF... |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parameters |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
**/ |
43
|
|
View Code Duplication |
public function testRenderParameters() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$generator = $this->createGenerator(); |
46
|
|
|
|
47
|
|
|
$structure = new Structure; |
48
|
|
|
$structure->file('foo', new TwigContents( |
|
|
|
|
49
|
|
|
$this->createTwig()->load('template_two.twig'), |
50
|
|
|
array( |
51
|
|
|
'foo' => 'foo', |
52
|
|
|
'bar' => 'bar', |
53
|
|
|
'baz' => 'baz' |
54
|
|
|
) |
55
|
|
|
)); |
56
|
|
|
|
57
|
|
|
$generator->generate($structure); |
58
|
|
|
|
59
|
|
|
// See if structure was generated |
60
|
|
|
$this->assertEquals( |
61
|
|
|
file_get_contents($generator->getRoot() . '/foo'), |
62
|
|
|
"foo bar baz\n" // Twig generates a newline at EOF... |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test parameters through structure |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
**/ |
71
|
|
View Code Duplication |
public function testStructureParameters() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$structure = new Structure; |
74
|
|
|
$structure |
75
|
|
|
->file('foo', new TwigContents( |
|
|
|
|
76
|
|
|
$this->createTwig()->load('template_two.twig') |
77
|
|
|
)); |
78
|
|
|
|
79
|
|
|
$generator = $this->createGenerator(array( |
80
|
|
|
'foo' => 'foo', |
81
|
|
|
'bar' => 'bar', |
82
|
|
|
'baz' => 'baz' |
83
|
|
|
)); |
84
|
|
|
$generator->generate($structure); |
85
|
|
|
|
86
|
|
|
// See if structure was generated |
87
|
|
|
$this->assertEquals( |
88
|
|
|
file_get_contents($generator->getRoot() . '/foo'), |
89
|
|
|
"foo bar baz\n" // Twig generates a newline at EOF... |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parameters |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
**/ |
98
|
|
View Code Duplication |
public function testMissingParameters() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$generator = $this->createGenerator(); |
101
|
|
|
|
102
|
|
|
$structure = new Structure; |
103
|
|
|
$structure->file('foo', new TwigContents( |
|
|
|
|
104
|
|
|
$this->createTwig()->load('template_two.twig'), |
105
|
|
|
array( |
106
|
|
|
'foo' => 'foo', |
107
|
|
|
'baz' => 'baz' |
108
|
|
|
) |
109
|
|
|
)); |
110
|
|
|
|
111
|
|
|
$generator->generate($structure); |
112
|
|
|
|
113
|
|
|
// See if structure was generated |
114
|
|
|
$this->assertEquals( |
115
|
|
|
file_get_contents($generator->getRoot() . '/foo'), |
116
|
|
|
"foo baz\n" // Twig generates a newline at EOF... |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Create a twig environment |
122
|
|
|
* |
123
|
|
|
* @return TwigEnvironment |
124
|
|
|
**/ |
125
|
|
|
private function createTwig() |
126
|
|
|
{ |
127
|
|
|
return new TwigEnvironment( |
128
|
|
|
new TwigFileLoader($this->getTestsRoot() . '/templates/'), |
129
|
|
|
array( |
130
|
|
|
'cache' => sys_get_temp_dir() . '/filegen-tests-twig-compile' |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.