Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
ba551f93
Commit
ba551f93
authored
Jul 20, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved expression support in templates.
parent
72509f0a
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
318 additions
and
136 deletions
+318
-136
template.php
template.php
+185
-81
tests/_files/templates/full.tpl
tests/_files/templates/full.tpl
+2
-2
tests/_files/templates/variables.tpl
tests/_files/templates/variables.tpl
+2
-2
tests/test_template.php
tests/test_template.php
+129
-51
No files found.
template.php
View file @
ba551f93
This diff is collapsed.
Click to expand it.
tests/_files/templates/full.tpl
View file @
ba551f93
...
...
@@ -2,8 +2,8 @@ bar
{
block
:
foo
}
foofoo
{
block
:
bar
}
{
foobar
}
{
$
foobar
}
{
end
}
{
foobaz
:
strtolower
}
{
strtolower
(
$foobaz
)
}
{
end
}
baz
\ No newline at end of file
tests/_files/templates/variables.tpl
View file @
ba551f93
foo
{
foobar
}
{
$
foobar
}
bar
{
foobaz
:
strtolower
}
{
strtolower
(
$foobaz
)
}
baz
\ No newline at end of file
tests/test_template.php
View file @
ba551f93
...
...
@@ -5,6 +5,20 @@ use WebBasics\Template;
use
WebBasics\Node
;
define
(
'TEMPLATES_DIR'
,
'tests/_files/templates/'
);
define
(
'FOOBAR'
,
'foobar_const'
);
class
DataObject
{
var
$foo
=
'bar'
;
var
$bar
=
'baz'
;
function
baz
()
{
return
'foobar'
;
}
static
function
foobar
(
$param
)
{
return
ucfirst
(
$param
);
}
}
class
TemplateTest
extends
PHPUnit_Framework_TestCase
{
/**
...
...
@@ -26,7 +40,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
'true'
=>
true
,
'false'
=>
false
,
'array'
=>
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
),
'object'
=>
$o
bject
,
'object'
=>
new
DataO
bject
,
'foobar'
=>
'my_foobar_variable'
,
'foobaz'
=>
'MY_FOOBAZ_VARIABLE'
,
));
...
...
@@ -108,8 +122,8 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
$child_count
,
count
(
$node
->
get_children
()));
}
function
assert_is_
variable
_node
(
$node
,
$brackets_content
)
{
$this
->
assertEquals
(
'
variable
'
,
$node
->
get_name
());
function
assert_is_
exp
_node
(
$node
,
$brackets_content
)
{
$this
->
assertEquals
(
'
expression
'
,
$node
->
get_name
());
$this
->
assertEquals
(
$brackets_content
,
$node
->
get
(
'content'
));
$this
->
assertEquals
(
array
(),
$node
->
get_children
());
}
...
...
@@ -172,9 +186,9 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
list
(
$foo
,
$foobar
,
$bar
,
$foobaz
,
$baz
)
=
$root_block
->
get_children
();
$this
->
assert_is_html_node
(
$foo
,
"foo
\n
"
);
$this
->
assert_is_
variable_node
(
$foobar
,
'
foobar'
);
$this
->
assert_is_
exp_node
(
$foobar
,
'$
foobar'
);
$this
->
assert_is_html_node
(
$bar
,
"
\n
bar
\n
"
);
$this
->
assert_is_
variable_node
(
$foobaz
,
'foobaz:strtolower
'
);
$this
->
assert_is_
exp_node
(
$foobaz
,
'strtolower($foobaz)
'
);
$this
->
assert_is_html_node
(
$baz
,
"
\n
baz"
);
}
...
...
@@ -196,79 +210,143 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$this
->
assert_is_html_node
(
$foofoo
,
"
\n
foofoo
\n\t
"
);
$this
->
assert_is_block_node
(
$bar
,
'bar'
,
3
);
$this
->
assert_is_html_node
(
$first_space
,
"
\n
"
);
$this
->
assert_is_
variable_node
(
$foobaz
,
'foobaz:strtolower
'
);
$this
->
assert_is_
exp_node
(
$foobaz
,
'strtolower($foobaz)
'
);
$this
->
assert_is_html_node
(
$second_space
,
"
\n
"
);
list
(
$space_before
,
$foobar
,
$space_after
)
=
$bar
->
get_children
();
$this
->
assert_is_html_node
(
$space_before
,
"
\n\t
"
);
$this
->
assert_is_
variable_node
(
$foobar
,
'
foobar'
);
$this
->
assert_is_
exp_node
(
$foobar
,
'$
foobar'
);
$this
->
assert_is_html_node
(
$space_after
,
"
\n\t
"
);
}
function
assert_replaces
(
$expected
,
$variable
)
{
$rm
=
new
ReflectionMethod
(
'WebBasics\Template'
,
'replace_variable'
);
$rm
->
setAccessible
(
true
);
$this
->
assertEquals
(
$expected
,
$rm
->
invoke
(
null
,
$variable
,
$this
->
data
));
function
evaluate_expression
()
{
$args
=
func_get_args
();
$eval
=
new
ReflectionMethod
(
'WebBasics\Template'
,
'evaluate_expression'
);
$eval
->
setAccessible
(
true
);
return
$eval
->
invokeArgs
(
null
,
$args
);
}
function
test_replace_variable_simple
(
)
{
$this
->
assert
_replaces
(
'bar'
,
'foo'
);
function
assert_evaluates
(
$expected
,
$expression
)
{
$this
->
assert
Equals
(
$expected
,
$this
->
evaluate_expression
(
$expression
,
$this
->
data
)
);
}
/**
* @
depends test_replace_variable_simple
/**
* @
expectedException \UnexpectedValueException
*/
function
test_replace_variable_helper_functions
()
{
$this
->
assert_replaces
(
'Bar'
,
'foo:ucfirst'
);
$this
->
assert_replaces
(
'bar'
,
'FOO:strtolower'
);
$this
->
assert_replaces
(
'Bar'
,
'FOO:strtolower:ucfirst'
);
function
test_evaluate_variable_attribute_null
()
{
$this
->
evaluate_expression
(
'$foobarbaz.foo'
,
$this
->
data
);
}
/**
* @
depends test_replace_variable_helper_functions
/**
* @
expectedException \UnexpectedValueException
*/
function
test_replace_variable_constant
()
{
define
(
'FOOCONST'
,
'foobar'
);
$this
->
assert_replaces
(
'foobar'
,
'FOOCONST'
);
$this
->
assert_replaces
(
'FOOBAR'
,
'FOOCONST:strtoupper'
);
function
test_evaluate_variable_attribute_no_such_attribute
()
{
$this
->
evaluate_expression
(
'$object.foobar'
,
$this
->
data
);
}
/**
* @expectedException UnexpectedValueException
* @expectedExceptionMessage Helper function "idonotexist" is not callable.
/**
* @expectedException \UnexpectedValueException
*/
function
test_evaluate_variable_attribute_no_array_or_object
()
{
$this
->
evaluate_expression
(
'$foo.bar'
,
$this
->
data
);
}
/**
* @expectedException \UnexpectedValueException
*/
function
test_
replace_variable_non_callable_helper_function
()
{
$this
->
assert_replaces
(
null
,
'foo:idonotexist'
);
function
test_
evaluate_variable_method_null
()
{
$this
->
evaluate_expression
(
'$foobarbaz.foo()'
,
$this
->
data
);
}
function
test_replace_variable_not_found
()
{
$this
->
assert_replaces
(
'{idonotexist}'
,
'idonotexist'
);
/**
* @expectedException \BadMethodCallException
*/
function
test_evaluate_variable_method_no_such_method
()
{
$this
->
evaluate_expression
(
'$object.foo()'
,
$this
->
data
);
}
function
test_replace_variable_associative_array
()
{
$this
->
assert_replaces
(
'bar'
,
'array.foo'
);
$this
->
assert_replaces
(
'baz'
,
'array.bar'
);
/**
* @expectedException \BadMethodCallException
*/
function
test_evaluate_variable_method_no_object
()
{
$this
->
evaluate_expression
(
'$foo.bar()'
,
$this
->
data
);
}
function
test_replace_variable_object_property
()
{
$this
->
assert_replaces
(
'bar'
,
'object.foo'
);
$this
->
assert_replaces
(
'baz'
,
'object.bar'
);
function
test_evaluate_variable_success
()
{
$this
->
assert_evaluates
(
'bar'
,
'$array.foo'
);
$this
->
assert_evaluates
(
'bar'
,
'$foo'
);
$this
->
assert_evaluates
(
'baz'
,
'$bar'
);
$this
->
assert_evaluates
(
'bar'
,
'$object.foo'
);
$this
->
assert_evaluates
(
'baz'
,
'$object.bar'
);
$this
->
assert_evaluates
(
'foobar'
,
'$object.baz()'
);
}
function
test_replace_variable_if_statement
()
{
$this
->
assert_replaces
(
'bar'
,
'if:true:foo'
);
$this
->
assert_replaces
(
''
,
'if:false:foo'
);
$this
->
assert_replaces
(
'bar'
,
'if:true:foo:else:bar'
);
$this
->
assert_replaces
(
'baz'
,
'if:false:foo:else:bar'
);
$this
->
assert_replaces
(
'Bar'
,
'if:false:foo:else:FOO:strtolower:ucfirst'
);
function
test_evaluate_constant
()
{
$this
->
assert_evaluates
(
'foobar_const'
,
'FOOBAR'
);
$this
->
assert_evaluates
(
'{NON_DEFINED_CONST}'
,
'NON_DEFINED_CONST'
);
}
/*function assert_block_renders($expected_file, $block, $data) {
$rm = new ReflectionMethod('WebBasics\Template', 'render_block');
$rm->setAccessible(true);
$expected_file = "tests/_files/rendered/$expected_file.html";
$this->assertStringEqualsFile($expected_file, $rm->invoke(null, $block, $data));
}*/
function
test_evaluate_no_expression
()
{
$this
->
assert_evaluates
(
'{foo}'
,
'foo'
);
}
function
test_evaluate_condition_if
()
{
$this
->
assert_evaluates
(
'bar'
,
'$true?bar'
);
$this
->
assert_evaluates
(
''
,
'$false?bar'
);
}
function
test_evaluate_condition_if_else
()
{
$this
->
assert_evaluates
(
'bar'
,
'$true?bar:baz'
);
$this
->
assert_evaluates
(
'baz'
,
'$false?bar:baz'
);
}
/**
* @depends test_evaluate_condition_if
* @depends test_evaluate_condition_if_else
*/
function
test_evaluate_condition_spaces
()
{
$this
->
assert_evaluates
(
' bar '
,
'$true? bar : baz'
);
$this
->
assert_evaluates
(
' baz'
,
'$false? bar : baz'
);
$this
->
assert_evaluates
(
' bar '
,
'$true ? bar : baz'
);
$this
->
assert_evaluates
(
' baz'
,
'$false ? bar : baz'
);
$this
->
assert_evaluates
(
' Foo bar '
,
'$true ? Foo bar : Baz foo'
);
$this
->
assert_evaluates
(
' Baz foo'
,
'$false ? Foo bar : Baz foo'
);
}
/**
* @expectedException \BadFunctionCallException
*/
function
test_evaluate_function_error
()
{
$this
->
evaluate_expression
(
'undefined_function($foo)'
,
$this
->
data
);
}
function
test_evaluate_function_success
()
{
$this
->
assert_evaluates
(
'Bar'
,
'ucfirst($foo)'
);
$this
->
assert_evaluates
(
'Bar'
,
'DataObject::foobar($foo)'
);
}
/**
* @depends test_evaluate_function_success
*/
function
test_evaluate_function_nested
()
{
$this
->
assert_evaluates
(
'Bar'
,
'ucfirst(strtolower($FOO))'
);
}
/**
* @depends test_evaluate_variable_success
* @depends test_evaluate_no_expression
* @depends test_evaluate_condition_spaces
* @depends test_evaluate_function_success
*/
function
test_evaluate_expression_combined
()
{
$this
->
assert_evaluates
(
'Bar'
,
'$true?ucfirst($foo)'
);
$this
->
assert_evaluates
(
''
,
'$false?ucfirst($foo)'
);
$this
->
assert_evaluates
(
'Bar'
,
'$true?ucfirst($foo):baz'
);
$this
->
assert_evaluates
(
'baz'
,
'$false?ucfirst($foo):baz'
);
$this
->
assert_evaluates
(
'Baz'
,
'ucfirst($array.bar)'
);
}
function
assert_renders
(
$expected_file
,
$tpl
)
{
$expected_file
=
"tests/_files/rendered/
$expected_file
.html"
;
...
...
@@ -280,7 +358,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test_
replace_variable_helper_functions
* @depends test_
evaluate_expression_combined
*/
function
test_render_variable
()
{
$tpl
=
new
Template
(
'variables'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment