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
3566402f
Commit
3566402f
authored
Oct 05, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Camelized class method names
parent
4dde6795
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
575 additions
and
575 deletions
+575
-575
autoloader.php
autoloader.php
+20
-20
base.php
base.php
+1
-1
collection.php
collection.php
+21
-21
logger.php
logger.php
+14
-14
node.php
node.php
+19
-19
router.php
router.php
+7
-7
template.php
template.php
+41
-41
tests/test_autoloader.php
tests/test_autoloader.php
+61
-61
tests/test_base.php
tests/test_base.php
+5
-5
tests/test_collection.php
tests/test_collection.php
+59
-59
tests/test_logger.php
tests/test_logger.php
+70
-70
tests/test_node.php
tests/test_node.php
+65
-65
tests/test_router.php
tests/test_router.php
+12
-12
tests/test_template.php
tests/test_template.php
+180
-180
No files found.
autoloader.php
View file @
3566402f
...
...
@@ -16,7 +16,7 @@ require_once 'base.php';
* Simple example: all classes are located in the 'classes' directory.
* <code>
* $loader = new Autoloader('classes');
* $loader->load
_c
lass('FooBar'); // Includes file 'classes/foo_bar.php'
* $loader->load
C
lass('FooBar'); // Includes file 'classes/foo_bar.php'
* </code>
*
* An Autoloader instance can register itself to the SPL autoload stack, so
...
...
@@ -85,9 +85,9 @@ class Autoloader extends Base {
* @param bool $throw Whether to throw an exception when a class file does not exist.
*/
function
__construct
(
$root_directory
,
$root_namespace
=
'\\'
,
$throw
=
false
)
{
$this
->
set
_root_d
irectory
(
$root_directory
);
$this
->
set
_root_n
amespace
(
$root_namespace
);
$this
->
set
_throw_e
rrors
(
$throw
);
$this
->
set
RootD
irectory
(
$root_directory
);
$this
->
set
RootN
amespace
(
$root_namespace
);
$this
->
set
ThrowE
rrors
(
$throw
);
}
/**
...
...
@@ -95,7 +95,7 @@ class Autoloader extends Base {
*
* @param bool $throw Whether to throw exceptions.
*/
function
set
_throw_e
rrors
(
$throw
)
{
function
set
ThrowE
rrors
(
$throw
)
{
$this
->
throw_errors
=
!!
$throw
;
}
...
...
@@ -104,7 +104,7 @@ class Autoloader extends Base {
*
* @return bool
*/
function
get
_throw_e
rrors
()
{
function
get
ThrowE
rrors
()
{
return
$this
->
throw_errors
;
}
...
...
@@ -113,8 +113,8 @@ class Autoloader extends Base {
*
* @param string $directory The new root directory.
*/
function
set
_root_d
irectory
(
$directory
)
{
$this
->
root_directory
=
self
::
path
_with_s
lash
(
$directory
);
function
set
RootD
irectory
(
$directory
)
{
$this
->
root_directory
=
self
::
path
WithS
lash
(
$directory
);
}
/**
...
...
@@ -122,7 +122,7 @@ class Autoloader extends Base {
*
* @return string
*/
function
get
_root_d
irectory
()
{
function
get
RootD
irectory
()
{
return
$this
->
root_directory
;
}
...
...
@@ -131,9 +131,9 @@ class Autoloader extends Base {
*
* @param string $namespace The new root namespace.
*/
function
set
_root_n
amespace
(
$namespace
)
{
function
set
RootN
amespace
(
$namespace
)
{
// Assert that the namespace ends with a backslash
if
(
$namespace
[
strlen
(
$namespace
)
-
1
]
!=
'\\'
)
if
(
$namespace
[
strlen
(
$namespace
)
-
1
]
!=
'\\'
)
$namespace
.=
'\\'
;
$this
->
root_namespace
=
$namespace
;
...
...
@@ -144,7 +144,7 @@ class Autoloader extends Base {
*
* @return string
*/
function
get
_root_n
amespace
()
{
function
get
RootN
amespace
()
{
return
$this
->
root_namespace
;
}
...
...
@@ -157,7 +157,7 @@ class Autoloader extends Base {
* @param string $classname The class name to convert.
* @return string
*/
static
function
classname
_to_f
ilename
(
$classname
)
{
static
function
classname
ToF
ilename
(
$classname
)
{
return
strtolower
(
preg_replace
(
'/(?<=.)([A-Z])/'
,
'_\\1'
,
$classname
));
}
...
...
@@ -167,7 +167,7 @@ class Autoloader extends Base {
* @param string $classname The name of the class to strip the namespace from.
* @return string The stripped class name.
*/
private
function
strip
_root_n
amespace
(
$classname
)
{
private
function
strip
RootN
amespace
(
$classname
)
{
$begin
=
substr
(
$classname
,
0
,
strlen
(
$this
->
root_namespace
));
if
(
$begin
==
$this
->
root_namespace
)
...
...
@@ -184,9 +184,9 @@ class Autoloader extends Base {
*
* @param string $classname The name of the class to create the file path of.
*/
function
create
_p
ath
(
$classname
)
{
function
create
P
ath
(
$classname
)
{
$namespaces
=
array_filter
(
explode
(
'\\'
,
$classname
));
$dirs
=
array_map
(
'self::classname
_to_f
ilename'
,
$namespaces
);
$dirs
=
array_map
(
'self::classname
ToF
ilename'
,
$namespaces
);
$path
=
$this
->
root_directory
;
if
(
count
(
$dirs
)
>
1
)
...
...
@@ -207,9 +207,9 @@ class Autoloader extends Base {
* @return bool
* @throws FileNotFoundError If the class file does not exist.
*/
function
load
_c
lass
(
$classname
,
$throw
=
null
)
{
$classname
=
$this
->
strip
_root_n
amespace
(
$classname
);
$path
=
$this
->
create
_p
ath
(
$classname
);
function
load
C
lass
(
$classname
,
$throw
=
null
)
{
$classname
=
$this
->
strip
RootN
amespace
(
$classname
);
$path
=
$this
->
create
P
ath
(
$classname
);
if
(
!
file_exists
(
$path
))
{
if
(
$throw
||
(
$throw
===
null
&&
$this
->
throw_errors
))
...
...
@@ -229,7 +229,7 @@ class Autoloader extends Base {
* the stack, instead of appending it.
*/
function
register
(
$prepend
=
false
)
{
spl_autoload_register
(
array
(
$this
,
'load
_c
lass'
),
true
,
$prepend
);
spl_autoload_register
(
array
(
$this
,
'load
C
lass'
),
true
,
$prepend
);
}
}
...
...
base.php
View file @
3566402f
...
...
@@ -61,7 +61,7 @@ abstract class Base {
* @param string $directory The directory to append a slash to.
* @return string
*/
static
function
path
_with_s
lash
(
$directory
)
{
static
function
path
WithS
lash
(
$directory
)
{
return
$directory
[
strlen
(
$directory
)
-
1
]
==
'/'
?
$directory
:
$directory
.
'/'
;
}
}
...
...
collection.php
View file @
3566402f
...
...
@@ -16,25 +16,25 @@ require_once 'base.php';
*
* Example 1: Index a list based on its values and extract a single attribute.
* <code>
* class Book extends
PHP
ActiveRecord\Model {
* class Book extends ActiveRecord\Model {
* static $attr_accessible = array('id', 'name');
* }
*
* // Index all book names by their id.
* $books = Book::all(); // Find a list of books
* $collection = new Collection($books); // Put the list in a collection
* $indexed = $collection->index
_b
y('id'); // Create indexes for all books
* $names = $indexed->get
_a
ttribute('name'); // Get the values of a single attribute
* $indexed = $collection->index
B
y('id'); // Create indexes for all books
* $names = $indexed->get
A
ttribute('name'); // Get the values of a single attribute
* // $names now contains something like array(1 => 'Some book', 2 => 'Another book')
*
* // Same as above:
* $names = Collection::create(Book::all())->index
_by('id')->get_a
ttribute('name');
* $names = Collection::create(Book::all())->index
By('id')->getA
ttribute('name');
* </code>
*
* Example 2: Execute a method for each item in a list.
* <code>
* // Delete all books
* Collection::create(Book::all())->map
_m
ethod('delete');
* Collection::create(Book::all())->map
M
ethod('delete');
* </code>
*
* @package WebBasics
...
...
@@ -138,7 +138,7 @@ class Collection extends Base {
* @param int|string $index The index to check existance of.
* @return bool Whether the index exists.
*/
function
index
_e
xists
(
$index
)
{
function
index
E
xists
(
$index
)
{
return
isset
(
$this
->
items
[
$index
]);
}
...
...
@@ -157,7 +157,7 @@ class Collection extends Base {
*
* @param int|string $index The index to the item to delete.
*/
function
delete
_i
ndex
(
$index
)
{
function
delete
I
ndex
(
$index
)
{
unset
(
$this
->
items
[
$index
]);
}
...
...
@@ -167,7 +167,7 @@ class Collection extends Base {
* @param mixed $item The item to delete.
*/
function
delete
(
$item
)
{
$this
->
delete
_i
ndex
(
array_search
(
$item
,
$this
->
items
));
$this
->
delete
I
ndex
(
array_search
(
$item
,
$this
->
items
));
}
/**
...
...
@@ -178,7 +178,7 @@ class Collection extends Base {
* object's item set and not create a new object.
* @return Collection A collection with the new item set.
*/
private
function
set
_i
tems
(
array
$items
,
$clone
=
true
)
{
private
function
set
I
tems
(
array
$items
,
$clone
=
true
)
{
if
(
$clone
)
return
new
self
(
$items
);
...
...
@@ -194,7 +194,7 @@ class Collection extends Base {
* @return Collection A collection without duplicates.
*/
function
uniques
(
$clone
=
false
)
{
return
$this
->
set
_i
tems
(
array_values
(
array_unique
(
$this
->
items
)),
$clone
);
return
$this
->
set
I
tems
(
array_values
(
array_unique
(
$this
->
items
)),
$clone
);
}
/**
...
...
@@ -207,7 +207,7 @@ class Collection extends Base {
* @return Collection A collection with the filtered set of items.
*/
function
filter
(
$callback
,
$clone
=
true
)
{
return
$this
->
set
_i
tems
(
array_values
(
array_filter
(
$this
->
items
,
$callback
)),
$clone
);
return
$this
->
set
I
tems
(
array_values
(
array_filter
(
$this
->
items
,
$callback
)),
$clone
);
}
/**
...
...
@@ -227,13 +227,13 @@ class Collection extends Base {
return
$this
->
filter
(
function
(
$item
)
use
(
$conditions
)
{
if
(
is_object
(
$item
))
{
// Object, match property values
foreach
(
$conditions
as
$property
=>
$value
)
if
(
$item
->
{
$property
}
!=
$value
)
foreach
(
$conditions
as
$property
=>
$value
)
if
(
$item
->
{
$property
}
!=
$value
)
return
false
;
}
elseif
(
is_array
(
$item
))
{
// Array item, match array values
foreach
(
$conditions
as
$property
=>
$value
)
if
(
$item
[
$property
]
!=
$value
)
if
(
$item
[
$property
]
!=
$value
)
return
false
;
}
else
{
// Other, incompatible type -> throw exception
...
...
@@ -254,7 +254,7 @@ class Collection extends Base {
* @param string $attribute The name of the attribute to get the value of.
* @return array The original item keys, pointing to single attribute values.
*/
function
get
_a
ttribute
(
$attribute
)
{
function
get
A
ttribute
(
$attribute
)
{
return
array_map
(
function
(
$item
)
use
(
$attribute
)
{
return
$item
->
{
$attribute
};
},
$this
->
items
);
...
...
@@ -269,13 +269,13 @@ class Collection extends Base {
* @param bool $clone Whether to create a new object, or overwrite the current item set.
* @return Collection A collection object with the values of the attribute used as indices.
*/
function
index
_b
y
(
$attribute
,
$clone
=
true
)
{
function
index
B
y
(
$attribute
,
$clone
=
true
)
{
$indexed
=
array
();
foreach
(
$this
->
items
as
$item
)
foreach
(
$this
->
items
as
$item
)
$indexed
[
$item
->
$attribute
]
=
$item
;
return
$this
->
set
_i
tems
(
$indexed
,
$clone
);
return
$this
->
set
I
tems
(
$indexed
,
$clone
);
}
/**
...
...
@@ -286,7 +286,7 @@ class Collection extends Base {
* @return Collection A collection with return values of the callback calls.
*/
function
map
(
$callback
,
$clone
=
true
)
{
return
$this
->
set
_i
tems
(
array_map
(
$callback
,
$this
->
items
),
$clone
);
return
$this
->
set
I
tems
(
array_map
(
$callback
,
$this
->
items
),
$clone
);
}
/**
...
...
@@ -299,13 +299,13 @@ class Collection extends Base {
* @param bool $clone Whether to create a new object, or overwrite the current item set.
* @return Collection A collection with return values of the method calls.
*/
function
map
_m
ethod
(
$method_name
,
array
$args
=
array
(),
$clone
=
true
)
{
function
map
M
ethod
(
$method_name
,
array
$args
=
array
(),
$clone
=
true
)
{
$items
=
array
();
foreach
(
$this
->
items
as
$item
)
$items
[]
=
call_user_func_array
(
array
(
$item
,
$method_name
),
$args
);
return
$this
->
set
_i
tems
(
$items
);
return
$this
->
set
I
tems
(
$items
);
}
}
...
...
logger.php
View file @
3566402f
...
...
@@ -35,34 +35,34 @@ class Logger extends Base {
private
$dump_format
=
'plain'
;
private
$log_directory
=
''
;
function
set
_d
irectory
(
$directory
)
{
$this
->
log_directory
=
self
::
path
_with_s
lash
(
$directory
);
function
set
D
irectory
(
$directory
)
{
$this
->
log_directory
=
self
::
path
WithS
lash
(
$directory
);
}
function
set
_dump_f
ormat
(
$format
)
{
function
set
DumpF
ormat
(
$format
)
{
if
(
!
in_array
(
$format
,
self
::
$allowed_dump_formats
))
throw
new
\InvalidArgumentException
(
sprintf
(
''
,
$format
));
$this
->
dump_format
=
$format
;
}
function
set
_f
ormat
(
$format
)
{
function
set
F
ormat
(
$format
)
{
$this
->
format
=
(
string
)
$format
;
}
function
get
_f
ormat
()
{
function
get
F
ormat
()
{
return
$this
->
format
;
}
function
get
_l
evel
()
{
function
get
L
evel
()
{
return
$this
->
level
;
}
function
get
_level_n
ame
()
{
function
get
LevelN
ame
()
{
return
self
::
$level_names
[
$this
->
level
];
}
function
set
_l
evel
(
$level
)
{
function
set
L
evel
(
$level
)
{
if
(
is_string
(
$level
))
{
$level
=
strtoupper
(
$level
);
...
...
@@ -78,7 +78,7 @@ class Logger extends Base {
$this
->
level
=
$level
;
}
function
set
_p
roperty
(
$name
,
$value
)
{
function
set
P
roperty
(
$name
,
$value
)
{
$this
->
properties
[
$name
]
=
(
string
)
$value
;
}
...
...
@@ -133,7 +133,7 @@ class Logger extends Base {
if
(
$name
==
'level'
)
return
Logger
::
$level_names
[
$level
];
return
$logger
->
get
_formatted_p
roperty
(
$matches
[
1
]);
return
$logger
->
get
FormattedP
roperty
(
$matches
[
1
]);
},
$this
->
format
);
...
...
@@ -169,7 +169,7 @@ class Logger extends Base {
file_put_contents
(
$this
->
log_directory
.
$path
,
$this
->
dumps
());
}
function
handle
_e
xception
(
\Exception
$e
)
{
function
handle
E
xception
(
\Exception
$e
)
{
if
(
$e
===
null
)
return
;
...
...
@@ -179,17 +179,17 @@ class Logger extends Base {
$this
->
dump
(
'error'
);
}
function
set
_as_exception_h
andler
()
{
function
set
AsExceptionH
andler
()
{
set_exception_handler
(
array
(
$this
,
'handle_exception'
));
}
function
get
_formatted_p
roperty
(
$property
)
{
function
get
FormattedP
roperty
(
$property
)
{
if
(
isset
(
$this
->
properties
[
$property
]))
return
$this
->
properties
[
$property
];
switch
(
$property
)
{
case
'loglevel'
:
return
$this
->
get
_level_n
ame
();
return
$this
->
get
LevelN
ame
();
case
'date'
:
return
strftime
(
'%d-%m-%Y'
);
case
'time'
:
...
...
node.php
View file @
3566402f
...
...
@@ -88,7 +88,7 @@ class Node extends Base {
*
* @return int The node's id.
*/
function
get
_i
d
()
{
function
get
I
d
()
{
return
$this
->
id
;
}
...
...
@@ -97,7 +97,7 @@ class Node extends Base {
*
* @return string The node's name.
*/
function
get
_n
ame
()
{
function
get
N
ame
()
{
return
$this
->
name
;
}
...
...
@@ -106,7 +106,7 @@ class Node extends Base {
*
* @return Node|null The parent node if any, NULL otherwise.
*/
function
get
_p
arent
()
{
function
get
P
arent
()
{
return
$this
->
parent_node
;
}
...
...
@@ -115,7 +115,7 @@ class Node extends Base {
*
* @return array A list of child nodes.
*/
function
get
_c
hildren
()
{
function
get
C
hildren
()
{
return
$this
->
children
;
}
...
...
@@ -126,7 +126,7 @@ class Node extends Base {
* @return bool Whether the nodes have the same unique id.
*/
function
is
(
Node
$node
)
{
return
$node
->
get
_i
d
()
==
$this
->
id
;
return
$node
->
get
I
d
()
==
$this
->
id
;
}
/**
...
...
@@ -136,7 +136,7 @@ class Node extends Base {
*
* @return bool Whether this node is the root node.
*/
function
is
_r
oot
()
{
function
is
R
oot
()
{
return
$this
->
parent_node
===
null
;
}
...
...
@@ -147,7 +147,7 @@ class Node extends Base {
*
* @return bool Whether this node is a leaf node.
*/
function
is
_l
eaf
()
{
function
is
L
eaf
()
{
return
!
count
(
$this
->
children
);
}
...
...
@@ -158,9 +158,9 @@ class Node extends Base {
* @param bool $set_parent Whether to set this node as the child's parent
* (defaults to TRUE).
*/
function
add
_c
hild
(
Node
&
$node
,
$set_parent
=
true
)
{
function
add
C
hild
(
Node
&
$node
,
$set_parent
=
true
)
{
$this
->
children
[]
=
$node
;
$set_parent
&&
$node
->
set
_p
arent
(
$this
);
$set_parent
&&
$node
->
set
P
arent
(
$this
);
}
/**
...
...
@@ -172,7 +172,7 @@ class Node extends Base {
*/
function
add
(
$name
,
array
$data
=
array
())
{
$node
=
new
self
(
$name
,
$this
);
$this
->
add
_c
hild
(
$node
,
false
);
$this
->
add
C
hild
(
$node
,
false
);
return
$node
->
set
(
$data
);
}
...
...
@@ -182,7 +182,7 @@ class Node extends Base {
*
* @param Node &$child The node to remove.
*/
function
remove
_c
hild
(
Node
&
$child
)
{
function
remove
C
hild
(
Node
&
$child
)
{
foreach
(
$this
->
children
as
$i
=>
$node
)
$node
->
is
(
$child
)
&&
array_splice
(
$this
->
children
,
$i
,
1
);
}
...
...
@@ -194,13 +194,13 @@ class Node extends Base {
* @return Node This node.
*/
function
remove
()
{
if
(
$this
->
is
_r
oot
())
if
(
$this
->
is
R
oot
())
throw
new
\RuntimeException
(
'Cannot remove the root node of a tree.'
);
$this
->
parent_node
->
remove
_c
hild
(
$this
);
$this
->
parent_node
->
remove
C
hild
(
$this
);
foreach
(
$this
->
children
as
$child
)
$child
->
set
_p
arent
(
null
);
$child
->
set
P
arent
(
null
);
return
$this
;
}
...
...
@@ -214,9 +214,9 @@ class Node extends Base {
* @param Node|null $parent The parent node to set.
* @return Node This node.
*/
function
set
_p
arent
(
$parent
)
{
function
set
P
arent
(
$parent
)
{
if
(
$this
->
parent_node
!==
null
)
$this
->
parent_node
->
remove
_c
hild
(
$this
);
$this
->
parent_node
->
remove
C
hild
(
$this
);
$this
->
parent_node
=
&
$parent
;
...
...
@@ -292,7 +292,7 @@ class Node extends Base {
*/
function
find
(
$name
)
{
$has_name
=
function
(
$child
)
use
(
$name
)
{
return
$child
->
get
_n
ame
()
==
$name
;
return
$child
->
get
N
ame
()
==
$name
;
};
return
array_values
(
array_filter
(
$this
->
children
,
$has_name
));
...
...
@@ -314,9 +314,9 @@ class Node extends Base {
foreach
(
$this
->
children
as
$child
)
{
if
(
$deep
)
{
$child_copy
=
$child
->
copy
(
true
);
$copy
->
add
_c
hild
(
$child_copy
);
$copy
->
add
C
hild
(
$child_copy
);
}
else
{
$copy
->
add
_c
hild
(
$child
,
false
);
$copy
->
add
C
hild
(
$child
,
false
);
}
}
...
...
router.php
View file @
3566402f
...
...
@@ -28,8 +28,8 @@ require_once 'base.php';
* '/home' => 'home',
* '/contact' => 'contact'
* ));
* $response = $router->call
_h
andler('/home'); // 'This is the home page.'
* $response = $router->call
_h
andler('/contact'); // 'This is the contact page.'
* $response = $router->call
H
andler('/home'); // 'This is the home page.'
* $response = $router->call
H
andler('/contact'); // 'This is the contact page.'
* </code>
*
* You can use regular expression patterns to specify an URL. Any matches are
...
...
@@ -42,8 +42,8 @@ require_once 'base.php';
* $router = new Router(array(
* '/(home|contact)' => 'page'
* ));
* $response = $router->call
_h
andler('/home'); // 'This is the home page.'
* $response = $router->call
_h
andler('/contact'); // 'This is the contact page.'
* $response = $router->call
H
andler('/home'); // 'This is the home page.'
* $response = $router->call
H
andler('/contact'); // 'This is the contact page.'
* </code>
*
* @package WebBasics
...
...
@@ -70,7 +70,7 @@ class Router extends Base {
*/
function
__construct
(
array
$routes
=
array
())
{
foreach
(
$routes
as
$pattern
=>
$handler
)
$this
->
add
_r
oute
(
$pattern
,
$handler
);
$this
->
add
R
oute
(
$pattern
,
$handler
);
}
/**
...
...
@@ -87,7 +87,7 @@ class Router extends Base {
* @param mixed $handler The handler function to call when $pattern is matched.
* @throws \InvalidArgumentException If $handler is not callable.
*/
function
add
_r
oute
(
$pattern
,
$handler
)
{
function
add
R
oute
(
$pattern
,
$handler
)
{
if
(
!
is_callable
(
$handler
))
throw
new
\InvalidArgumentException
(
sprintf
(
'Handler for patterns "%s" is not callable.'
,
$pattern
));
...
...
@@ -107,7 +107,7 @@ class Router extends Base {
* @return mixed FALSE if no pattern was matched, the return value of the
* corresponding handler function otherwise.
*/
function
call
_h
andler
(
$url
)
{
function
call
H
andler
(
$url
)
{
foreach
(
$this
->
routes
as
$pattern
=>
$handler
)
{
if
(
preg_match
(
$pattern
,
$url
,
$matches
))
{
array_shift
(
$matches
);
...
...
template.php
View file @
3566402f
...
...
@@ -43,7 +43,7 @@ require_once 'node.php';
* 'page_content' => 'Lorem ipsum ...'
* ));
*
* foreach
( array('Some ad', 'Another ad', 'More ads') as $ad
)
* foreach
(array('Some ad', 'Another ad', 'More ads') as $ad
)
* $tpl->add('ad')->set('ad_content', $ad);
*
* echo $tpl->render();
...
...
@@ -164,7 +164,7 @@ class Template extends Node {
);
}
$this
->
parse
_b
locks
();
$this
->
parse
B
locks
();
}
/**
...
...
@@ -172,7 +172,7 @@ class Template extends Node {
*
* @return string The path to the template file.
*/
function
get
_p
ath
()
{
function
get
P
ath
()
{
return
$this
->
path
;
}
...
...
@@ -182,7 +182,7 @@ class Template extends Node {
*
* @throws ParseError If an {end} tag is not used properly.
*/
private
function
parse
_b
locks
()
{
private
function
parse
B
locks
()
{
$current
=
$root
=
new
Node
(
'block'
);
$after
=
$this
->
file_content
;
$line_count
=
0
;
...
...
@@ -196,10 +196,10 @@ class Template extends Node {
if
(
$brackets_content
==
'end'
)
{
// {end} encountered, go one level up in the tree
if
(
$current
->
is
_r
oot
())
if
(
$current
->
is
R
oot
())
throw
new
ParseError
(
$this
,
'unexpected {end}'
,
$line_count
+
1
);
$current
=
$current
->
get
_p
arent
();
$current
=
$current
->
get
P
arent
();
}
elseif
(
substr
(
$brackets_content
,
0
,
6
)
==
'block:'
)
{
// {block:...} encountered
$block_name
=
substr
(
$brackets_content
,
6
);
...
...
@@ -232,7 +232,7 @@ class Template extends Node {
*/
function
render
()
{
// Use recursion to parse all blocks from the root level
return
self
::
render
_b
lock
(
$this
->
root_block
,
$this
);
return
self
::
render
B
lock
(
$this
->
root_block
,
$this
);
}
/**
...
...
@@ -241,13 +241,13 @@ class Template extends Node {
* @param Node $block The block to render.
* @param Node $data The data block to search in for the variable values.
* @return string The rendered block.
* @uses evaluate
_e
xpression()
* @uses evaluate
E
xpression()
*/
private
static
function
render
_b
lock
(
Node
$block
,
Node
$data
)
{
private
static
function
render
B
lock
(
Node
$block
,
Node
$data
)
{
$html
=
''
;
foreach
(
$block
->
get
_c
hildren
()
as
$child
)
{
switch
(
$child
->
get
_n
ame
())
{
foreach
(
$block
->
get
C
hildren
()
as
$child
)
{
switch
(
$child
->
get
N
ame
())
{
case
'html'
:
$html
.=
$child
->
get
(
'content'
);
break
;
...
...
@@ -255,11 +255,11 @@ class Template extends Node {
$block_name
=
$child
->
get
(
'name'
);
foreach
(
$data
->
find
(
$block_name
)
as
$child_data
)
$html
.=
self
::
render
_b
lock
(
$child
,
$child_data
);
$html
.=
self
::
render
B
lock
(
$child
,
$child_data
);
break
;
case
'expression'
:
$html
.=
self
::
evaluate
_e
xpression
(
$child
->
get
(
'content'
),
$data
);
$html
.=
self
::
evaluate
E
xpression
(
$child
->
get
(
'content'
),
$data
);
}
}
...
...
@@ -269,7 +269,7 @@ class Template extends Node {
/**
* Evaluate a <variable> expression.
*
* This function is a helper for {@link evaluate
_e
xpression()}.
* This function is a helper for {@link evaluate
E
xpression()}.
*
* @param string[] $matches Regex matches for variable pattern.
* @return string The evaluation of the variable.
...
...
@@ -278,7 +278,7 @@ class Template extends Node {
* @throws \OutOfBoundsException If an unexisting array key is requested.
* @throws \UnexpectedValueException In some other error situations.
*/
private
static
function
evaluate
_v
ariable
(
array
$matches
,
Node
$data
)
{
private
static
function
evaluate
V
ariable
(
array
$matches
,
Node
$data
)
{
$before
=
$matches
[
1
];
$noescape_sign
=
$matches
[
2
];
$variable
=
$matches
[
3
];
...
...
@@ -335,7 +335,7 @@ class Template extends Node {
// Escape value
if
(
is_string
(
$value
)
&&
!
$noescape_sign
)
$value
=
self
::
escape
_variable_v
alue
(
$value
);
$value
=
self
::
escape
VariableV
alue
(
$value
);
return
$before
.
$value
;
}
...
...
@@ -348,27 +348,27 @@ class Template extends Node {
* @param string $value The variable value to escape.
* @return string The escaped value.
*/
private
static
function
escape
_variable_v
alue
(
$value
)
{
private
static
function
escape
VariableV
alue
(
$value
)
{
return
htmlspecialchars
(
$value
,
ENT_QUOTES
);
}
/**
* Evaluate a conditional expression.
*
* This function is a helper for {@link evaluate
_e
xpression()}.
* This function is a helper for {@link evaluate
E
xpression()}.
*
* @param string[] $matches Regex matches for conditional pattern.
* @param Node $data A data tree containing variable values to use for
* variable expressions.
* @return string The evaluation of the condition.
*/
private
static
function
evaluate
_c
ondition
(
array
$matches
,
Node
$data
)
{
if
(
self
::
evaluate
_e
xpression
(
$matches
[
1
],
$data
,
false
))
{
private
static
function
evaluate
C
ondition
(
array
$matches
,
Node
$data
)
{
if
(
self
::
evaluate
E
xpression
(
$matches
[
1
],
$data
,
false
))
{
// Condition evaluates to true: return 'if' evaluation
return
self
::
evaluate
_e
xpression
(
$matches
[
2
],
$data
,
false
);
return
self
::
evaluate
E
xpression
(
$matches
[
2
],
$data
,
false
);
}
elseif
(
count
(
$matches
)
==
4
)
{
// <nested_exp>?<nested_exp>:<nested_exp>
return
self
::
evaluate
_e
xpression
(
$matches
[
3
],
$data
,
false
);
return
self
::
evaluate
E
xpression
(
$matches
[
3
],
$data
,
false
);
}
// No 'else' specified: evaluation is an empty string
...
...
@@ -378,7 +378,7 @@ class Template extends Node {
/**
* Evaluate a static function call expression.
*
* This function is a helper for {@link evaluate
_e
xpression()}.
* This function is a helper for {@link evaluate
E
xpression()}.
*
* @param array $matches Regex matches for function pattern.
* @param Node $data A data tree containing variable values to use for
...
...
@@ -386,7 +386,7 @@ class Template extends Node {
* @return string The evaluation of the function call.
* @throws \BadFunctionCallException If the function is undefined.
*/
private
static
function
evaluate
_f
unction
(
array
$matches
,
Node
$data
)
{
private
static
function
evaluate
F
unction
(
array
$matches
,
Node
$data
)
{
$function
=
$matches
[
1
];
$parameter
=
$matches
[
2
];
...
...
@@ -396,7 +396,7 @@ class Template extends Node {
);
}
$parameter_value
=
self
::
evaluate
_e
xpression
(
$parameter
,
$data
,
false
);
$parameter_value
=
self
::
evaluate
E
xpression
(
$parameter
,
$data
,
false
);
return
call_user_func
(
$function
,
$parameter_value
);
}
...
...
@@ -404,7 +404,7 @@ class Template extends Node {
/**
* Evaluate a PHP-constant expression.
*
* This function is a helper for {@link evaluate
_e
xpression()}.
* This function is a helper for {@link evaluate
E
xpression()}.
*
* @param string $constant The name of the PHP constant.
* @param bool $root_level Whether the expression was enclosed in curly
...
...
@@ -412,7 +412,7 @@ class Template extends Node {
* @return string The evaluation of the constant if it is defined, the
* original constant name otherwise.
*/
private
static
function
evaluate
_c
onstant
(
$constant
,
$root_level
)
{
private
static
function
evaluate
C
onstant
(
$constant
,
$root_level
)
{
if
(
defined
(
$constant
))
return
constant
(
$constant
);
...
...
@@ -433,30 +433,30 @@ class Template extends Node {
* @return string The evaluation of the expression if present, the
* original string enclosed in curly brackets otherwise.
*/
private
static
function
evaluate
_e
xpression
(
$expression
,
Node
$data
,
$root_level
=
true
)
{
private
static
function
evaluate
E
xpression
(
$expression
,
Node
$data
,
$root_level
=
true
)
{
if
(
$expression
)
{
$name
=
'[a-zA-Z0-9-_]+'
;
$function
=
"
$name
(?:::
$name
)?"
;
if
(
preg_match
(
"/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/"
,
$expression
,
$matches
))
{
// <nested_exp>?<nested_exp> | <nested_exp>?<nested_exp>:<nested_exp>
return
self
::
evaluate
_c
ondition
(
$matches
,
$data
);
return
self
::
evaluate
C
ondition
(
$matches
,
$data
);
}
elseif
(
preg_match
(
"/^(.*?)
\\
$(
\\
$?)(
$name
)(?:\.(
$name
)(\(\))?)?$/"
,
$expression
,
$matches
))
{
// $<name> | $<name>.<name> | $<name>.<name>()
// | $$<name> | $$<name>.<name> | $$<name>.<name>()
return
self
::
evaluate
_v
ariable
(
$matches
,
$data
);
return
self
::
evaluate
V
ariable
(
$matches
,
$data
);
}
elseif
(
preg_match
(
"/^(
$function
)\((.+?)\)?$/"
,
$expression
,
$matches
))
{
// <function>(<nested_exp>)
return
self
::
evaluate
_f
unction
(
$matches
,
$data
);
return
self
::
evaluate
F
unction
(
$matches
,
$data
);
}
elseif
(
preg_match
(
"/^([A-Z0-9_]+)$/"
,
$expression
,
$matches
))
{
// <constant>
return
self
::
evaluate
_c
onstant
(
$expression
,
$root_level
);
return
self
::
evaluate
C
onstant
(
$expression
,
$root_level
);
}
elseif
((
$split_at
=
strpos
(
$expression
,
'||'
,
1
))
!==
false
)
{
// <nested_exp>||<nested_exp>
try
{
return
self
::
evaluate
_e
xpression
(
substr
(
$expression
,
0
,
$split_at
),
$data
,
false
);
return
self
::
evaluate
E
xpression
(
substr
(
$expression
,
0
,
$split_at
),
$data
,
false
);
}
catch
(
\RuntimeException
$e
)
{
return
self
::
evaluate
_e
xpression
(
substr
(
$expression
,
$split_at
+
2
),
$data
,
false
);
return
self
::
evaluate
E
xpression
(
substr
(
$expression
,
$split_at
+
2
),
$data
,
false
);
}
}
}
...
...
@@ -468,7 +468,7 @@ class Template extends Node {
/**
* Remove all current include paths.
*/
static
function
clear
_include_p
ath
()
{
static
function
clear
IncludeP
ath
()
{
self
::
$include_path
=
array
();
}
...
...
@@ -476,11 +476,11 @@ class Template extends Node {
* Replace all include paths by a single new one.
*
* @param string $path The new path to set as root.
* @uses clear
_include_p
ath()
* @uses clear
IncludeP
ath()
*/
static
function
set
_r
oot
(
$path
)
{
self
::
clear
_include_p
ath
();
self
::
add
_r
oot
(
$path
);
static
function
set
R
oot
(
$path
)
{
self
::
clear
IncludeP
ath
();
self
::
add
R
oot
(
$path
);
}
/**
...
...
@@ -489,7 +489,7 @@ class Template extends Node {
* @param string $path The path to add.
* @throws FileNotFoundError If the path does not exist.
*/
static
function
add
_r
oot
(
$path
)
{
static
function
add
R
oot
(
$path
)
{
if
(
$path
[
strlen
(
$path
)
-
1
]
!=
'/'
)
$path
.=
'/'
;
...
...
@@ -517,7 +517,7 @@ class ParseError extends \RuntimeException {
*/
function
__construct
(
Template
$tpl
,
$message
,
$line
)
{
$this
->
message
=
sprintf
(
'Parse error in file %s, line %d: %s'
,
$tpl
->
get
_p
ath
(),
$line
,
$message
);
$tpl
->
get
P
ath
(),
$line
,
$message
);
}
}
...
...
tests/test_autoloader.php
View file @
3566402f
...
...
@@ -10,139 +10,139 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase {
$this
->
autoloader
=
new
Autoloader
(
PATH
);
}
function
test
_set_root_n
amespace
()
{
function
test
SetRootN
amespace
()
{
$this
->
assertAttributeEquals
(
'\\'
,
'root_namespace'
,
$this
->
autoloader
);
$this
->
autoloader
->
set
_root_n
amespace
(
'Foo'
);
$this
->
autoloader
->
set
RootN
amespace
(
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
$this
->
autoloader
->
set
_root_n
amespace
(
'Foo\\'
);
$this
->
autoloader
->
set
RootN
amespace
(
'Foo\\'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$this
->
autoloader
);
}
/**
* @depends test
_set_root_n
amespace
* @depends test
SetRootN
amespace
*/
function
test
_get_root_n
amespace
()
{
$this
->
autoloader
->
set
_root_n
amespace
(
'Foo'
);
$this
->
assertEquals
(
$this
->
autoloader
->
get
_root_n
amespace
(),
'Foo\\'
);
function
test
GetRootN
amespace
()
{
$this
->
autoloader
->
set
RootN
amespace
(
'Foo'
);
$this
->
assertEquals
(
$this
->
autoloader
->
get
RootN
amespace
(),
'Foo\\'
);
}
/**
* @depends test
_set_root_n
amespace
* @depends test
SetRootN
amespace
*/
function
test
_construct_root_n
amespace
()
{
function
test
ConstructRootN
amespace
()
{
$autoloader
=
new
Autoloader
(
PATH
,
'Foo'
);
$this
->
assertAttributeEquals
(
'Foo\\'
,
'root_namespace'
,
$autoloader
);
}
/**
* @depends test
_set_root_n
amespace
* @depends test
SetRootN
amespace
*/
function
test
_strip_root_n
amespace
()
{
$strip
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'strip
_root_n
amespace'
);
function
test
StripRootN
amespace
()
{
$strip
=
new
ReflectionMethod
(
'webbasics\Autoloader'
,
'strip
RootN
amespace'
);
$strip
->
setAccessible
(
true
);
$this
->
autoloader
->
set
_root_n
amespace
(
'Foo'
);
$this
->
autoloader
->
set
RootN
amespace
(
'Foo'
);
$this
->
assertEquals
(
$strip
->
invoke
(
$this
->
autoloader
,
'Foo\Bar'
),
'Bar'
);
}
function
test
_set_root_d
irectory
()
{
$this
->
autoloader
->
set
_root_d
irectory
(
'tests'
);
$this
->
assertEquals
(
$this
->
autoloader
->
get
_root_d
irectory
(),
'tests/'
);
function
test
SetRootD
irectory
()
{
$this
->
autoloader
->
set
RootD
irectory
(
'tests'
);
$this
->
assertEquals
(
$this
->
autoloader
->
get
RootD
irectory
(),
'tests/'
);
}
function
test
_classname_to_f
ilename
()
{
$this
->
assertEquals
(
Autoloader
::
classname
_to_f
ilename
(
'Foo'
),
'foo'
);
$this
->
assertEquals
(
Autoloader
::
classname
_to_f
ilename
(
'FooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classname
_to_f
ilename
(
'fooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classname
_to_f
ilename
(
'FooBarBaz'
),
'foo_bar_baz'
);
function
test
ClassnameToF
ilename
()
{
$this
->
assertEquals
(
Autoloader
::
classname
ToF
ilename
(
'Foo'
),
'foo'
);
$this
->
assertEquals
(
Autoloader
::
classname
ToF
ilename
(
'FooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classname
ToF
ilename
(
'fooBar'
),
'foo_bar'
);
$this
->
assertEquals
(
Autoloader
::
classname
ToF
ilename
(
'FooBarBaz'
),
'foo_bar_baz'
);
}
/**
* @depends test
_classname_to_f
ilename
* @depends test
ClassnameToF
ilename
*/
function
test
_create_p
ath
()
{
$this
->
assertEquals
(
$this
->
autoloader
->
create
_p
ath
(
'Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
_p
ath
(
'\Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
_p
ath
(
'Foo\Bar'
),
PATH
.
'foo/bar.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
_p
ath
(
'Foo\Bar\Baz'
),
PATH
.
'foo/bar/baz.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
_p
ath
(
'FooBar\Baz'
),
PATH
.
'foo_bar/baz.php'
);
function
test
CreateP
ath
()
{
$this
->
assertEquals
(
$this
->
autoloader
->
create
P
ath
(
'Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
P
ath
(
'\Foo'
),
PATH
.
'foo.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
P
ath
(
'Foo\Bar'
),
PATH
.
'foo/bar.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
P
ath
(
'Foo\Bar\Baz'
),
PATH
.
'foo/bar/baz.php'
);
$this
->
assertEquals
(
$this
->
autoloader
->
create
P
ath
(
'FooBar\Baz'
),
PATH
.
'foo_bar/baz.php'
);
}
function
test
_throw_e
rrors
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
get
_throw_e
rrors
());
$this
->
autoloader
->
set
_throw_e
rrors
(
true
);
$this
->
assertTrue
(
$this
->
autoloader
->
get
_throw_e
rrors
());
function
test
ThrowE
rrors
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
get
ThrowE
rrors
());
$this
->
autoloader
->
set
ThrowE
rrors
(
true
);
$this
->
assertTrue
(
$this
->
autoloader
->
get
ThrowE
rrors
());
}
/**
* @depends test
_create_p
ath
* @depends test
_throw_e
rrors
* @depends test
CreateP
ath
* @depends test
ThrowE
rrors
*/
function
test
_load_class_not_f
ound
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
load
_c
lass
(
'foobar'
));
function
test
LoadClassNotF
ound
()
{
$this
->
assertFalse
(
$this
->
autoloader
->
load
C
lass
(
'foobar'
));
}
/**
* @depends test
_load_class_not_f
ound
* @depends test
LoadClassNotF
ound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function
test
_load_class_not_found_e
rror
()
{
$this
->
autoloader
->
set
_throw_e
rrors
(
true
);
$this
->
autoloader
->
load
_c
lass
(
'foobar'
);
function
test
LoadClassNotFoundE
rror
()
{
$this
->
autoloader
->
set
ThrowE
rrors
(
true
);
$this
->
autoloader
->
load
C
lass
(
'foobar'
);
}
/**
* @depends test
_load_class_not_f
ound
* @depends test
LoadClassNotF
ound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function
test
_load_class_not_found_noerror_o
verwrite
()
{
$this
->
autoloader
->
load
_c
lass
(
'foobar'
,
true
);
function
test
LoadClassNotFoundNoerrorO
verwrite
()
{
$this
->
autoloader
->
load
C
lass
(
'foobar'
,
true
);
}
/**
* @depends test
_load_class_not_f
ound
* @depends test
LoadClassNotF
ound
*/
function
test
_load_class_not_found_error_o
verwrite
()
{
$this
->
autoloader
->
set
_throw_e
rrors
(
true
);
$this
->
assertFalse
(
$this
->
autoloader
->
load
_c
lass
(
'foobar'
,
false
));
function
test
LoadClassNotFoundErrorO
verwrite
()
{
$this
->
autoloader
->
set
ThrowE
rrors
(
true
);
$this
->
assertFalse
(
$this
->
autoloader
->
load
C
lass
(
'foobar'
,
false
));
}
/**
* @depends test
_load_class_not_f
ound
* @depends test
LoadClassNotF
ound
*/
function
test
_load_c
lass
()
{
$this
->
assertTrue
(
$this
->
autoloader
->
load
_c
lass
(
'Foo'
));
function
test
LoadC
lass
()
{
$this
->
assertTrue
(
$this
->
autoloader
->
load
C
lass
(
'Foo'
));
$this
->
assertTrue
(
class_exists
(
'Foo'
,
false
));
$this
->
assertTrue
(
$this
->
autoloader
->
load
_c
lass
(
'Foo\Bar'
));
$this
->
assertTrue
(
$this
->
autoloader
->
load
C
lass
(
'Foo\Bar'
));
$this
->
assertTrue
(
class_exists
(
'Foo\Bar'
,
false
));
}
/**
* @depends test
_load_c
lass
* @depends test
_strip_root_n
amespace
* @depends test
LoadC
lass
* @depends test
StripRootN
amespace
*/
function
test
_load_class_root_n
amespace
()
{
function
test
LoadClassRootN
amespace
()
{
$autoloader
=
new
Autoloader
(
PATH
.
'foo'
);
$autoloader
->
set
_root_n
amespace
(
'Foo'
);
$this
->
assertTrue
(
$autoloader
->
load
_c
lass
(
'Bar'
));
$autoloader
->
set
RootN
amespace
(
'Foo'
);
$this
->
assertTrue
(
$autoloader
->
load
C
lass
(
'Bar'
));
$this
->
assertTrue
(
class_exists
(
'Foo\Bar'
,
false
));
}
/**
* @depends test
_load_c
lass
* @depends test
LoadC
lass
*/
function
test
_r
egister
()
{
function
test
R
egister
()
{
$this
->
autoloader
->
register
();
$this
->
assertTrue
(
class_exists
(
'Baz'
));
}
/**
* @depends test
_r
egister
* @depends test
_throw_e
rrors
* @depends test
R
egister
* @depends test
ThrowE
rrors
*/
function
test
_register_p
repend
()
{
function
test
RegisterP
repend
()
{
$second_loader
=
new
Autoloader
(
PATH
.
'second'
);
$this
->
autoloader
->
register
();
$second_loader
->
register
(
true
);
// Prepend so that the second loader attemps to load Bar first
...
...
tests/test_base.php
View file @
3566402f
...
...
@@ -11,11 +11,11 @@ class BaseExtension extends Base {
}
class
BaseTest
extends
PHPUnit_Framework_TestCase
{
function
test
_c
reate
()
{
function
test
C
reate
()
{
$this
->
assertEquals
(
BaseExtension
::
create
(
'a'
,
'b'
),
new
BaseExtension
(
'a'
,
'b'
));
}
function
test
_a
sprintf
()
{
function
test
A
sprintf
()
{
$this
->
assertEquals
(
webbasics\asprintf
(
'%(foo) baz'
,
array
(
'foo'
=>
'bar'
)),
'bar baz'
);
$this
->
assertEquals
(
webbasics\asprintf
(
'%(foo) baz %(foo)'
,
array
(
'foo'
=>
'bar'
)),
'bar baz bar'
);
...
...
@@ -23,9 +23,9 @@ class BaseTest extends PHPUnit_Framework_TestCase {
array
(
'foo'
=>
'bar'
,
'bar'
=>
'foobar'
)),
'foobar baz bar'
);
}
function
test
_path_with_s
lash
()
{
$this
->
assertEquals
(
Base
::
path
_with_s
lash
(
'dirname'
),
'dirname/'
);
$this
->
assertEquals
(
Base
::
path
_with_s
lash
(
'dirname/'
),
'dirname/'
);
function
test
PathWithS
lash
()
{
$this
->
assertEquals
(
Base
::
path
WithS
lash
(
'dirname'
),
'dirname/'
);
$this
->
assertEquals
(
Base
::
path
WithS
lash
(
'dirname/'
),
'dirname/'
);
}
}
...
...
tests/test_collection.php
View file @
3566402f
...
...
@@ -11,11 +11,11 @@ class IdObject {
$this
->
foo
=
$foo
;
}
function
get
_i
d
()
{
function
get
I
d
()
{
return
$this
->
id
;
}
static
function
clear
_c
ounter
()
{
static
function
clear
C
ounter
()
{
self
::
$count
=
0
;
}
}
...
...
@@ -39,7 +39,7 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
$this
->
set
=
set
(
array
(
1
,
2
));
}
function
test
_a
dd
()
{
function
test
A
dd
()
{
$this
->
set
->
add
(
3
);
$this
->
assertEquals
(
$this
->
set
,
set
(
array
(
1
,
2
,
3
)));
}
...
...
@@ -47,18 +47,18 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException InvalidArgumentException
*/
function
test
_insert_e
rror
()
{
function
test
InsertE
rror
()
{
set
(
array
(
'foo'
=>
1
))
->
insert
(
2
,
'foo'
);
}
function
test
_insert_s
uccess
()
{
function
test
InsertS
uccess
()
{
$this
->
set
->
insert
(
4
,
1
);
$this
->
assertEquals
(
$this
->
set
,
set
(
array
(
1
,
4
,
2
)));
$this
->
set
->
insert
(
5
,
0
);
$this
->
assertEquals
(
$this
->
set
,
set
(
array
(
5
,
1
,
4
,
2
)));
}
function
test
_a
ll
()
{
function
test
A
ll
()
{
$this
->
assertEquals
(
set
()
->
all
(),
array
());
$this
->
assertEquals
(
set
(
array
())
->
all
(),
array
());
$this
->
assertEquals
(
set
(
array
(
1
))
->
all
(),
array
(
1
));
...
...
@@ -68,108 +68,108 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException OutOfBoundsException
*/
function
test
_last_e
mpty
()
{
function
test
LastE
mpty
()
{
set
()
->
last
();
}
function
test
_l
ast
()
{
function
test
L
ast
()
{
$this
->
assertEquals
(
$this
->
set
->
last
(),
2
);
}
/**
* @expectedException OutOfBoundsException
*/
function
test
_first_e
mpty
()
{
function
test
FirstE
mpty
()
{
set
()
->
first
();
}
function
test
_f
irst
()
{
function
test
F
irst
()
{
$this
->
assertEquals
(
$this
->
set
->
first
(),
1
);
}
function
test
_c
ount
()
{
function
test
C
ount
()
{
$this
->
assertEquals
(
set
()
->
count
(),
0
);
$this
->
assertEquals
(
set
(
array
())
->
count
(),
0
);
$this
->
assertEquals
(
set
(
array
(
1
))
->
count
(),
1
);
$this
->
assertEquals
(
set
(
array
(
1
,
2
))
->
count
(),
2
);
}
function
test
_index_e
xists
()
{
$this
->
assertTrue
(
$this
->
set
->
index
_e
xists
(
1
));
$this
->
assertTrue
(
set
(
array
(
'foo'
=>
'bar'
))
->
index
_e
xists
(
'foo'
));
function
test
IndexE
xists
()
{
$this
->
assertTrue
(
$this
->
set
->
index
E
xists
(
1
));
$this
->
assertTrue
(
set
(
array
(
'foo'
=>
'bar'
))
->
index
E
xists
(
'foo'
));
}
function
test
_g
et
()
{
function
test
G
et
()
{
$this
->
assertEquals
(
$this
->
set
->
get
(
0
),
1
);
$this
->
assertEquals
(
$this
->
set
->
get
(
1
),
2
);
$this
->
assertEquals
(
set
(
array
(
'foo'
=>
'bar'
))
->
get
(
'foo'
),
'bar'
);
}
function
test
_delete_i
ndex
()
{
$this
->
set
->
delete
_i
ndex
(
0
);
function
test
DeleteI
ndex
()
{
$this
->
set
->
delete
I
ndex
(
0
);
$this
->
assertEquals
(
$this
->
set
,
set
(
array
(
1
=>
2
)));
}
function
test
_d
elete
()
{
function
test
D
elete
()
{
$this
->
set
->
delete
(
1
);
$this
->
assertEquals
(
$this
->
set
,
set
(
array
(
1
=>
2
)));
}
function
assert
_set_e
quals
(
array
$expected_items
,
$set
)
{
function
assert
SetE
quals
(
array
$expected_items
,
$set
)
{
$this
->
assertAttributeEquals
(
$expected_items
,
'items'
,
$set
);
}
function
test
_u
niques
()
{
$this
->
assert
_set_e
quals
(
array
(
1
,
2
),
set
(
array
(
1
,
2
,
2
))
->
uniques
());
$this
->
assert
_set_e
quals
(
array
(
2
,
1
),
set
(
array
(
2
,
1
,
2
))
->
uniques
());
$this
->
assert
_set_e
quals
(
array
(
2
,
1
),
set
(
array
(
2
,
2
,
1
))
->
uniques
());
function
test
U
niques
()
{
$this
->
assert
SetE
quals
(
array
(
1
,
2
),
set
(
array
(
1
,
2
,
2
))
->
uniques
());
$this
->
assert
SetE
quals
(
array
(
2
,
1
),
set
(
array
(
2
,
1
,
2
))
->
uniques
());
$this
->
assert
SetE
quals
(
array
(
2
,
1
),
set
(
array
(
2
,
2
,
1
))
->
uniques
());
}
function
set
_i
tems
(
$collection
,
$items
,
$clone
)
{
$rm
=
new
ReflectionMethod
(
$collection
,
'set
_i
tems'
);
function
set
I
tems
(
$collection
,
$items
,
$clone
)
{
$rm
=
new
ReflectionMethod
(
$collection
,
'set
I
tems'
);
$rm
->
setAccessible
(
true
);
return
$rm
->
invoke
(
$collection
,
$items
,
$clone
);
}
function
test
_set_items_c
lone
()
{
$result
=
$this
->
set
_i
tems
(
$this
->
set
,
array
(
3
,
4
),
true
);
$this
->
assert
_set_e
quals
(
array
(
1
,
2
),
$this
->
set
);
$this
->
assert
_set_e
quals
(
array
(
3
,
4
),
$result
);
function
test
SetItemsC
lone
()
{
$result
=
$this
->
set
I
tems
(
$this
->
set
,
array
(
3
,
4
),
true
);
$this
->
assert
SetE
quals
(
array
(
1
,
2
),
$this
->
set
);
$this
->
assert
SetE
quals
(
array
(
3
,
4
),
$result
);
$this
->
assertNotSame
(
$this
->
set
,
$result
);
}
function
test
_set_items_no_c
lone
()
{
$result
=
$this
->
set
_i
tems
(
$this
->
set
,
array
(
3
,
4
),
false
);
function
test
SetItemsNoC
lone
()
{
$result
=
$this
->
set
I
tems
(
$this
->
set
,
array
(
3
,
4
),
false
);
$this
->
assertSame
(
$this
->
set
,
$result
);
}
/**
* @depends test
_set_items_c
lone
* @depends test
SetItemsC
lone
*/
function
test
_f
ilter
()
{
function
test
F
ilter
()
{
$smaller_than_five
=
function
(
$number
)
{
return
$number
<
5
;
};
$this
->
assert
_set_e
quals
(
array
(
2
,
4
,
1
,
4
),
set
(
array
(
2
,
7
,
4
,
7
,
1
,
8
,
4
,
5
))
->
filter
(
$smaller_than_five
));
$this
->
assert
SetE
quals
(
array
(
2
,
4
,
1
,
4
),
set
(
array
(
2
,
7
,
4
,
7
,
1
,
8
,
4
,
5
))
->
filter
(
$smaller_than_five
));
}
/**
* @depends test
_f
ilter
* @depends test
F
ilter
*/
function
test
_find_s
uccess
()
{
function
test
FindS
uccess
()
{
$items
=
array
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
),
array
(
'foo'
=>
'baz'
,
'bar'
=>
'foo'
),
std_object
(
array
(
'foo'
=>
'bar'
,
'baz'
=>
'bar'
)),
);
$this
->
assert
_set_e
quals
(
array
(
$items
[
1
]),
set
(
$items
)
->
find
(
array
(
'foo'
=>
'baz'
)));
$this
->
assert
_set_e
quals
(
array
(
$items
[
0
],
$items
[
2
]),
set
(
$items
)
->
find
(
array
(
'foo'
=>
'bar'
)));
$this
->
assert
SetE
quals
(
array
(
$items
[
1
]),
set
(
$items
)
->
find
(
array
(
'foo'
=>
'baz'
)));
$this
->
assert
SetE
quals
(
array
(
$items
[
0
],
$items
[
2
]),
set
(
$items
)
->
find
(
array
(
'foo'
=>
'bar'
)));
}
/**
* @depends test
_find_s
uccess
* @depends test
FindS
uccess
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Collection::find encountered a non-object and non-array item "foobar".
*/
function
test
_find_f
ailure
()
{
function
test
FindF
ailure
()
{
$items
=
array
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
),
'foobar'
,
...
...
@@ -177,47 +177,47 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
set
(
$items
)
->
find
(
array
(
'foo'
=>
'bar'
));
}
function
test
_get_attribute_s
imple
()
{
IdObject
::
clear
_c
ounter
();
function
test
GetAttributeS
imple
()
{
IdObject
::
clear
C
ounter
();
$set
=
set
(
array
(
new
IdObject
(),
new
IdObject
(),
new
IdObject
()));
$this
->
assertEquals
(
array
(
1
,
2
,
3
),
$set
->
get
_a
ttribute
(
'id'
));
$this
->
assertEquals
(
array
(
1
,
2
,
3
),
$set
->
get
A
ttribute
(
'id'
));
}
/**
* @depends test
_get_attribute_s
imple
* @depends test
GetAttributeS
imple
*/
function
test
_get_attribute_i
ndices
()
{
IdObject
::
clear
_c
ounter
();
function
test
GetAttributeI
ndices
()
{
IdObject
::
clear
C
ounter
();
$set
=
set
(
array
(
'foo'
=>
new
IdObject
(),
'bar'
=>
new
IdObject
(),
'baz'
=>
new
IdObject
()));
$this
->
assertEquals
(
array
(
'foo'
=>
1
,
'bar'
=>
2
,
'baz'
=>
3
),
$set
->
get
_a
ttribute
(
'id'
));
$this
->
assertEquals
(
array
(
'foo'
=>
1
,
'bar'
=>
2
,
'baz'
=>
3
),
$set
->
get
A
ttribute
(
'id'
));
}
/**
* @depends test
_a
ll
* @depends test
_set_items_c
lone
* @depends test
A
ll
* @depends test
SetItemsC
lone
*/
function
test
_index_b
y
()
{
IdObject
::
clear
_c
ounter
();
function
test
IndexB
y
()
{
IdObject
::
clear
C
ounter
();
$set
=
set
(
array
(
new
IdObject
(
'foo'
),
new
IdObject
(
'bar'
),
new
IdObject
(
'baz'
)));
list
(
$foo
,
$bar
,
$baz
)
=
$set
->
all
();
$this
->
assert
_set_equals
(
array
(
'foo'
=>
$foo
,
'bar'
=>
$bar
,
'baz'
=>
$baz
),
$set
->
index_b
y
(
'foo'
));
$this
->
assert
SetEquals
(
array
(
'foo'
=>
$foo
,
'bar'
=>
$bar
,
'baz'
=>
$baz
),
$set
->
indexB
y
(
'foo'
));
}
/**
* @depends test
_set_items_c
lone
* @depends test
SetItemsC
lone
*/
function
test
_m
ap
()
{
function
test
M
ap
()
{
$plus_five
=
function
(
$number
)
{
return
$number
+
5
;
};
$this
->
assert
_set_e
quals
(
array
(
6
,
7
,
8
),
set
(
array
(
1
,
2
,
3
))
->
map
(
$plus_five
));
$this
->
assert
SetE
quals
(
array
(
6
,
7
,
8
),
set
(
array
(
1
,
2
,
3
))
->
map
(
$plus_five
));
}
/**
* @depends test
_set_items_c
lone
* @depends test
SetItemsC
lone
*/
function
test
_map_m
ethod
()
{
IdObject
::
clear
_c
ounter
();
function
test
MapM
ethod
()
{
IdObject
::
clear
C
ounter
();
$set
=
set
(
array
(
new
IdObject
(),
new
IdObject
(),
new
IdObject
()));
$this
->
assert
_set_equals
(
array
(
1
,
2
,
3
),
$set
->
map_method
(
'get_i
d'
));
$this
->
assert
SetEquals
(
array
(
1
,
2
,
3
),
$set
->
mapMethod
(
'getI
d'
));
}
}
...
...
tests/test_logger.php
View file @
3566402f
...
...
@@ -11,138 +11,138 @@ define('LOGFILE', 'build/temp.log');
class
LoggerTest
extends
PHPUnit_Extensions_OutputTestCase
{
function
setUp
()
{
$this
->
logger
=
new
Logger
();
$this
->
logger
->
set
_p
roperty
(
'name'
,
NAME
);
$this
->
logger
->
set
_f
ormat
(
FORMAT
);
$this
->
logger
->
set
P
roperty
(
'name'
,
NAME
);
$this
->
logger
->
set
F
ormat
(
FORMAT
);
is_dir
(
'build'
)
||
mkdir
(
'build'
);
}
function
assert
_d
umps
(
$expected
)
{
function
assert
D
umps
(
$expected
)
{
$this
->
assertEquals
(
$this
->
logger
->
dumps
(),
$expected
);
}
function
test
_set_d
irectory
()
{
$this
->
logger
->
set
_d
irectory
(
'logs'
);
function
test
SetD
irectory
()
{
$this
->
logger
->
set
D
irectory
(
'logs'
);
$this
->
assertAttributeEquals
(
'logs/'
,
'log_directory'
,
$this
->
logger
);
$this
->
logger
->
set
_d
irectory
(
'logs/'
);
$this
->
logger
->
set
D
irectory
(
'logs/'
);
$this
->
assertAttributeEquals
(
'logs/'
,
'log_directory'
,
$this
->
logger
);
}
function
test
_set_f
ormat
()
{
$this
->
logger
->
set
_f
ormat
(
'foo'
);
function
test
SetF
ormat
()
{
$this
->
logger
->
set
F
ormat
(
'foo'
);
$this
->
assertAttributeEquals
(
'foo'
,
'format'
,
$this
->
logger
);
}
function
test
_set_dump_format_s
uccess
()
{
$this
->
logger
->
set
_dump_f
ormat
(
'html'
);
function
test
SetDumpFormatS
uccess
()
{
$this
->
logger
->
set
DumpF
ormat
(
'html'
);
$this
->
assertAttributeEquals
(
'html'
,
'dump_format'
,
$this
->
logger
);
}
/**
* @expectedException InvalidArgumentException
*/
function
test
_set_dump_format_f
ailure
()
{
$this
->
logger
->
set
_dump_f
ormat
(
'foo'
);
function
test
SetDumpFormatF
ailure
()
{
$this
->
logger
->
set
DumpF
ormat
(
'foo'
);
}
function
test
_get_f
ormat
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
_f
ormat
(),
FORMAT
);
function
test
GetF
ormat
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
F
ormat
(),
FORMAT
);
}
function
test
_get_l
evel
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
_l
evel
(),
Logger
::
WARNING
);
$this
->
assertEquals
(
$this
->
logger
->
get
_level_n
ame
(),
'WARNING'
);
function
test
GetL
evel
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
L
evel
(),
Logger
::
WARNING
);
$this
->
assertEquals
(
$this
->
logger
->
get
LevelN
ame
(),
'WARNING'
);
}
/**
* @depends test
_get_l
evel
* @depends test
GetL
evel
*/
function
test
_set_l
evel
()
{
$this
->
logger
->
set
_l
evel
(
'info'
);
$this
->
assertEquals
(
$this
->
logger
->
get
_l
evel
(),
Logger
::
INFO
);
$this
->
logger
->
set
_l
evel
(
'DEBUG'
);
$this
->
assertEquals
(
$this
->
logger
->
get
_l
evel
(),
Logger
::
DEBUG
);
$this
->
logger
->
set
_l
evel
(
'WaRnInG'
);
$this
->
assertEquals
(
$this
->
logger
->
get
_l
evel
(),
Logger
::
WARNING
);
$this
->
logger
->
set
_l
evel
(
Logger
::
ERROR
);
$this
->
assertEquals
(
$this
->
logger
->
get
_l
evel
(),
Logger
::
ERROR
);
}
function
test
_f
ormat
()
{
function
test
SetL
evel
()
{
$this
->
logger
->
set
L
evel
(
'info'
);
$this
->
assertEquals
(
$this
->
logger
->
get
L
evel
(),
Logger
::
INFO
);
$this
->
logger
->
set
L
evel
(
'DEBUG'
);
$this
->
assertEquals
(
$this
->
logger
->
get
L
evel
(),
Logger
::
DEBUG
);
$this
->
logger
->
set
L
evel
(
'WaRnInG'
);
$this
->
assertEquals
(
$this
->
logger
->
get
L
evel
(),
Logger
::
WARNING
);
$this
->
logger
->
set
L
evel
(
Logger
::
ERROR
);
$this
->
assertEquals
(
$this
->
logger
->
get
L
evel
(),
Logger
::
ERROR
);
}
function
test
F
ormat
()
{
$this
->
logger
->
error
(
'test message'
);
$this
->
assert
_d
umps
(
'ERROR: test message'
);
$this
->
assert
D
umps
(
'ERROR: test message'
);
}
function
test
_set_p
roperty
()
{
$this
->
logger
->
set
_p
roperty
(
'name'
,
'Logger'
);
$this
->
assertEquals
(
$this
->
logger
->
get
_formatted_p
roperty
(
'name'
),
'Logger'
);
function
test
SetP
roperty
()
{
$this
->
logger
->
set
P
roperty
(
'name'
,
'Logger'
);
$this
->
assertEquals
(
$this
->
logger
->
get
FormattedP
roperty
(
'name'
),
'Logger'
);
}
/**
* @depends test
_f
ormat
* @depends test
F
ormat
*/
function
test
_c
lear
()
{
function
test
C
lear
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
clear
();
$this
->
assert
_d
umps
(
''
);
$this
->
assert
D
umps
(
''
);
}
/**
* @depends test
_set_l
evel
* @depends test
_c
lear
* @depends test
SetL
evel
* @depends test
C
lear
*/
function
test
_process_l
evel
()
{
function
test
ProcessL
evel
()
{
$this
->
logger
->
info
(
'test message'
);
$this
->
assert
_d
umps
(
''
);
$this
->
assert
D
umps
(
''
);
$this
->
logger
->
warning
(
'test message'
);
$this
->
assert
_d
umps
(
'WARNING: test message'
);
$this
->
assert
D
umps
(
'WARNING: test message'
);
$this
->
logger
->
critical
(
'test message'
);
$this
->
assert
_d
umps
(
"WARNING: test message
\n
CRITICAL: test message"
);
$this
->
assert
D
umps
(
"WARNING: test message
\n
CRITICAL: test message"
);
$this
->
logger
->
clear
();
$this
->
logger
->
set
_l
evel
(
'debug'
);
$this
->
logger
->
set
L
evel
(
'debug'
);
$this
->
logger
->
debug
(
'test message'
);
$this
->
assert
_d
umps
(
'DEBUG: test message'
);
$this
->
assert
D
umps
(
'DEBUG: test message'
);
}
function
test
_get_formatted_p
roperty
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
_formatted_p
roperty
(
'name'
),
NAME
);
$this
->
assertEquals
(
$this
->
logger
->
get
_formatted_p
roperty
(
'loglevel'
),
'WARNING'
);
function
test
GetFormattedP
roperty
()
{
$this
->
assertEquals
(
$this
->
logger
->
get
FormattedP
roperty
(
'name'
),
NAME
);
$this
->
assertEquals
(
$this
->
logger
->
get
FormattedP
roperty
(
'loglevel'
),
'WARNING'
);
$this
->
assertRegExp
(
'/^\d{2}-\d{2}-\d{4}$/'
,
$this
->
logger
->
get
_formatted_p
roperty
(
'date'
));
$this
->
logger
->
get
FormattedP
roperty
(
'date'
));
$this
->
assertRegExp
(
'/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}$/'
,
$this
->
logger
->
get
_formatted_p
roperty
(
'datetime'
));
$this
->
logger
->
get
FormattedP
roperty
(
'datetime'
));
$this
->
assertRegExp
(
'/^\d{2}:\d{2}:\d{2}$/'
,
$this
->
logger
->
get
_formatted_p
roperty
(
'time'
));
$this
->
logger
->
get
FormattedP
roperty
(
'time'
));
$this
->
setExpectedException
(
'\InvalidArgumentException'
);
$this
->
logger
->
get
_formatted_p
roperty
(
'foo'
);
$this
->
logger
->
get
FormattedP
roperty
(
'foo'
);
}
function
test
_dumps_property_f
ormat
()
{
function
test
DumpsPropertyF
ormat
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
set
_f
ormat
(
'%(name): %(level): %(message)'
);
$this
->
assert
_d
umps
(
NAME
.
': WARNING: test message'
);
$this
->
logger
->
set
F
ormat
(
'%(name): %(level): %(message)'
);
$this
->
assert
D
umps
(
NAME
.
': WARNING: test message'
);
}
/**
* @depends test
_process_l
evel
* @depends test
ProcessL
evel
*/
function
test
_dump_p
lain
()
{
function
test
DumpP
lain
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
expectOutputString
(
'WARNING: test message'
);
$this
->
logger
->
dump
();
}
/**
* @depends test
_process_l
evel
* @depends test
ProcessL
evel
*/
function
test
_dump_h
tml
()
{
function
test
DumpH
tml
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
set
_dump_f
ormat
(
'html'
);
$this
->
logger
->
set
DumpF
ormat
(
'html'
);
$this
->
expectOutputString
(
'<strong>Log:</strong><br /><pre>WARNING: test message</pre>'
);
$this
->
logger
->
dump
();
}
function
test
_s
ave
()
{
function
test
S
ave
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
save
(
LOGFILE
);
$this
->
assertStringEqualsFile
(
LOGFILE
,
'WARNING: test message'
);
...
...
@@ -152,30 +152,30 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
unlink
(
LOGFILE
);
}
function
find
_l
ogfile
()
{
function
find
L
ogfile
()
{
$files
=
scandir
(
LOGDIR
);
$this
->
assertEquals
(
3
,
count
(
$files
));
return
$files
[
2
];
}
/**
* @depends test
_s
ave
* @depends test
S
ave
*/
function
test
_dump_file_r
egular
()
{
$this
->
logger
->
set
_d
irectory
(
LOGDIR
);
$this
->
logger
->
set
_dump_f
ormat
(
'file'
);
function
test
DumpFileR
egular
()
{
$this
->
logger
->
set
D
irectory
(
LOGDIR
);
$this
->
logger
->
set
DumpF
ormat
(
'file'
);
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
dump
();
$filename
=
$this
->
find
_l
ogfile
();
$filename
=
$this
->
find
L
ogfile
();
$this
->
assertStringEqualsFile
(
LOGDIR
.
$filename
,
'WARNING: test message'
);
unlink
(
LOGDIR
.
$filename
);
$this
->
assertRegExp
(
'/^log_\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2}.log$/'
,
$filename
);
}
function
test
_handle_e
xception
()
{
$this
->
logger
->
set
_dump_f
ormat
(
'none'
);
$this
->
logger
->
handle
_e
xception
(
new
RuntimeException
(
'test message'
));
function
test
HandleE
xception
()
{
$this
->
logger
->
set
DumpF
ormat
(
'none'
);
$this
->
logger
->
handle
E
xception
(
new
RuntimeException
(
'test message'
));
$this
->
assertNotEquals
(
$this
->
logger
->
dumps
(),
''
);
}
}
...
...
tests/test_node.php
View file @
3566402f
...
...
@@ -10,90 +10,90 @@ class NodeTest extends PHPUnit_Framework_TestCase {
$this
->
root
=
new
Node
(
'test node'
);
}
function
test
_get_i
d
()
{
$this
->
assertEquals
(
$this
->
root
->
get
_i
d
(),
1
);
$this
->
assertEquals
(
Node
::
create
(
''
)
->
get
_i
d
(),
2
);
function
test
GetI
d
()
{
$this
->
assertEquals
(
$this
->
root
->
get
I
d
(),
1
);
$this
->
assertEquals
(
Node
::
create
(
''
)
->
get
I
d
(),
2
);
}
function
test
_get_n
ame
()
{
$this
->
assertEquals
(
$this
->
root
->
get
_n
ame
(),
'test node'
);
$this
->
assertEquals
(
Node
::
create
(
'second node'
)
->
get
_n
ame
(),
'second node'
);
function
test
GetN
ame
()
{
$this
->
assertEquals
(
$this
->
root
->
get
N
ame
(),
'test node'
);
$this
->
assertEquals
(
Node
::
create
(
'second node'
)
->
get
N
ame
(),
'second node'
);
}
function
test
_get_p
arent
()
{
$this
->
assertNull
(
$this
->
root
->
get
_p
arent
());
$this
->
assertSame
(
Node
::
create
(
''
,
$this
->
root
)
->
get
_p
arent
(),
$this
->
root
);
function
test
GetP
arent
()
{
$this
->
assertNull
(
$this
->
root
->
get
P
arent
());
$this
->
assertSame
(
Node
::
create
(
''
,
$this
->
root
)
->
get
P
arent
(),
$this
->
root
);
}
function
test
_i
s
()
{
function
test
I
s
()
{
$mirror
=
$this
->
root
;
$this
->
assertTrue
(
$mirror
->
is
(
$this
->
root
));
$this
->
assertFalse
(
Node
::
create
(
''
)
->
is
(
$this
->
root
));
}
function
test
_is_r
oot
()
{
$this
->
assertTrue
(
$this
->
root
->
is
_r
oot
());
$this
->
assertFalse
(
Node
::
create
(
''
,
$this
->
root
)
->
is
_r
oot
());
function
test
IsR
oot
()
{
$this
->
assertTrue
(
$this
->
root
->
is
R
oot
());
$this
->
assertFalse
(
Node
::
create
(
''
,
$this
->
root
)
->
is
R
oot
());
}
function
test
_add_c
hild
()
{
function
test
AddC
hild
()
{
$node
=
new
Node
(
''
);
$this
->
root
->
add
_c
hild
(
$node
);
$this
->
root
->
add
C
hild
(
$node
);
$this
->
assertAttributeEquals
(
array
(
$node
),
'children'
,
$this
->
root
);
$this
->
assertSame
(
$node
->
get
_p
arent
(),
$this
->
root
);
$this
->
assertSame
(
$node
->
get
P
arent
(),
$this
->
root
);
}
/**
* @depends test
_add_c
hild
* @depends test
AddC
hild
*/
function
test
_get_c
hildren
()
{
$this
->
assertEquals
(
$this
->
root
->
get
_c
hildren
(),
array
());
function
test
GetC
hildren
()
{
$this
->
assertEquals
(
$this
->
root
->
get
C
hildren
(),
array
());
$node
=
new
Node
(
''
);
$this
->
root
->
add
_c
hild
(
$node
);
$this
->
assertSame
(
$this
->
root
->
get
_c
hildren
(),
array
(
$node
));
$this
->
root
->
add
C
hild
(
$node
);
$this
->
assertSame
(
$this
->
root
->
get
C
hildren
(),
array
(
$node
));
}
function
test
_add_child_no_set_p
arent
()
{
function
test
AddChildNoSetP
arent
()
{
$node
=
new
Node
(
''
);
$this
->
root
->
add
_c
hild
(
$node
,
false
);
$this
->
root
->
add
C
hild
(
$node
,
false
);
$this
->
assertAttributeEquals
(
array
(
$node
),
'children'
,
$this
->
root
);
$this
->
assertNull
(
$node
->
get
_p
arent
());
$this
->
assertNull
(
$node
->
get
P
arent
());
}
/**
* @depends test
_add_c
hild
* @depends test
AddC
hild
*/
function
test
_is_l
eaf
()
{
function
test
IsL
eaf
()
{
$node
=
new
Node
(
''
);
$this
->
root
->
add
_c
hild
(
$node
);
$this
->
assertTrue
(
$node
->
is
_l
eaf
());
$this
->
assertFalse
(
$this
->
root
->
is
_l
eaf
());
$this
->
root
->
add
C
hild
(
$node
);
$this
->
assertTrue
(
$node
->
is
L
eaf
());
$this
->
assertFalse
(
$this
->
root
->
is
L
eaf
());
}
/**
* @depends test
_add_c
hild
* @depends test
AddC
hild
*/
function
test
_a
dd
()
{
function
test
A
dd
()
{
$node
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$this
->
assertEquals
(
$node
->
get
_n
ame
(),
'name'
);
$this
->
assertEquals
(
$node
->
get
N
ame
(),
'name'
);
$this
->
assertEquals
(
$node
->
get
(
'foo'
),
'bar'
);
$this
->
assertSame
(
$node
->
get
_p
arent
(),
$this
->
root
);
$this
->
assertSame
(
$node
->
get
P
arent
(),
$this
->
root
);
}
/**
* @depends test
_a
dd
* @depends test
A
dd
*/
function
test
_remove_c
hild
()
{
function
test
RemoveC
hild
()
{
$node1
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$node2
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$this
->
root
->
remove
_c
hild
(
$node2
);
$this
->
root
->
remove
C
hild
(
$node2
);
$this
->
assertAttributeSame
(
array
(
$node1
),
'children'
,
$this
->
root
);
}
/**
* @depends test
_remove_c
hild
* @depends test
RemoveC
hild
*/
function
test
_remove_l
eaf
()
{
function
test
RemoveL
eaf
()
{
$node1
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$node2
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$node1
->
remove
();
...
...
@@ -101,39 +101,39 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test
_remove_l
eaf
* @depends test
RemoveL
eaf
*/
function
test
_remove_n
ode
()
{
function
test
RemoveN
ode
()
{
$node
=
$this
->
root
->
add
(
'node'
);
$leaf
=
$node
->
add
(
'leaf'
);
$node
->
remove
();
$this
->
assertAttributeEquals
(
array
(),
'children'
,
$this
->
root
);
$this
->
assertNull
(
$leaf
->
get
_p
arent
());
$this
->
assertNull
(
$leaf
->
get
P
arent
());
}
/**
* @depends test
_remove_c
hild
* @depends test
RemoveC
hild
* @expectedException \RuntimeException
*/
function
test
_remove_r
oot
()
{
function
test
RemoveR
oot
()
{
$node1
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$node2
=
$this
->
root
->
add
(
'name'
,
array
(
'foo'
=>
'bar'
));
$this
->
root
->
remove
();
$this
->
assertAttributeSame
(
array
(
$node2
),
'children'
,
$this
->
root
);
}
function
test
_set_s
ingle
()
{
function
test
SetS
ingle
()
{
$this
->
root
->
set
(
'foo'
,
'bar'
);
$this
->
assertAttributeEquals
(
array
(
'foo'
=>
'bar'
),
'variables'
,
$this
->
root
);
$this
->
root
->
set
(
'bar'
,
'baz'
);
$this
->
assertAttributeEquals
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
),
'variables'
,
$this
->
root
);
}
function
test
_set_r
eturn
()
{
function
test
SetR
eturn
()
{
$this
->
assertSame
(
$this
->
root
->
set
(
'foo'
,
'bar'
),
$this
->
root
);
}
function
test
_set_m
ultiple
()
{
function
test
SetM
ultiple
()
{
$this
->
root
->
set
(
array
(
'foo'
=>
'bar'
));
$this
->
assertAttributeEquals
(
array
(
'foo'
=>
'bar'
),
'variables'
,
$this
->
root
);
$this
->
root
->
set
(
array
(
'bar'
=>
'baz'
));
...
...
@@ -141,7 +141,7 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test
_set_s
ingle
* @depends test
SetS
ingle
*/
function
test___set
()
{
$this
->
root
->
foo
=
'bar'
;
...
...
@@ -151,16 +151,16 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test
_set_m
ultiple
* @depends test
SetM
ultiple
*/
function
test
_get_d
irect
()
{
function
test
GetD
irect
()
{
$this
->
root
->
set
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
));
$this
->
assertEquals
(
$this
->
root
->
get
(
'foo'
),
'bar'
);
$this
->
assertEquals
(
$this
->
root
->
get
(
'bar'
),
'baz'
);
}
/**
* @depends test
_get_d
irect
* @depends test
GetD
irect
*/
function
test___get
()
{
$this
->
root
->
set
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
));
...
...
@@ -169,22 +169,22 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test
_set_s
ingle
* @depends test
SetS
ingle
*/
function
test
_get_a
ncestor
()
{
function
test
GetA
ncestor
()
{
$this
->
root
->
set
(
'foo'
,
'bar'
);
$node
=
$this
->
root
->
add
(
''
);
$this
->
assertEquals
(
$node
->
get
(
'foo'
),
'bar'
);
}
function
test
_get_f
ailure
()
{
function
test
GetF
ailure
()
{
$this
->
assertNull
(
$this
->
root
->
get
(
'foo'
));
}
/**
* @depends test
_get_n
ame
* @depends test
GetN
ame
*/
function
test
_f
ind
()
{
function
test
F
ind
()
{
$node1
=
$this
->
root
->
add
(
'foo'
);
$node2
=
$this
->
root
->
add
(
'bar'
);
$node3
=
$this
->
root
->
add
(
'foo'
);
...
...
@@ -192,34 +192,34 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test
_set_m
ultiple
* @depends test
SetM
ultiple
*/
function
test
_copy_s
imple
()
{
function
test
CopyS
imple
()
{
$copy
=
$this
->
root
->
copy
();
$this
->
assertEquals
(
$this
->
root
,
$copy
);
$this
->
assertNotSame
(
$this
->
root
,
$copy
);
}
/**
* @depends test
_copy_s
imple
* @depends test
CopyS
imple
*/
function
test
_copy_s
hallow
()
{
function
test
CopyS
hallow
()
{
$child
=
$this
->
root
->
add
(
''
);
$copy
=
$this
->
root
->
copy
();
$this
->
assertAttributeSame
(
array
(
$child
),
'children'
,
$copy
);
}
/**
* @depends test
_get_c
hildren
* @depends test
_copy_s
imple
* @depends test
GetC
hildren
* @depends test
CopyS
imple
*/
function
test
_copy_d
eep
()
{
function
test
CopyD
eep
()
{
$child
=
$this
->
root
->
add
(
'foo'
);
$copy
=
$this
->
root
->
copy
(
true
);
$copy_children
=
$copy
->
get
_c
hildren
();
$copy_children
=
$copy
->
get
C
hildren
();
$child_copy
=
reset
(
$copy_children
);
$this
->
assertNotSame
(
$copy_children
,
$this
->
root
->
get
_c
hildren
());
$this
->
assertSame
(
$child_copy
->
get
_p
arent
(),
$copy
);
$this
->
assertNotSame
(
$copy_children
,
$this
->
root
->
get
C
hildren
());
$this
->
assertSame
(
$child_copy
->
get
P
arent
(),
$copy
);
}
}
...
...
tests/test_router.php
View file @
3566402f
...
...
@@ -24,29 +24,29 @@ class RouterTest extends PHPUnit_Framework_TestCase {
));
}
function
test
_call_handler_s
uccess
()
{
$this
->
assertEquals
(
true
,
$this
->
router
->
call
_h
andler
(
'foo'
));
$this
->
assertEquals
(
'bar'
,
$this
->
router
->
call
_h
andler
(
'bar'
));
$this
->
assertEquals
(
'baz'
,
$this
->
router
->
call
_h
andler
(
'baz'
));
$this
->
assertEquals
(
'barbaz'
,
$this
->
router
->
call
_h
andler
(
'bazbar'
));
function
test
CallHandlerS
uccess
()
{
$this
->
assertEquals
(
true
,
$this
->
router
->
call
H
andler
(
'foo'
));
$this
->
assertEquals
(
'bar'
,
$this
->
router
->
call
H
andler
(
'bar'
));
$this
->
assertEquals
(
'baz'
,
$this
->
router
->
call
H
andler
(
'baz'
));
$this
->
assertEquals
(
'barbaz'
,
$this
->
router
->
call
H
andler
(
'bazbar'
));
}
function
test
_call_handler_f
ailure
()
{
$this
->
assertFalse
(
$this
->
router
->
call
_h
andler
(
'barfoo'
));
function
test
CallHandlerF
ailure
()
{
$this
->
assertFalse
(
$this
->
router
->
call
H
andler
(
'barfoo'
));
}
function
test
_call_handler_s
kip
()
{
function
test
CallHandlerS
kip
()
{
$foo
=
'foo'
;
$bar
=
function
()
use
(
&
$foo
)
{
$foo
=
'bar'
;
return
false
;
};
$baz
=
function
()
{
return
;
};
$router
=
new
Router
(
array
(
'.*'
=>
$bar
,
'baz'
=>
$baz
));
$router
->
call
_h
andler
(
'baz'
);
$router
->
call
H
andler
(
'baz'
);
$this
->
assertEquals
(
'bar'
,
$foo
);
}
function
test
_add_r
oute
()
{
$this
->
router
->
add
_r
oute
(
'(foobar)'
,
'test_handler_arg'
);
$this
->
assertEquals
(
'foobar'
,
$this
->
router
->
call
_h
andler
(
'foobar'
));
function
test
AddR
oute
()
{
$this
->
router
->
add
R
oute
(
'(foobar)'
,
'test_handler_arg'
);
$this
->
assertEquals
(
'foobar'
,
$this
->
router
->
call
H
andler
(
'foobar'
));
}
}
...
...
tests/test_template.php
View file @
3566402f
...
...
@@ -24,10 +24,10 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
const
INTERNATIONALIZATION_STRING
=
'Itrntinliztin'
;
/**
* @depends test
_add_root_s
uccess
* @depends test
AddRootS
uccess
*/
function
setUp
()
{
Template
::
set
_r
oot
(
TEMPLATES_DIR
);
Template
::
set
R
oot
(
TEMPLATES_DIR
);
$this
->
tpl
=
new
Template
(
'foo'
);
$this
->
data
=
new
Node
();
...
...
@@ -54,359 +54,359 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage Directory "non_existing_folder/" does not exist.
*/
function
test
_add_root_f
ailure
()
{
Template
::
add
_r
oot
(
'non_existing_folder'
);
function
test
AddRootF
ailure
()
{
Template
::
add
R
oot
(
'non_existing_folder'
);
}
function
assert
_include_path_e
quals
(
$expected
)
{
function
assert
IncludePathE
quals
(
$expected
)
{
$include_path
=
new
ReflectionProperty
(
'webbasics\Template'
,
'include_path'
);
$include_path
->
setAccessible
(
true
);
$this
->
assertEquals
(
$expected
,
$include_path
->
getValue
());
}
function
test
_clear_include_p
ath
()
{
Template
::
clear
_include_p
ath
();
$this
->
assert
_include_path_e
quals
(
array
());
function
test
ClearIncludeP
ath
()
{
Template
::
clear
IncludeP
ath
();
$this
->
assert
IncludePathE
quals
(
array
());
}
/**
* @depends test
_clear_include_p
ath
* @depends test
ClearIncludeP
ath
*/
function
test
_add_root_s
uccess
()
{
Template
::
clear
_include_p
ath
();
Template
::
add
_r
oot
(
TEMPLATES_DIR
);
$this
->
assert
_include_path_e
quals
(
array
(
TEMPLATES_DIR
));
Template
::
add
_r
oot
(
'tests/_files'
);
$this
->
assert
_include_path_e
quals
(
array
(
TEMPLATES_DIR
,
'tests/_files/'
));
function
test
AddRootS
uccess
()
{
Template
::
clear
IncludeP
ath
();
Template
::
add
R
oot
(
TEMPLATES_DIR
);
$this
->
assert
IncludePathE
quals
(
array
(
TEMPLATES_DIR
));
Template
::
add
R
oot
(
'tests/_files'
);
$this
->
assert
IncludePathE
quals
(
array
(
TEMPLATES_DIR
,
'tests/_files/'
));
}
/**
* @depends test
_add_root_s
uccess
* @depends test
AddRootS
uccess
*/
function
test
_set_r
oot
()
{
Template
::
clear
_include_p
ath
();
Template
::
add
_r
oot
(
TEMPLATES_DIR
);
Template
::
add
_r
oot
(
'tests/_files'
);
Template
::
set
_r
oot
(
TEMPLATES_DIR
);
$this
->
assert
_include_path_e
quals
(
array
(
TEMPLATES_DIR
));
function
test
SetR
oot
()
{
Template
::
clear
IncludeP
ath
();
Template
::
add
R
oot
(
TEMPLATES_DIR
);
Template
::
add
R
oot
(
'tests/_files'
);
Template
::
set
R
oot
(
TEMPLATES_DIR
);
$this
->
assert
IncludePathE
quals
(
array
(
TEMPLATES_DIR
));
}
/**
* @expectedException RuntimeException
*/
function
test
_non_existing_t
emplate
()
{
function
test
NonExistingT
emplate
()
{
$bar
=
new
Template
(
'bar'
);
}
function
test
_other_r
oot
()
{
Template
::
add
_r
oot
(
'tests/_files/other_templates'
);
function
test
OtherR
oot
()
{
Template
::
add
R
oot
(
'tests/_files/other_templates'
);
new
Template
(
'bar'
);
}
function
test
_get_p
ath
()
{
$this
->
assertEquals
(
TEMPLATES_DIR
.
'foo.tpl'
,
$this
->
tpl
->
get
_p
ath
());
function
test
GetP
ath
()
{
$this
->
assertEquals
(
TEMPLATES_DIR
.
'foo.tpl'
,
$this
->
tpl
->
get
P
ath
());
}
function
get
_p
roperty
(
$object
,
$property_name
)
{
function
get
P
roperty
(
$object
,
$property_name
)
{
$rp
=
new
ReflectionProperty
(
$object
,
$property_name
);
$rp
->
setAccessible
(
true
);
return
$rp
->
getValue
(
$object
);
}
static
function
strip
_n
ewlines
(
$html
)
{
static
function
strip
N
ewlines
(
$html
)
{
return
str_replace
(
"
\r\n
"
,
"
\n
"
,
$html
);
}
function
assert
_is_html_n
ode
(
$node
,
$content
)
{
$this
->
assertEquals
(
'html'
,
$node
->
get
_n
ame
());
$this
->
assertEquals
(
$content
,
self
::
strip
_n
ewlines
(
$node
->
get
(
'content'
)));
$this
->
assertEquals
(
array
(),
$node
->
get
_c
hildren
());
function
assert
IsHtmlN
ode
(
$node
,
$content
)
{
$this
->
assertEquals
(
'html'
,
$node
->
get
N
ame
());
$this
->
assertEquals
(
$content
,
self
::
strip
N
ewlines
(
$node
->
get
(
'content'
)));
$this
->
assertEquals
(
array
(),
$node
->
get
C
hildren
());
}
function
assert
_is_block_n
ode
(
$node
,
$block_name
,
$child_count
)
{
$this
->
assertEquals
(
'block'
,
$node
->
get
_n
ame
());
function
assert
IsBlockN
ode
(
$node
,
$block_name
,
$child_count
)
{
$this
->
assertEquals
(
'block'
,
$node
->
get
N
ame
());
$this
->
assertSame
(
$block_name
,
$node
->
get
(
'name'
));
$this
->
assertNull
(
$node
->
get
(
'content'
));
$this
->
assertEquals
(
$child_count
,
count
(
$node
->
get
_c
hildren
()));
$this
->
assertEquals
(
$child_count
,
count
(
$node
->
get
C
hildren
()));
}
function
assert
_is_exp_n
ode
(
$node
,
$brackets_content
)
{
$this
->
assertEquals
(
'expression'
,
$node
->
get
_n
ame
());
function
assert
IsExpN
ode
(
$node
,
$brackets_content
)
{
$this
->
assertEquals
(
'expression'
,
$node
->
get
N
ame
());
$this
->
assertEquals
(
$brackets_content
,
$node
->
get
(
'content'
));
$this
->
assertEquals
(
array
(),
$node
->
get
_c
hildren
());
$this
->
assertEquals
(
array
(),
$node
->
get
C
hildren
());
}
function
test
_parse_blocks_s
imple
()
{
$root_block
=
$this
->
get
_p
roperty
(
$this
->
tpl
,
'root_block'
);
$this
->
assert
_is_block_n
ode
(
$root_block
,
null
,
1
);
function
test
ParseBlocksS
imple
()
{
$root_block
=
$this
->
get
P
roperty
(
$this
->
tpl
,
'root_block'
);
$this
->
assert
IsBlockN
ode
(
$root_block
,
null
,
1
);
list
(
$child
)
=
$root_block
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$child
,
'test'
);
list
(
$child
)
=
$root_block
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$child
,
'test'
);
}
/**
* @depends test
_parse_blocks_s
imple
* @depends test
ParseBlocksS
imple
*/
function
test
_parse_blocks_b
locks
()
{
function
test
ParseBlocksB
locks
()
{
$tpl
=
new
Template
(
'blocks'
);
$root_block
=
$this
->
get
_p
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
_is_block_n
ode
(
$root_block
,
null
,
2
);
$root_block
=
$this
->
get
P
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
IsBlockN
ode
(
$root_block
,
null
,
2
);
list
(
$before
,
$foo
)
=
$root_block
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$before
,
''
);
$this
->
assert
_is_block_n
ode
(
$foo
,
'foo'
,
3
);
list
(
$before
,
$foo
)
=
$root_block
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$before
,
''
);
$this
->
assert
IsBlockN
ode
(
$foo
,
'foo'
,
3
);
list
(
$foofoo
,
$bar
,
$foobaz
)
=
$foo
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$foofoo
,
"
\n
foofoo
\n\t
"
);
$this
->
assert
_is_block_n
ode
(
$bar
,
'bar'
,
1
);
$this
->
assert
_is_html_n
ode
(
$foobaz
,
"
\n
foobaz
\n
"
);
list
(
$foofoo
,
$bar
,
$foobaz
)
=
$foo
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$foofoo
,
"
\n
foofoo
\n\t
"
);
$this
->
assert
IsBlockN
ode
(
$bar
,
'bar'
,
1
);
$this
->
assert
IsHtmlN
ode
(
$foobaz
,
"
\n
foobaz
\n
"
);
list
(
$foobar
)
=
$bar
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$foobar
,
"
\n\t
foobar
\n\t
"
);
list
(
$foobar
)
=
$bar
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$foobar
,
"
\n\t
foobar
\n\t
"
);
}
/**
* @depends test
_parse_blocks_b
locks
* @depends test
ParseBlocksB
locks
* @expectedException webbasics\ParseError
* @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
*/
function
test
_parse_blocks_unexpected_e
nd
()
{
function
test
ParseBlocksUnexpectedE
nd
()
{
new
Template
(
'unexpected_end'
);
}
/**
* @depends test
_parse_blocks_b
locks
* @depends test
ParseBlocksB
locks
* @expectedException webbasics\ParseError
* @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
*/
function
test
_parse_blocks_missing_e
nd
()
{
function
test
ParseBlocksMissingE
nd
()
{
new
Template
(
'missing_end'
);
}
/**
* @depends test
_parse_blocks_s
imple
* @depends test
ParseBlocksS
imple
*/
function
test
_parse_blocks_v
ariables
()
{
function
test
ParseBlocksV
ariables
()
{
$tpl
=
new
Template
(
'variables'
);
$root_block
=
$this
->
get
_p
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
_is_block_n
ode
(
$root_block
,
null
,
5
);
$root_block
=
$this
->
get
P
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
IsBlockN
ode
(
$root_block
,
null
,
5
);
list
(
$foo
,
$foobar
,
$bar
,
$foobaz
,
$baz
)
=
$root_block
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$foo
,
"foo
\n
"
);
$this
->
assert
_is_exp_n
ode
(
$foobar
,
'$foobar'
);
$this
->
assert
_is_html_n
ode
(
$bar
,
"
\n
bar
\n
"
);
$this
->
assert
_is_exp_n
ode
(
$foobaz
,
'strtolower($foobaz)'
);
$this
->
assert
_is_html_n
ode
(
$baz
,
"
\n
baz
\n
{
\nno_variable\n
}
"
);
list
(
$foo
,
$foobar
,
$bar
,
$foobaz
,
$baz
)
=
$root_block
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$foo
,
"foo
\n
"
);
$this
->
assert
IsExpN
ode
(
$foobar
,
'$foobar'
);
$this
->
assert
IsHtmlN
ode
(
$bar
,
"
\n
bar
\n
"
);
$this
->
assert
IsExpN
ode
(
$foobaz
,
'strtolower($foobaz)'
);
$this
->
assert
IsHtmlN
ode
(
$baz
,
"
\n
baz
\n
{
\nno_variable\n
}
"
);
}
/**
* @depends test
_parse_blocks_b
locks
* @depends test
_parse_blocks_v
ariables
* @depends test
ParseBlocksB
locks
* @depends test
ParseBlocksV
ariables
*/
function
test
_parse_blocks_f
ull
()
{
function
test
ParseBlocksF
ull
()
{
$tpl
=
new
Template
(
'full'
);
$root_block
=
$this
->
get
_p
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
_is_block_n
ode
(
$root_block
,
null
,
3
);
$root_block
=
$this
->
get
P
roperty
(
$tpl
,
'root_block'
);
$this
->
assert
IsBlockN
ode
(
$root_block
,
null
,
3
);
list
(
$bar
,
$foo
,
$baz
)
=
$root_block
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$bar
,
"bar
\n
"
);
$this
->
assert
_is_block_n
ode
(
$foo
,
'foo'
,
5
);
$this
->
assert
_is_html_n
ode
(
$baz
,
"
\n
baz"
);
list
(
$bar
,
$foo
,
$baz
)
=
$root_block
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$bar
,
"bar
\n
"
);
$this
->
assert
IsBlockN
ode
(
$foo
,
'foo'
,
5
);
$this
->
assert
IsHtmlN
ode
(
$baz
,
"
\n
baz"
);
list
(
$foofoo
,
$bar
,
$first_space
,
$foobaz
,
$second_space
)
=
$foo
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$foofoo
,
"
\n
foofoo
\n\t
"
);
$this
->
assert
_is_block_n
ode
(
$bar
,
'bar'
,
3
);
$this
->
assert
_is_html_n
ode
(
$first_space
,
"
\n
"
);
$this
->
assert
_is_exp_n
ode
(
$foobaz
,
'strtolower($foobaz)'
);
$this
->
assert
_is_html_n
ode
(
$second_space
,
"
\n
"
);
list
(
$foofoo
,
$bar
,
$first_space
,
$foobaz
,
$second_space
)
=
$foo
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$foofoo
,
"
\n
foofoo
\n\t
"
);
$this
->
assert
IsBlockN
ode
(
$bar
,
'bar'
,
3
);
$this
->
assert
IsHtmlN
ode
(
$first_space
,
"
\n
"
);
$this
->
assert
IsExpN
ode
(
$foobaz
,
'strtolower($foobaz)'
);
$this
->
assert
IsHtmlN
ode
(
$second_space
,
"
\n
"
);
list
(
$space_before
,
$foobar
,
$space_after
)
=
$bar
->
get
_c
hildren
();
$this
->
assert
_is_html_n
ode
(
$space_before
,
"
\n\t
"
);
$this
->
assert
_is_exp_n
ode
(
$foobar
,
'$foobar'
);
$this
->
assert
_is_html_n
ode
(
$space_after
,
"
\n\t
"
);
list
(
$space_before
,
$foobar
,
$space_after
)
=
$bar
->
get
C
hildren
();
$this
->
assert
IsHtmlN
ode
(
$space_before
,
"
\n\t
"
);
$this
->
assert
IsExpN
ode
(
$foobar
,
'$foobar'
);
$this
->
assert
IsHtmlN
ode
(
$space_after
,
"
\n\t
"
);
}
function
evaluate
_e
xpression
()
{
function
evaluate
E
xpression
()
{
$args
=
func_get_args
();
$eval
=
new
ReflectionMethod
(
'webbasics\Template'
,
'evaluate
_e
xpression'
);
$eval
=
new
ReflectionMethod
(
'webbasics\Template'
,
'evaluate
E
xpression'
);
$eval
->
setAccessible
(
true
);
return
$eval
->
invokeArgs
(
null
,
$args
);
}
function
assert
_e
valuates
(
$expected
,
$expression
)
{
$this
->
assertEquals
(
$expected
,
$this
->
evaluate
_e
xpression
(
$expression
,
$this
->
data
));
function
assert
E
valuates
(
$expected
,
$expression
)
{
$this
->
assertEquals
(
$expected
,
$this
->
evaluate
E
xpression
(
$expression
,
$this
->
data
));
}
/**
* @expectedException \UnexpectedValueException
*/
function
test
_evaluate_variable_attribute_n
ull
()
{
$this
->
evaluate
_e
xpression
(
'$foobarbaz.foo'
,
$this
->
data
);
function
test
EvaluateVariableAttributeN
ull
()
{
$this
->
evaluate
E
xpression
(
'$foobarbaz.foo'
,
$this
->
data
);
}
/**
* @expectedException \UnexpectedValueException
*/
function
test
_evaluate_variable_attribute_no_such_a
ttribute
()
{
$this
->
evaluate
_e
xpression
(
'$object.foobar'
,
$this
->
data
);
function
test
EvaluateVariableAttributeNoSuchA
ttribute
()
{
$this
->
evaluate
E
xpression
(
'$object.foobar'
,
$this
->
data
);
}
/**
* @expectedException \UnexpectedValueException
*/
function
test
_evaluate_variable_attribute_no_array_or_o
bject
()
{
$this
->
evaluate
_e
xpression
(
'$foo.bar'
,
$this
->
data
);
function
test
EvaluateVariableAttributeNoArrayOrO
bject
()
{
$this
->
evaluate
E
xpression
(
'$foo.bar'
,
$this
->
data
);
}
/**
* @expectedException \UnexpectedValueException
*/
function
test
_evaluate_variable_method_n
ull
()
{
$this
->
evaluate
_e
xpression
(
'$foobarbaz.foo()'
,
$this
->
data
);
function
test
EvaluateVariableMethodN
ull
()
{
$this
->
evaluate
E
xpression
(
'$foobarbaz.foo()'
,
$this
->
data
);
}
/**
* @expectedException \BadMethodCallException
*/
function
test
_evaluate_variable_method_no_such_m
ethod
()
{
$this
->
evaluate
_e
xpression
(
'$object.foo()'
,
$this
->
data
);
function
test
EvaluateVariableMethodNoSuchM
ethod
()
{
$this
->
evaluate
E
xpression
(
'$object.foo()'
,
$this
->
data
);
}
/**
* @expectedException \BadMethodCallException
*/
function
test
_evaluate_variable_method_no_o
bject
()
{
$this
->
evaluate
_e
xpression
(
'$foo.bar()'
,
$this
->
data
);
function
test
EvaluateVariableMethodNoO
bject
()
{
$this
->
evaluate
E
xpression
(
'$foo.bar()'
,
$this
->
data
);
}
function
test
_evaluate_variable_s
uccess
()
{
$this
->
assert
_e
valuates
(
'bar'
,
'$array.foo'
);
$this
->
assert
_e
valuates
(
'bar'
,
'$foo'
);
$this
->
assert
_e
valuates
(
'baz'
,
'$bar'
);
$this
->
assert
_e
valuates
(
'bar'
,
'$object.foo'
);
$this
->
assert
_e
valuates
(
'baz'
,
'$object.bar'
);
$this
->
assert
_e
valuates
(
'foobar'
,
'$object.baz()'
);
function
test
EvaluateVariableS
uccess
()
{
$this
->
assert
E
valuates
(
'bar'
,
'$array.foo'
);
$this
->
assert
E
valuates
(
'bar'
,
'$foo'
);
$this
->
assert
E
valuates
(
'baz'
,
'$bar'
);
$this
->
assert
E
valuates
(
'bar'
,
'$object.foo'
);
$this
->
assert
E
valuates
(
'baz'
,
'$object.bar'
);
$this
->
assert
E
valuates
(
'foobar'
,
'$object.baz()'
);
}
/**
* @depends test
_evaluate_variable_s
uccess
* @depends test
EvaluateVariableS
uccess
*/
function
test
_evaluate_variable_e
scape
()
{
$this
->
assert
_e
valuates
(
'<script></script>'
,
'$html'
);
$this
->
assert
_e
valuates
(
'Itrntinliztin'
,
'$internationalization'
);
//$this->assert
_e
valuates('Iñtërnâtiônàlizætiøn', '$internationalization');
function
test
EvaluateVariableE
scape
()
{
$this
->
assert
E
valuates
(
'<script></script>'
,
'$html'
);
$this
->
assert
E
valuates
(
'Itrntinliztin'
,
'$internationalization'
);
//$this->assert
E
valuates('Iñtërnâtiônàlizætiøn', '$internationalization');
}
/**
* @depends test
_evaluate_variable_s
uccess
* @depends test
EvaluateVariableS
uccess
*/
function
test
_evaluate_variable_n
oescape
()
{
$this
->
assert
_e
valuates
(
'<script></script>'
,
'$$html'
);
$this
->
assert
_e
valuates
(
'Itrntinliztin'
,
'$$internationalization'
);
function
test
EvaluateVariableN
oescape
()
{
$this
->
assert
E
valuates
(
'<script></script>'
,
'$$html'
);
$this
->
assert
E
valuates
(
'Itrntinliztin'
,
'$$internationalization'
);
}
function
test
_evaluate_c
onstant
()
{
$this
->
assert
_e
valuates
(
'foobar_const'
,
'FOOBAR'
);
$this
->
assert
_e
valuates
(
'{NON_DEFINED_CONST}'
,
'NON_DEFINED_CONST'
);
function
test
EvaluateC
onstant
()
{
$this
->
assert
E
valuates
(
'foobar_const'
,
'FOOBAR'
);
$this
->
assert
E
valuates
(
'{NON_DEFINED_CONST}'
,
'NON_DEFINED_CONST'
);
}
function
test
_evaluate_no_e
xpression
()
{
$this
->
assert
_e
valuates
(
'{foo}'
,
'foo'
);
function
test
EvaluateNoE
xpression
()
{
$this
->
assert
E
valuates
(
'{foo}'
,
'foo'
);
}
function
test
_evaluate_condition_i
f
()
{
$this
->
assert
_e
valuates
(
'bar'
,
'$true?bar'
);
$this
->
assert
_e
valuates
(
''
,
'$false?bar'
);
function
test
EvaluateConditionI
f
()
{
$this
->
assert
E
valuates
(
'bar'
,
'$true?bar'
);
$this
->
assert
E
valuates
(
''
,
'$false?bar'
);
}
function
test
_evaluate_condition_if_e
lse
()
{
$this
->
assert
_e
valuates
(
'bar'
,
'$true?bar:baz'
);
$this
->
assert
_e
valuates
(
'baz'
,
'$false?bar:baz'
);
function
test
EvaluateConditionIfE
lse
()
{
$this
->
assert
E
valuates
(
'bar'
,
'$true?bar:baz'
);
$this
->
assert
E
valuates
(
'baz'
,
'$false?bar:baz'
);
}
/**
* @depends test
_evaluate_condition_i
f
* @depends test
_evaluate_condition_if_e
lse
* @depends test
EvaluateConditionI
f
* @depends test
EvaluateConditionIfE
lse
*/
function
test
_evaluate_condition_e
xtended
()
{
$this
->
assert
_e
valuates
(
' bar '
,
'$true? bar : baz'
);
$this
->
assert
_e
valuates
(
' baz'
,
'$false? bar : baz'
);
function
test
EvaluateConditionE
xtended
()
{
$this
->
assert
E
valuates
(
' bar '
,
'$true? bar : baz'
);
$this
->
assert
E
valuates
(
' baz'
,
'$false? bar : baz'
);
$this
->
assert
_e
valuates
(
' bar '
,
'$true ? bar : baz'
);
$this
->
assert
_e
valuates
(
' baz'
,
'$false ? bar : baz'
);
$this
->
assert
E
valuates
(
' bar '
,
'$true ? bar : baz'
);
$this
->
assert
E
valuates
(
' baz'
,
'$false ? bar : baz'
);
$this
->
assert
_e
valuates
(
' Foo bar '
,
'$true ? Foo bar : Baz foo'
);
$this
->
assert
_e
valuates
(
' Baz foo'
,
'$false ? Foo bar : Baz foo'
);
$this
->
assert
E
valuates
(
' Foo bar '
,
'$true ? Foo bar : Baz foo'
);
$this
->
assert
E
valuates
(
' Baz foo'
,
'$false ? Foo bar : Baz foo'
);
$this
->
assert
_e
valuates
(
'| bar'
,
'$true ?| $foo'
);
$this
->
assert
E
valuates
(
'| bar'
,
'$true ?| $foo'
);
}
/**
* @expectedException \BadFunctionCallException
*/
function
test
_evaluate_function_e
rror
()
{
$this
->
evaluate
_e
xpression
(
'undefined_function($foo)'
,
$this
->
data
);
function
test
EvaluateFunctionE
rror
()
{
$this
->
evaluate
E
xpression
(
'undefined_function($foo)'
,
$this
->
data
);
}
function
test
_evaluate_function_s
uccess
()
{
$this
->
assert
_e
valuates
(
'Bar'
,
'ucfirst($foo)'
);
$this
->
assert
_e
valuates
(
'Bar'
,
'DataObject::foobar($foo)'
);
function
test
EvaluateFunctionS
uccess
()
{
$this
->
assert
E
valuates
(
'Bar'
,
'ucfirst($foo)'
);
$this
->
assert
E
valuates
(
'Bar'
,
'DataObject::foobar($foo)'
);
}
/**
* @depends test
_evaluate_function_s
uccess
* @depends test
EvaluateFunctionS
uccess
*/
function
test
_evaluate_function_n
ested
()
{
$this
->
assert
_e
valuates
(
'Bar'
,
'ucfirst(strtolower($FOO))'
);
function
test
EvaluateFunctionN
ested
()
{
$this
->
assert
E
valuates
(
'Bar'
,
'ucfirst(strtolower($FOO))'
);
}
function
test
_evaluate_default_v
alue
()
{
$this
->
assert
_e
valuates
(
'bar'
,
'$foo||fallback'
);
$this
->
assert
_e
valuates
(
'fallback'
,
'$foo.bar||fallback'
);
$this
->
assert
_e
valuates
(
''
,
'$foo.bar||'
);
function
test
EvaluateDefaultV
alue
()
{
$this
->
assert
E
valuates
(
'bar'
,
'$foo||fallback'
);
$this
->
assert
E
valuates
(
'fallback'
,
'$foo.bar||fallback'
);
$this
->
assert
E
valuates
(
''
,
'$foo.bar||'
);
}
/**
* @depends test
_evaluate_variable_s
uccess
* @depends test
_evaluate_no_e
xpression
* @depends test
_evaluate_condition_e
xtended
* @depends test
_evaluate_function_s
uccess
* @depends test
_evaluate_default_v
alue
* @depends test
EvaluateVariableS
uccess
* @depends test
EvaluateNoE
xpression
* @depends test
EvaluateConditionE
xtended
* @depends test
EvaluateFunctionS
uccess
* @depends test
EvaluateDefaultV
alue
*/
function
test
_evaluate_expression_c
ombined
()
{
$this
->
assert
_e
valuates
(
'Bar'
,
'$true?ucfirst($foo)'
);
$this
->
assert
_e
valuates
(
''
,
'$false?ucfirst($foo)'
);
$this
->
assert
_e
valuates
(
'Bar'
,
'$true?ucfirst($foo):baz'
);
$this
->
assert
_e
valuates
(
'baz'
,
'$false?ucfirst($foo):baz'
);
$this
->
assert
_e
valuates
(
'Baz'
,
'ucfirst($array.bar)'
);
function
test
EvaluateExpressionC
ombined
()
{
$this
->
assert
E
valuates
(
'Bar'
,
'$true?ucfirst($foo)'
);
$this
->
assert
E
valuates
(
''
,
'$false?ucfirst($foo)'
);
$this
->
assert
E
valuates
(
'Bar'
,
'$true?ucfirst($foo):baz'
);
$this
->
assert
E
valuates
(
'baz'
,
'$false?ucfirst($foo):baz'
);
$this
->
assert
E
valuates
(
'Baz'
,
'ucfirst($array.bar)'
);
}
function
assert
_r
enders
(
$expected_file
,
$tpl
)
{
function
assert
R
enders
(
$expected_file
,
$tpl
)
{
$expected_file
=
"tests/_files/rendered/
$expected_file
.html"
;
$this
->
assertEquals
(
self
::
strip
_n
ewlines
(
file_get_contents
(
$expected_file
)),
self
::
strip
_n
ewlines
(
$tpl
->
render
()));
$this
->
assertEquals
(
self
::
strip
N
ewlines
(
file_get_contents
(
$expected_file
)),
self
::
strip
N
ewlines
(
$tpl
->
render
()));
}
function
test
_render_s
imple
()
{
function
test
RenderS
imple
()
{
$this
->
assertEquals
(
'test'
,
$this
->
tpl
->
render
());
}
/**
* @depends test
_evaluate_expression_c
ombined
* @depends test
EvaluateExpressionC
ombined
*/
function
test
_render_v
ariable
()
{
function
test
RenderV
ariable
()
{
$tpl
=
new
Template
(
'variables'
);
$tpl
->
set
(
array
(
'foobar'
=>
'my_foobar_variable'
,
'foobaz'
=>
'MY_FOOBAZ_VARIABLE'
));
$this
->
assert
_r
enders
(
'variables'
,
$tpl
);
$this
->
assert
R
enders
(
'variables'
,
$tpl
);
}
/**
* @depends test
_render_s
imple
* @depends test
RenderS
imple
*/
function
test
_render_b
locks
()
{
function
test
RenderB
locks
()
{
$tpl
=
new
Template
(
'blocks'
);
$foo
=
$tpl
->
add
(
'foo'
);
...
...
@@ -414,21 +414,21 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
$foo
->
add
(
'bar'
);
$tpl
->
add
(
'foo'
);
$this
->
assert
_r
enders
(
'blocks'
,
$tpl
);
$this
->
assert
R
enders
(
'blocks'
,
$tpl
);
}
/**
* @depends test
_render_v
ariable
* @depends test
_render_b
locks
* @depends test
RenderV
ariable
* @depends test
RenderB
locks
*/
function
test
_render_f
ull
()
{
function
test
RenderF
ull
()
{
$tpl
=
new
Template
(
'full'
);
$first_foo
=
$tpl
->
add
(
'foo'
)
->
set
(
'foobaz'
,
'FIRST_FOOBAZ_VAR'
);
$first_foo
->
add
(
'bar'
)
->
set
(
'foobar'
,
'first_foobar_var'
);
$second_foo
=
$tpl
->
add
(
'foo'
)
->
set
(
'foobaz'
,
'SECOND_FOOBAZ_VAR'
);
$second_foo
->
add
(
'bar'
)
->
set
(
'foobar'
,
'second_foobar_var'
);
$second_foo
->
add
(
'bar'
)
->
set
(
'foobar'
,
'third_foobar_var'
);
$this
->
assert
_r
enders
(
'full'
,
$tpl
);
$this
->
assert
R
enders
(
'full'
,
$tpl
);
}
}
...
...
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