-
Notifications
You must be signed in to change notification settings - Fork 17
/
tests.php
142 lines (126 loc) · 5.58 KB
/
tests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
use PHPUnit\Framework\TestCase;
/**
* To run the tests, use the following command:
* $ phpunit --bootstrap compiler.php tests.php
*/
class CompilerTest extends TestCase
{
/**
* DynamicCSSCompiler::strtoval test
*/
public function testStrToVal()
{
// Make protected functions accessible through a reflection class
$class = new ReflectionClass('DynamicCSSCompiler');
$method = $class->getMethod('strtoval');
$method->setAccessible(true);
$dcss = DynamicCSSCompiler::get_instance();
// Assert
$str = "'value'";
$method->invokeArgs($dcss, array(&$str));
$this->assertEquals($str, 'value');
$str = "false";
$method->invokeArgs($dcss, array(&$str));
$this->assertEquals($str, false);
$str = "true";
$method->invokeArgs($dcss, array(&$str));
$this->assertEquals($str, true);
$str = "4";
$method->invokeArgs($dcss, array(&$str));
$this->assertEquals($str, 4);
$str = "4.321";
$method->invokeArgs($dcss, array(&$str));
$this->assertEquals($str, 4.321);
}
/**
* DynamicCSSCompiler::apply_filters test
*/
public function testFilterApplication()
{
// Make protected functions accessible through a reflection class
$class = new ReflectionClass('DynamicCSSCompiler');
$method = $class->getMethod('apply_filters');
$method->setAccessible(true);
$dcss = DynamicCSSCompiler::get_instance();
// Assert
$this->assertEquals($method->invokeArgs($dcss, array('simple_filter', 'foo', array('simple_filter' => 'simple_filter_callback'))), 'foobar');
$this->assertEquals($method->invokeArgs($dcss, array('simple_filter|simple_filter', 'foo', array('simple_filter' => 'simple_filter_callback'))), 'foobarbar');
$this->assertEquals($method->invokeArgs($dcss, array('complex_filter(\'bar\')', 'foo', array('complex_filter' => 'complex_filter_callback'))), 'foobar');
$this->assertEquals($method->invokeArgs($dcss, array('complex_filter(\'bar\',\'foo\')', 'foo', array('complex_filter' => 'complex_filter_callback'))), 'foobarfoo');
$this->assertEquals($method->invokeArgs($dcss, array('complex_filter(\'bar\')|complex_filter(\'foo\')', 'foo', array('complex_filter' => 'complex_filter_callback'))), 'foobarfoo');
$this->assertEquals($method->invokeArgs($dcss, array('add_filter(5,5)', '5 + 5 = ', array('add_filter' => 'add_filter_callback'))), '5 + 5 = 10');
$this->assertEquals($method->invokeArgs($dcss, array('add_filter(5.5,5.5)', '5.5 + 5.5 = ', array('add_filter' => 'add_filter_callback'))), '5.5 + 5.5 = 11');
}
/**
* DynamicCSSCompiler::compile_css test
*/
public function testCompilation()
{
// Make protected functions accessible through a reflection class
$class = new ReflectionClass('DynamicCSSCompiler');
$method = $class->getMethod('compile_css');
$method->setAccessible(true);
$dcss = DynamicCSSCompiler::get_instance();
// Assert
$this->assertEquals($method->invokeArgs($dcss, array('$var1', 'callback', array())), 'value1');
$this->assertEquals($method->invokeArgs($dcss, array('$var2', 'callback', array())), 'value2');
$this->assertEquals($method->invokeArgs($dcss, array('$var3[\'index1\']', 'callback', array())), 'value3');
$this->assertEquals($method->invokeArgs($dcss, array('$var3[\'index2\'][\'subindex1\']', 'callback', array())), 'value4');
$this->assertEquals($method->invokeArgs($dcss, array('$var3[index2][subindex1]', 'callback', array())), 'value4');
$this->assertEquals($method->invokeArgs($dcss, array('$var3[index2][subindex1]', 'callback', array())), 'value4');
$this->assertEquals($method->invokeArgs($dcss, array('$var4|simpleFilter', 'callback', array('simpleFilter' => 'simple_filter_callback'))), 'valuebar');
$this->assertEquals($method->invokeArgs($dcss, array('$var4|simpleFilter|simpleFilter', 'callback', array('simpleFilter' => 'simple_filter_callback'))), 'valuebarbar');
$this->assertEquals($method->invokeArgs($dcss, array('$var4|complexFilter(6)', 'callback', array('complexFilter' => 'complex_filter_callback'))), 'value6');
$this->assertEquals($method->invokeArgs($dcss, array('$var5|complexFilter(e)|complexFilter(7)', 'callback', array('complexFilter' => 'complex_filter_callback'))), 'value7');
$this->assertEquals($method->invokeArgs($dcss, array('$var3[index1]|simpleFilter', 'callback', array('simpleFilter' => 'simple_filter_callback'))), 'value3bar');
}
}
/**
* Value retrieval function
*/
function callback( $varname, $subscripts )
{
$values = array(
'var1' => 'value1',
'var2' => 'value2',
'var3' => array(
'index1' => 'value3',
'index2' => array(
'subindex1' => 'value4'
)
),
'var4' => 'value',
'var5' => 'valu'
);
$val = $values[$varname];
if( null !== $subscripts )
{
foreach( $subscripts as $subscript )
{
$val = $val[$subscript];
}
}
return $val;
}
/**
* Simple string concat filter
*/
function simple_filter_callback( $foo )
{
return $foo.'bar';
}
/**
* String concat filter with parameters
*/
function complex_filter_callback( $value, $arg1 = '', $arg2 = '')
{
return $value.$arg1.$arg2;
}
/**
* Number adding filter
*/
function add_filter_callback( $value, $a, $b )
{
return $value.($a+$b);
}