Commit 3566402f authored by Taddeus Kroes's avatar Taddeus Kroes

Camelized class method names

parent 4dde6795
......@@ -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_class('FooBar'); // Includes file 'classes/foo_bar.php'
* $loader->loadClass('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_directory($root_directory);
$this->set_root_namespace($root_namespace);
$this->set_throw_errors($throw);
$this->setRootDirectory($root_directory);
$this->setRootNamespace($root_namespace);
$this->setThrowErrors($throw);
}
/**
......@@ -95,7 +95,7 @@ class Autoloader extends Base {
*
* @param bool $throw Whether to throw exceptions.
*/
function set_throw_errors($throw) {
function setThrowErrors($throw) {
$this->throw_errors = !!$throw;
}
......@@ -104,7 +104,7 @@ class Autoloader extends Base {
*
* @return bool
*/
function get_throw_errors() {
function getThrowErrors() {
return $this->throw_errors;
}
......@@ -113,8 +113,8 @@ class Autoloader extends Base {
*
* @param string $directory The new root directory.
*/
function set_root_directory($directory) {
$this->root_directory = self::path_with_slash($directory);
function setRootDirectory($directory) {
$this->root_directory = self::pathWithSlash($directory);
}
/**
......@@ -122,7 +122,7 @@ class Autoloader extends Base {
*
* @return string
*/
function get_root_directory() {
function getRootDirectory() {
return $this->root_directory;
}
......@@ -131,9 +131,9 @@ class Autoloader extends Base {
*
* @param string $namespace The new root namespace.
*/
function set_root_namespace($namespace) {
function setRootNamespace($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_namespace() {
function getRootNamespace() {
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_filename($classname) {
static function classnameToFilename($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_namespace($classname) {
private function stripRootNamespace($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_path($classname) {
function createPath($classname) {
$namespaces = array_filter(explode('\\', $classname));
$dirs = array_map('self::classname_to_filename', $namespaces);
$dirs = array_map('self::classnameToFilename', $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_class($classname, $throw=null) {
$classname = $this->strip_root_namespace($classname);
$path = $this->create_path($classname);
function loadClass($classname, $throw=null) {
$classname = $this->stripRootNamespace($classname);
$path = $this->createPath($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_class'), true, $prepend);
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
}
......
......@@ -61,7 +61,7 @@ abstract class Base {
* @param string $directory The directory to append a slash to.
* @return string
*/
static function path_with_slash($directory) {
static function pathWithSlash($directory) {
return $directory[strlen($directory) - 1] == '/' ? $directory : $directory.'/';
}
}
......
......@@ -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 PHPActiveRecord\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_by('id'); // Create indexes for all books
* $names = $indexed->get_attribute('name'); // Get the values of a single attribute
* $indexed = $collection->indexBy('id'); // Create indexes for all books
* $names = $indexed->getAttribute('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_attribute('name');
* $names = Collection::create(Book::all())->indexBy('id')->getAttribute('name');
* </code>
*
* Example 2: Execute a method for each item in a list.
* <code>
* // Delete all books
* Collection::create(Book::all())->map_method('delete');
* Collection::create(Book::all())->mapMethod('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_exists($index) {
function indexExists($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_index($index) {
function deleteIndex($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_index(array_search($item, $this->items));
$this->deleteIndex(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_items(array $items, $clone=true) {
private function setItems(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_items(array_values(array_unique($this->items)), $clone);
return $this->setItems(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_items(array_values(array_filter($this->items, $callback)), $clone);
return $this->setItems(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_attribute($attribute) {
function getAttribute($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_by($attribute, $clone=true) {
function indexBy($attribute, $clone=true) {
$indexed = array();
foreach( $this->items as $item )
foreach ($this->items as $item)
$indexed[$item->$attribute] = $item;
return $this->set_items($indexed, $clone);
return $this->setItems($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_items(array_map($callback, $this->items), $clone);
return $this->setItems(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_method($method_name, array $args=array(), $clone=true) {
function mapMethod($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_items($items);
return $this->setItems($items);
}
}
......
......@@ -35,34 +35,34 @@ class Logger extends Base {
private $dump_format = 'plain';
private $log_directory = '';
function set_directory($directory) {
$this->log_directory = self::path_with_slash($directory);
function setDirectory($directory) {
$this->log_directory = self::pathWithSlash($directory);
}
function set_dump_format($format) {
function setDumpFormat($format) {
if (!in_array($format, self::$allowed_dump_formats))
throw new \InvalidArgumentException(sprintf('', $format));
$this->dump_format = $format;
}
function set_format($format) {
function setFormat($format) {
$this->format = (string)$format;
}
function get_format() {
function getFormat() {
return $this->format;
}
function get_level() {
function getLevel() {
return $this->level;
}
function get_level_name() {
function getLevelName() {
return self::$level_names[$this->level];
}
function set_level($level) {
function setLevel($level) {
if (is_string($level)) {
$level = strtoupper($level);
......@@ -78,7 +78,7 @@ class Logger extends Base {
$this->level = $level;
}
function set_property($name, $value) {
function setProperty($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_property($matches[1]);
return $logger->getFormattedProperty($matches[1]);
},
$this->format
);
......@@ -169,7 +169,7 @@ class Logger extends Base {
file_put_contents($this->log_directory . $path, $this->dumps());
}
function handle_exception(\Exception $e) {
function handleException(\Exception $e) {
if( $e === null )
return;
......@@ -179,17 +179,17 @@ class Logger extends Base {
$this->dump('error');
}
function set_as_exception_handler() {
function setAsExceptionHandler() {
set_exception_handler(array($this, 'handle_exception'));
}
function get_formatted_property($property) {
function getFormattedProperty($property) {
if (isset($this->properties[$property]))
return $this->properties[$property];
switch( $property ) {
case 'loglevel':
return $this->get_level_name();
return $this->getLevelName();
case 'date':
return strftime('%d-%m-%Y');
case 'time':
......
......@@ -88,7 +88,7 @@ class Node extends Base {
*
* @return int The node's id.
*/
function get_id() {
function getId() {
return $this->id;
}
......@@ -97,7 +97,7 @@ class Node extends Base {
*
* @return string The node's name.
*/
function get_name() {
function getName() {
return $this->name;
}
......@@ -106,7 +106,7 @@ class Node extends Base {
*
* @return Node|null The parent node if any, NULL otherwise.
*/
function get_parent() {
function getParent() {
return $this->parent_node;
}
......@@ -115,7 +115,7 @@ class Node extends Base {
*
* @return array A list of child nodes.
*/
function get_children() {
function getChildren() {
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_id() == $this->id;
return $node->getId() == $this->id;
}
/**
......@@ -136,7 +136,7 @@ class Node extends Base {
*
* @return bool Whether this node is the root node.
*/
function is_root() {
function isRoot() {
return $this->parent_node === null;
}
......@@ -147,7 +147,7 @@ class Node extends Base {
*
* @return bool Whether this node is a leaf node.
*/
function is_leaf() {
function isLeaf() {
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_child(Node &$node, $set_parent=true) {
function addChild(Node &$node, $set_parent=true) {
$this->children[] = $node;
$set_parent && $node->set_parent($this);
$set_parent && $node->setParent($this);
}
/**
......@@ -172,7 +172,7 @@ class Node extends Base {
*/
function add($name, array $data=array()) {
$node = new self($name, $this);
$this->add_child($node, false);
$this->addChild($node, false);
return $node->set($data);
}
......@@ -182,7 +182,7 @@ class Node extends Base {
*
* @param Node &$child The node to remove.
*/
function remove_child(Node &$child) {
function removeChild(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_root())
if ($this->isRoot())
throw new \RuntimeException('Cannot remove the root node of a tree.');
$this->parent_node->remove_child($this);
$this->parent_node->removeChild($this);
foreach ($this->children as $child)
$child->set_parent(null);
$child->setParent(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_parent($parent) {
function setParent($parent) {
if ($this->parent_node !== null)
$this->parent_node->remove_child($this);
$this->parent_node->removeChild($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_name() == $name;
return $child->getName() == $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_child($child_copy);
$copy->addChild($child_copy);
} else {
$copy->add_child($child, false);
$copy->addChild($child, false);
}
}
......
......@@ -28,8 +28,8 @@ require_once 'base.php';
* '/home' => 'home',
* '/contact' => 'contact'
* ));
* $response = $router->call_handler('/home'); // 'This is the home page.'
* $response = $router->call_handler('/contact'); // 'This is the contact page.'
* $response = $router->callHandler('/home'); // 'This is the home page.'
* $response = $router->callHandler('/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_handler('/home'); // 'This is the home page.'
* $response = $router->call_handler('/contact'); // 'This is the contact page.'
* $response = $router->callHandler('/home'); // 'This is the home page.'
* $response = $router->callHandler('/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_route($pattern, $handler);
$this->addRoute($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_route($pattern, $handler) {
function addRoute($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_handler($url) {
function callHandler($url) {
foreach ($this->routes as $pattern => $handler) {
if (preg_match($pattern, $url, $matches)) {
array_shift($matches);
......
......@@ -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_blocks();
$this->parseBlocks();
}
/**
......@@ -172,7 +172,7 @@ class Template extends Node {
*
* @return string The path to the template file.
*/
function get_path() {
function getPath() {
return $this->path;
}
......@@ -182,7 +182,7 @@ class Template extends Node {
*
* @throws ParseError If an {end} tag is not used properly.
*/
private function parse_blocks() {
private function parseBlocks() {
$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_root())
if ($current->isRoot())
throw new ParseError($this, 'unexpected {end}', $line_count + 1);
$current = $current->get_parent();
$current = $current->getParent();
} 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_block($this->root_block, $this);
return self::renderBlock($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_expression()
* @uses evaluateExpression()
*/
private static function render_block(Node $block, Node $data) {
private static function renderBlock(Node $block, Node $data) {
$html = '';
foreach ($block->get_children() as $child) {
switch ($child->get_name()) {
foreach ($block->getChildren() as $child) {
switch ($child->getName()) {
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_block($child, $child_data);
$html .= self::renderBlock($child, $child_data);
break;
case 'expression':
$html .= self::evaluate_expression($child->get('content'), $data);
$html .= self::evaluateExpression($child->get('content'), $data);
}
}
......@@ -269,7 +269,7 @@ class Template extends Node {
/**
* Evaluate a <variable> expression.
*
* This function is a helper for {@link evaluate_expression()}.
* This function is a helper for {@link evaluateExpression()}.
*
* @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_variable(array $matches, Node $data) {
private static function evaluateVariable(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_value($value);
$value = self::escapeVariableValue($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_value($value) {
private static function escapeVariableValue($value) {
return htmlspecialchars($value, ENT_QUOTES);
}
/**
* Evaluate a conditional expression.
*
* This function is a helper for {@link evaluate_expression()}.
* This function is a helper for {@link evaluateExpression()}.
*
* @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_condition(array $matches, Node $data) {
if (self::evaluate_expression($matches[1], $data, false)) {
private static function evaluateCondition(array $matches, Node $data) {
if (self::evaluateExpression($matches[1], $data, false)) {
// Condition evaluates to true: return 'if' evaluation
return self::evaluate_expression($matches[2], $data, false);
return self::evaluateExpression($matches[2], $data, false);
} elseif (count($matches) == 4) {
// <nested_exp>?<nested_exp>:<nested_exp>
return self::evaluate_expression($matches[3], $data, false);
return self::evaluateExpression($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_expression()}.
* This function is a helper for {@link evaluateExpression()}.
*
* @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_function(array $matches, Node $data) {
private static function evaluateFunction(array $matches, Node $data) {
$function = $matches[1];
$parameter = $matches[2];
......@@ -396,7 +396,7 @@ class Template extends Node {
);
}
$parameter_value = self::evaluate_expression($parameter, $data, false);
$parameter_value = self::evaluateExpression($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_expression()}.
* This function is a helper for {@link evaluateExpression()}.
*
* @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_constant($constant, $root_level) {
private static function evaluateConstant($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_expression($expression, Node $data, $root_level=true) {
private static function evaluateExpression($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_condition($matches, $data);
return self::evaluateCondition($matches, $data);
} elseif (preg_match("/^(.*?)\\$(\\$?)($name)(?:\.($name)(\(\))?)?$/", $expression, $matches)) {
// $<name> | $<name>.<name> | $<name>.<name>()
// | $$<name> | $$<name>.<name> | $$<name>.<name>()
return self::evaluate_variable($matches, $data);
return self::evaluateVariable($matches, $data);
} elseif (preg_match("/^($function)\((.+?)\)?$/", $expression, $matches)) {
// <function>(<nested_exp>)
return self::evaluate_function($matches, $data);
return self::evaluateFunction($matches, $data);
} elseif (preg_match("/^([A-Z0-9_]+)$/", $expression, $matches)) {
// <constant>
return self::evaluate_constant($expression, $root_level);
return self::evaluateConstant($expression, $root_level);
} elseif (($split_at = strpos($expression, '||', 1)) !== false) {
// <nested_exp>||<nested_exp>
try {
return self::evaluate_expression(substr($expression, 0, $split_at), $data, false);
return self::evaluateExpression(substr($expression, 0, $split_at), $data, false);
} catch(\RuntimeException $e) {
return self::evaluate_expression(substr($expression, $split_at + 2), $data, false);
return self::evaluateExpression(substr($expression, $split_at + 2), $data, false);
}
}
}
......@@ -468,7 +468,7 @@ class Template extends Node {
/**
* Remove all current include paths.
*/
static function clear_include_path() {
static function clearIncludePath() {
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_path()
* @uses clearIncludePath()
*/
static function set_root($path) {
self::clear_include_path();
self::add_root($path);
static function setRoot($path) {
self::clearIncludePath();
self::addRoot($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_root($path) {
static function addRoot($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_path(), $line, $message);
$tpl->getPath(), $line, $message);
}
}
......
......@@ -10,139 +10,139 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase {
$this->autoloader = new Autoloader(PATH);
}
function test_set_root_namespace() {
function testSetRootNamespace() {
$this->assertAttributeEquals('\\', 'root_namespace', $this->autoloader);
$this->autoloader->set_root_namespace('Foo');
$this->autoloader->setRootNamespace('Foo');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
$this->autoloader->set_root_namespace('Foo\\');
$this->autoloader->setRootNamespace('Foo\\');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
}
/**
* @depends test_set_root_namespace
* @depends testSetRootNamespace
*/
function test_get_root_namespace() {
$this->autoloader->set_root_namespace('Foo');
$this->assertEquals($this->autoloader->get_root_namespace(), 'Foo\\');
function testGetRootNamespace() {
$this->autoloader->setRootNamespace('Foo');
$this->assertEquals($this->autoloader->getRootNamespace(), 'Foo\\');
}
/**
* @depends test_set_root_namespace
* @depends testSetRootNamespace
*/
function test_construct_root_namespace() {
function testConstructRootNamespace() {
$autoloader = new Autoloader(PATH, 'Foo');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $autoloader);
}
/**
* @depends test_set_root_namespace
* @depends testSetRootNamespace
*/
function test_strip_root_namespace() {
$strip = new ReflectionMethod('webbasics\Autoloader', 'strip_root_namespace');
function testStripRootNamespace() {
$strip = new ReflectionMethod('webbasics\Autoloader', 'stripRootNamespace');
$strip->setAccessible(true);
$this->autoloader->set_root_namespace('Foo');
$this->autoloader->setRootNamespace('Foo');
$this->assertEquals($strip->invoke($this->autoloader, 'Foo\Bar'), 'Bar');
}
function test_set_root_directory() {
$this->autoloader->set_root_directory('tests');
$this->assertEquals($this->autoloader->get_root_directory(), 'tests/');
function testSetRootDirectory() {
$this->autoloader->setRootDirectory('tests');
$this->assertEquals($this->autoloader->getRootDirectory(), 'tests/');
}
function test_classname_to_filename() {
$this->assertEquals(Autoloader::classname_to_filename('Foo'), 'foo');
$this->assertEquals(Autoloader::classname_to_filename('FooBar'), 'foo_bar');
$this->assertEquals(Autoloader::classname_to_filename('fooBar'), 'foo_bar');
$this->assertEquals(Autoloader::classname_to_filename('FooBarBaz'), 'foo_bar_baz');
function testClassnameToFilename() {
$this->assertEquals(Autoloader::classnameToFilename('Foo'), 'foo');
$this->assertEquals(Autoloader::classnameToFilename('FooBar'), 'foo_bar');
$this->assertEquals(Autoloader::classnameToFilename('fooBar'), 'foo_bar');
$this->assertEquals(Autoloader::classnameToFilename('FooBarBaz'), 'foo_bar_baz');
}
/**
* @depends test_classname_to_filename
* @depends testClassnameToFilename
*/
function test_create_path() {
$this->assertEquals($this->autoloader->create_path('Foo'), PATH.'foo.php');
$this->assertEquals($this->autoloader->create_path('\Foo'), PATH.'foo.php');
$this->assertEquals($this->autoloader->create_path('Foo\Bar'), PATH.'foo/bar.php');
$this->assertEquals($this->autoloader->create_path('Foo\Bar\Baz'), PATH.'foo/bar/baz.php');
$this->assertEquals($this->autoloader->create_path('FooBar\Baz'), PATH.'foo_bar/baz.php');
function testCreatePath() {
$this->assertEquals($this->autoloader->createPath('Foo'), PATH.'foo.php');
$this->assertEquals($this->autoloader->createPath('\Foo'), PATH.'foo.php');
$this->assertEquals($this->autoloader->createPath('Foo\Bar'), PATH.'foo/bar.php');
$this->assertEquals($this->autoloader->createPath('Foo\Bar\Baz'), PATH.'foo/bar/baz.php');
$this->assertEquals($this->autoloader->createPath('FooBar\Baz'), PATH.'foo_bar/baz.php');
}
function test_throw_errors() {
$this->assertFalse($this->autoloader->get_throw_errors());
$this->autoloader->set_throw_errors(true);
$this->assertTrue($this->autoloader->get_throw_errors());
function testThrowErrors() {
$this->assertFalse($this->autoloader->getThrowErrors());
$this->autoloader->setThrowErrors(true);
$this->assertTrue($this->autoloader->getThrowErrors());
}
/**
* @depends test_create_path
* @depends test_throw_errors
* @depends testCreatePath
* @depends testThrowErrors
*/
function test_load_class_not_found() {
$this->assertFalse($this->autoloader->load_class('foobar'));
function testLoadClassNotFound() {
$this->assertFalse($this->autoloader->loadClass('foobar'));
}
/**
* @depends test_load_class_not_found
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function test_load_class_not_found_error() {
$this->autoloader->set_throw_errors(true);
$this->autoloader->load_class('foobar');
function testLoadClassNotFoundError() {
$this->autoloader->setThrowErrors(true);
$this->autoloader->loadClass('foobar');
}
/**
* @depends test_load_class_not_found
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function test_load_class_not_found_noerror_overwrite() {
$this->autoloader->load_class('foobar', true);
function testLoadClassNotFoundNoerrorOverwrite() {
$this->autoloader->loadClass('foobar', true);
}
/**
* @depends test_load_class_not_found
* @depends testLoadClassNotFound
*/
function test_load_class_not_found_error_overwrite() {
$this->autoloader->set_throw_errors(true);
$this->assertFalse($this->autoloader->load_class('foobar', false));
function testLoadClassNotFoundErrorOverwrite() {
$this->autoloader->setThrowErrors(true);
$this->assertFalse($this->autoloader->loadClass('foobar', false));
}
/**
* @depends test_load_class_not_found
* @depends testLoadClassNotFound
*/
function test_load_class() {
$this->assertTrue($this->autoloader->load_class('Foo'));
function testLoadClass() {
$this->assertTrue($this->autoloader->loadClass('Foo'));
$this->assertTrue(class_exists('Foo', false));
$this->assertTrue($this->autoloader->load_class('Foo\Bar'));
$this->assertTrue($this->autoloader->loadClass('Foo\Bar'));
$this->assertTrue(class_exists('Foo\Bar', false));
}
/**
* @depends test_load_class
* @depends test_strip_root_namespace
* @depends testLoadClass
* @depends testStripRootNamespace
*/
function test_load_class_root_namespace() {
function testLoadClassRootNamespace() {
$autoloader = new Autoloader(PATH.'foo');
$autoloader->set_root_namespace('Foo');
$this->assertTrue($autoloader->load_class('Bar'));
$autoloader->setRootNamespace('Foo');
$this->assertTrue($autoloader->loadClass('Bar'));
$this->assertTrue(class_exists('Foo\Bar', false));
}
/**
* @depends test_load_class
* @depends testLoadClass
*/
function test_register() {
function testRegister() {
$this->autoloader->register();
$this->assertTrue(class_exists('Baz'));
}
/**
* @depends test_register
* @depends test_throw_errors
* @depends testRegister
* @depends testThrowErrors
*/
function test_register_prepend() {
function testRegisterPrepend() {
$second_loader = new Autoloader(PATH.'second');
$this->autoloader->register();
$second_loader->register(true); // Prepend so that the second loader attemps to load Bar first
......
......@@ -11,11 +11,11 @@ class BaseExtension extends Base {
}
class BaseTest extends PHPUnit_Framework_TestCase {
function test_create() {
function testCreate() {
$this->assertEquals(BaseExtension::create('a', 'b'), new BaseExtension('a', 'b'));
}
function test_asprintf() {
function testAsprintf() {
$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_slash() {
$this->assertEquals(Base::path_with_slash('dirname'), 'dirname/');
$this->assertEquals(Base::path_with_slash('dirname/'), 'dirname/');
function testPathWithSlash() {
$this->assertEquals(Base::pathWithSlash('dirname'), 'dirname/');
$this->assertEquals(Base::pathWithSlash('dirname/'), 'dirname/');
}
}
......
......@@ -11,11 +11,11 @@ class IdObject {
$this->foo = $foo;
}
function get_id() {
function getId() {
return $this->id;
}
static function clear_counter() {
static function clearCounter() {
self::$count = 0;
}
}
......@@ -39,7 +39,7 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
$this->set = set(array(1, 2));
}
function test_add() {
function testAdd() {
$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_error() {
function testInsertError() {
set(array('foo' => 1))->insert(2, 'foo');
}
function test_insert_success() {
function testInsertSuccess() {
$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_all() {
function testAll() {
$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_empty() {
function testLastEmpty() {
set()->last();
}
function test_last() {
function testLast() {
$this->assertEquals($this->set->last(), 2);
}
/**
* @expectedException OutOfBoundsException
*/
function test_first_empty() {
function testFirstEmpty() {
set()->first();
}
function test_first() {
function testFirst() {
$this->assertEquals($this->set->first(), 1);
}
function test_count() {
function testCount() {
$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_exists() {
$this->assertTrue($this->set->index_exists(1));
$this->assertTrue(set(array('foo' => 'bar'))->index_exists('foo'));
function testIndexExists() {
$this->assertTrue($this->set->indexExists(1));
$this->assertTrue(set(array('foo' => 'bar'))->indexExists('foo'));
}
function test_get() {
function testGet() {
$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_index() {
$this->set->delete_index(0);
function testDeleteIndex() {
$this->set->deleteIndex(0);
$this->assertEquals($this->set, set(array(1 => 2)));
}
function test_delete() {
function testDelete() {
$this->set->delete(1);
$this->assertEquals($this->set, set(array(1 => 2)));
}
function assert_set_equals(array $expected_items, $set) {
function assertSetEquals(array $expected_items, $set) {
$this->assertAttributeEquals($expected_items, 'items', $set);
}
function test_uniques() {
$this->assert_set_equals(array(1, 2), set(array(1, 2, 2))->uniques());
$this->assert_set_equals(array(2, 1), set(array(2, 1, 2))->uniques());
$this->assert_set_equals(array(2, 1), set(array(2, 2, 1))->uniques());
function testUniques() {
$this->assertSetEquals(array(1, 2), set(array(1, 2, 2))->uniques());
$this->assertSetEquals(array(2, 1), set(array(2, 1, 2))->uniques());
$this->assertSetEquals(array(2, 1), set(array(2, 2, 1))->uniques());
}
function set_items($collection, $items, $clone) {
$rm = new ReflectionMethod($collection, 'set_items');
function setItems($collection, $items, $clone) {
$rm = new ReflectionMethod($collection, 'setItems');
$rm->setAccessible(true);
return $rm->invoke($collection, $items, $clone);
}
function test_set_items_clone() {
$result = $this->set_items($this->set, array(3, 4), true);
$this->assert_set_equals(array(1, 2), $this->set);
$this->assert_set_equals(array(3, 4), $result);
function testSetItemsClone() {
$result = $this->setItems($this->set, array(3, 4), true);
$this->assertSetEquals(array(1, 2), $this->set);
$this->assertSetEquals(array(3, 4), $result);
$this->assertNotSame($this->set, $result);
}
function test_set_items_no_clone() {
$result = $this->set_items($this->set, array(3, 4), false);
function testSetItemsNoClone() {
$result = $this->setItems($this->set, array(3, 4), false);
$this->assertSame($this->set, $result);
}
/**
* @depends test_set_items_clone
* @depends testSetItemsClone
*/
function test_filter() {
function testFilter() {
$smaller_than_five = function($number) { return $number < 5; };
$this->assert_set_equals(array(2, 4, 1, 4), set(array(2, 7, 4, 7, 1, 8, 4, 5))->filter($smaller_than_five));
$this->assertSetEquals(array(2, 4, 1, 4), set(array(2, 7, 4, 7, 1, 8, 4, 5))->filter($smaller_than_five));
}
/**
* @depends test_filter
* @depends testFilter
*/
function test_find_success() {
function testFindSuccess() {
$items = array(
array('foo' => 'bar', 'bar' => 'baz'),
array('foo' => 'baz', 'bar' => 'foo'),
std_object(array('foo' => 'bar', 'baz' => 'bar')),
);
$this->assert_set_equals(array($items[1]), set($items)->find(array('foo' => 'baz')));
$this->assert_set_equals(array($items[0], $items[2]), set($items)->find(array('foo' => 'bar')));
$this->assertSetEquals(array($items[1]), set($items)->find(array('foo' => 'baz')));
$this->assertSetEquals(array($items[0], $items[2]), set($items)->find(array('foo' => 'bar')));
}
/**
* @depends test_find_success
* @depends testFindSuccess
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage Collection::find encountered a non-object and non-array item "foobar".
*/
function test_find_failure() {
function testFindFailure() {
$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_simple() {
IdObject::clear_counter();
function testGetAttributeSimple() {
IdObject::clearCounter();
$set = set(array(new IdObject(), new IdObject(), new IdObject()));
$this->assertEquals(array(1, 2, 3), $set->get_attribute('id'));
$this->assertEquals(array(1, 2, 3), $set->getAttribute('id'));
}
/**
* @depends test_get_attribute_simple
* @depends testGetAttributeSimple
*/
function test_get_attribute_indices() {
IdObject::clear_counter();
function testGetAttributeIndices() {
IdObject::clearCounter();
$set = set(array('foo' => new IdObject(), 'bar' => new IdObject(), 'baz' => new IdObject()));
$this->assertEquals(array('foo' => 1, 'bar' => 2, 'baz' => 3), $set->get_attribute('id'));
$this->assertEquals(array('foo' => 1, 'bar' => 2, 'baz' => 3), $set->getAttribute('id'));
}
/**
* @depends test_all
* @depends test_set_items_clone
* @depends testAll
* @depends testSetItemsClone
*/
function test_index_by() {
IdObject::clear_counter();
function testIndexBy() {
IdObject::clearCounter();
$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_by('foo'));
$this->assertSetEquals(array('foo' => $foo, 'bar' => $bar, 'baz' => $baz), $set->indexBy('foo'));
}
/**
* @depends test_set_items_clone
* @depends testSetItemsClone
*/
function test_map() {
function testMap() {
$plus_five = function($number) { return $number + 5; };
$this->assert_set_equals(array(6, 7, 8), set(array(1, 2, 3))->map($plus_five));
$this->assertSetEquals(array(6, 7, 8), set(array(1, 2, 3))->map($plus_five));
}
/**
* @depends test_set_items_clone
* @depends testSetItemsClone
*/
function test_map_method() {
IdObject::clear_counter();
function testMapMethod() {
IdObject::clearCounter();
$set = set(array(new IdObject(), new IdObject(), new IdObject()));
$this->assert_set_equals(array(1, 2, 3), $set->map_method('get_id'));
$this->assertSetEquals(array(1, 2, 3), $set->mapMethod('getId'));
}
}
......
......@@ -11,138 +11,138 @@ define('LOGFILE', 'build/temp.log');
class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
function setUp() {
$this->logger = new Logger();
$this->logger->set_property('name', NAME);
$this->logger->set_format(FORMAT);
$this->logger->setProperty('name', NAME);
$this->logger->setFormat(FORMAT);
is_dir('build') || mkdir('build');
}
function assert_dumps($expected) {
function assertDumps($expected) {
$this->assertEquals($this->logger->dumps(), $expected);
}
function test_set_directory() {
$this->logger->set_directory('logs');
function testSetDirectory() {
$this->logger->setDirectory('logs');
$this->assertAttributeEquals('logs/', 'log_directory', $this->logger);
$this->logger->set_directory('logs/');
$this->logger->setDirectory('logs/');
$this->assertAttributeEquals('logs/', 'log_directory', $this->logger);
}
function test_set_format() {
$this->logger->set_format('foo');
function testSetFormat() {
$this->logger->setFormat('foo');
$this->assertAttributeEquals('foo', 'format', $this->logger);
}
function test_set_dump_format_success() {
$this->logger->set_dump_format('html');
function testSetDumpFormatSuccess() {
$this->logger->setDumpFormat('html');
$this->assertAttributeEquals('html', 'dump_format', $this->logger);
}
/**
* @expectedException InvalidArgumentException
*/
function test_set_dump_format_failure() {
$this->logger->set_dump_format('foo');
function testSetDumpFormatFailure() {
$this->logger->setDumpFormat('foo');
}
function test_get_format() {
$this->assertEquals($this->logger->get_format(), FORMAT);
function testGetFormat() {
$this->assertEquals($this->logger->getFormat(), FORMAT);
}
function test_get_level() {
$this->assertEquals($this->logger->get_level(), Logger::WARNING);
$this->assertEquals($this->logger->get_level_name(), 'WARNING');
function testGetLevel() {
$this->assertEquals($this->logger->getLevel(), Logger::WARNING);
$this->assertEquals($this->logger->getLevelName(), 'WARNING');
}
/**
* @depends test_get_level
* @depends testGetLevel
*/
function test_set_level() {
$this->logger->set_level('info');
$this->assertEquals($this->logger->get_level(), Logger::INFO);
$this->logger->set_level('DEBUG');
$this->assertEquals($this->logger->get_level(), Logger::DEBUG);
$this->logger->set_level('WaRnInG');
$this->assertEquals($this->logger->get_level(), Logger::WARNING);
$this->logger->set_level(Logger::ERROR);
$this->assertEquals($this->logger->get_level(), Logger::ERROR);
}
function test_format() {
function testSetLevel() {
$this->logger->setLevel('info');
$this->assertEquals($this->logger->getLevel(), Logger::INFO);
$this->logger->setLevel('DEBUG');
$this->assertEquals($this->logger->getLevel(), Logger::DEBUG);
$this->logger->setLevel('WaRnInG');
$this->assertEquals($this->logger->getLevel(), Logger::WARNING);
$this->logger->setLevel(Logger::ERROR);
$this->assertEquals($this->logger->getLevel(), Logger::ERROR);
}
function testFormat() {
$this->logger->error('test message');
$this->assert_dumps('ERROR: test message');
$this->assertDumps('ERROR: test message');
}
function test_set_property() {
$this->logger->set_property('name', 'Logger');
$this->assertEquals($this->logger->get_formatted_property('name'), 'Logger');
function testSetProperty() {
$this->logger->setProperty('name', 'Logger');
$this->assertEquals($this->logger->getFormattedProperty('name'), 'Logger');
}
/**
* @depends test_format
* @depends testFormat
*/
function test_clear() {
function testClear() {
$this->logger->warning('test message');
$this->logger->clear();
$this->assert_dumps('');
$this->assertDumps('');
}
/**
* @depends test_set_level
* @depends test_clear
* @depends testSetLevel
* @depends testClear
*/
function test_process_level() {
function testProcessLevel() {
$this->logger->info('test message');
$this->assert_dumps('');
$this->assertDumps('');
$this->logger->warning('test message');
$this->assert_dumps('WARNING: test message');
$this->assertDumps('WARNING: test message');
$this->logger->critical('test message');
$this->assert_dumps("WARNING: test message\nCRITICAL: test message");
$this->assertDumps("WARNING: test message\nCRITICAL: test message");
$this->logger->clear();
$this->logger->set_level('debug');
$this->logger->setLevel('debug');
$this->logger->debug('test message');
$this->assert_dumps('DEBUG: test message');
$this->assertDumps('DEBUG: test message');
}
function test_get_formatted_property() {
$this->assertEquals($this->logger->get_formatted_property('name'), NAME);
$this->assertEquals($this->logger->get_formatted_property('loglevel'), 'WARNING');
function testGetFormattedProperty() {
$this->assertEquals($this->logger->getFormattedProperty('name'), NAME);
$this->assertEquals($this->logger->getFormattedProperty('loglevel'), 'WARNING');
$this->assertRegExp('/^\d{2}-\d{2}-\d{4}$/',
$this->logger->get_formatted_property('date'));
$this->logger->getFormattedProperty('date'));
$this->assertRegExp('/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}$/',
$this->logger->get_formatted_property('datetime'));
$this->logger->getFormattedProperty('datetime'));
$this->assertRegExp('/^\d{2}:\d{2}:\d{2}$/',
$this->logger->get_formatted_property('time'));
$this->logger->getFormattedProperty('time'));
$this->setExpectedException('\InvalidArgumentException');
$this->logger->get_formatted_property('foo');
$this->logger->getFormattedProperty('foo');
}
function test_dumps_property_format() {
function testDumpsPropertyFormat() {
$this->logger->warning('test message');
$this->logger->set_format('%(name): %(level): %(message)');
$this->assert_dumps(NAME.': WARNING: test message');
$this->logger->setFormat('%(name): %(level): %(message)');
$this->assertDumps(NAME.': WARNING: test message');
}
/**
* @depends test_process_level
* @depends testProcessLevel
*/
function test_dump_plain() {
function testDumpPlain() {
$this->logger->warning('test message');
$this->expectOutputString('WARNING: test message');
$this->logger->dump();
}
/**
* @depends test_process_level
* @depends testProcessLevel
*/
function test_dump_html() {
function testDumpHtml() {
$this->logger->warning('test message');
$this->logger->set_dump_format('html');
$this->logger->setDumpFormat('html');
$this->expectOutputString('<strong>Log:</strong><br /><pre>WARNING: test message</pre>');
$this->logger->dump();
}
function test_save() {
function testSave() {
$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_logfile() {
function findLogfile() {
$files = scandir(LOGDIR);
$this->assertEquals(3, count($files));
return $files[2];
}
/**
* @depends test_save
* @depends testSave
*/
function test_dump_file_regular() {
$this->logger->set_directory(LOGDIR);
$this->logger->set_dump_format('file');
function testDumpFileRegular() {
$this->logger->setDirectory(LOGDIR);
$this->logger->setDumpFormat('file');
$this->logger->warning('test message');
$this->logger->dump();
$filename = $this->find_logfile();
$filename = $this->findLogfile();
$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_exception() {
$this->logger->set_dump_format('none');
$this->logger->handle_exception(new RuntimeException('test message'));
function testHandleException() {
$this->logger->setDumpFormat('none');
$this->logger->handleException(new RuntimeException('test message'));
$this->assertNotEquals($this->logger->dumps(), '');
}
}
......
......@@ -10,90 +10,90 @@ class NodeTest extends PHPUnit_Framework_TestCase {
$this->root = new Node('test node');
}
function test_get_id() {
$this->assertEquals($this->root->get_id(), 1);
$this->assertEquals(Node::create('')->get_id(), 2);
function testGetId() {
$this->assertEquals($this->root->getId(), 1);
$this->assertEquals(Node::create('')->getId(), 2);
}
function test_get_name() {
$this->assertEquals($this->root->get_name(), 'test node');
$this->assertEquals(Node::create('second node')->get_name(), 'second node');
function testGetName() {
$this->assertEquals($this->root->getName(), 'test node');
$this->assertEquals(Node::create('second node')->getName(), 'second node');
}
function test_get_parent() {
$this->assertNull($this->root->get_parent());
$this->assertSame(Node::create('', $this->root)->get_parent(), $this->root);
function testGetParent() {
$this->assertNull($this->root->getParent());
$this->assertSame(Node::create('', $this->root)->getParent(), $this->root);
}
function test_is() {
function testIs() {
$mirror = $this->root;
$this->assertTrue($mirror->is($this->root));
$this->assertFalse(Node::create('')->is($this->root));
}
function test_is_root() {
$this->assertTrue($this->root->is_root());
$this->assertFalse(Node::create('', $this->root)->is_root());
function testIsRoot() {
$this->assertTrue($this->root->isRoot());
$this->assertFalse(Node::create('', $this->root)->isRoot());
}
function test_add_child() {
function testAddChild() {
$node = new Node('');
$this->root->add_child($node);
$this->root->addChild($node);
$this->assertAttributeEquals(array($node), 'children', $this->root);
$this->assertSame($node->get_parent(), $this->root);
$this->assertSame($node->getParent(), $this->root);
}
/**
* @depends test_add_child
* @depends testAddChild
*/
function test_get_children() {
$this->assertEquals($this->root->get_children(), array());
function testGetChildren() {
$this->assertEquals($this->root->getChildren(), array());
$node = new Node('');
$this->root->add_child($node);
$this->assertSame($this->root->get_children(), array($node));
$this->root->addChild($node);
$this->assertSame($this->root->getChildren(), array($node));
}
function test_add_child_no_set_parent() {
function testAddChildNoSetParent() {
$node = new Node('');
$this->root->add_child($node, false);
$this->root->addChild($node, false);
$this->assertAttributeEquals(array($node), 'children', $this->root);
$this->assertNull($node->get_parent());
$this->assertNull($node->getParent());
}
/**
* @depends test_add_child
* @depends testAddChild
*/
function test_is_leaf() {
function testIsLeaf() {
$node = new Node('');
$this->root->add_child($node);
$this->assertTrue($node->is_leaf());
$this->assertFalse($this->root->is_leaf());
$this->root->addChild($node);
$this->assertTrue($node->isLeaf());
$this->assertFalse($this->root->isLeaf());
}
/**
* @depends test_add_child
* @depends testAddChild
*/
function test_add() {
function testAdd() {
$node = $this->root->add('name', array('foo' => 'bar'));
$this->assertEquals($node->get_name(), 'name');
$this->assertEquals($node->getName(), 'name');
$this->assertEquals($node->get('foo'), 'bar');
$this->assertSame($node->get_parent(), $this->root);
$this->assertSame($node->getParent(), $this->root);
}
/**
* @depends test_add
* @depends testAdd
*/
function test_remove_child() {
function testRemoveChild() {
$node1 = $this->root->add('name', array('foo' => 'bar'));
$node2 = $this->root->add('name', array('foo' => 'bar'));
$this->root->remove_child($node2);
$this->root->removeChild($node2);
$this->assertAttributeSame(array($node1), 'children', $this->root);
}
/**
* @depends test_remove_child
* @depends testRemoveChild
*/
function test_remove_leaf() {
function testRemoveLeaf() {
$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_leaf
* @depends testRemoveLeaf
*/
function test_remove_node() {
function testRemoveNode() {
$node = $this->root->add('node');
$leaf = $node->add('leaf');
$node->remove();
$this->assertAttributeEquals(array(), 'children', $this->root);
$this->assertNull($leaf->get_parent());
$this->assertNull($leaf->getParent());
}
/**
* @depends test_remove_child
* @depends testRemoveChild
* @expectedException \RuntimeException
*/
function test_remove_root() {
function testRemoveRoot() {
$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_single() {
function testSetSingle() {
$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_return() {
function testSetReturn() {
$this->assertSame($this->root->set('foo', 'bar'), $this->root);
}
function test_set_multiple() {
function testSetMultiple() {
$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_single
* @depends testSetSingle
*/
function test___set() {
$this->root->foo = 'bar';
......@@ -151,16 +151,16 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test_set_multiple
* @depends testSetMultiple
*/
function test_get_direct() {
function testGetDirect() {
$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_direct
* @depends testGetDirect
*/
function test___get() {
$this->root->set(array('foo' => 'bar', 'bar' => 'baz'));
......@@ -169,22 +169,22 @@ class NodeTest extends PHPUnit_Framework_TestCase {
}
/**
* @depends test_set_single
* @depends testSetSingle
*/
function test_get_ancestor() {
function testGetAncestor() {
$this->root->set('foo', 'bar');
$node = $this->root->add('');
$this->assertEquals($node->get('foo'), 'bar');
}
function test_get_failure() {
function testGetFailure() {
$this->assertNull($this->root->get('foo'));
}
/**
* @depends test_get_name
* @depends testGetName
*/
function test_find() {
function testFind() {
$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_multiple
* @depends testSetMultiple
*/
function test_copy_simple() {
function testCopySimple() {
$copy = $this->root->copy();
$this->assertEquals($this->root, $copy);
$this->assertNotSame($this->root, $copy);
}
/**
* @depends test_copy_simple
* @depends testCopySimple
*/
function test_copy_shallow() {
function testCopyShallow() {
$child = $this->root->add('');
$copy = $this->root->copy();
$this->assertAttributeSame(array($child), 'children', $copy);
}
/**
* @depends test_get_children
* @depends test_copy_simple
* @depends testGetChildren
* @depends testCopySimple
*/
function test_copy_deep() {
function testCopyDeep() {
$child = $this->root->add('foo');
$copy = $this->root->copy(true);
$copy_children = $copy->get_children();
$copy_children = $copy->getChildren();
$child_copy = reset($copy_children);
$this->assertNotSame($copy_children, $this->root->get_children());
$this->assertSame($child_copy->get_parent(), $copy);
$this->assertNotSame($copy_children, $this->root->getChildren());
$this->assertSame($child_copy->getParent(), $copy);
}
}
......
......@@ -24,29 +24,29 @@ class RouterTest extends PHPUnit_Framework_TestCase {
));
}
function test_call_handler_success() {
$this->assertEquals(true, $this->router->call_handler('foo'));
$this->assertEquals('bar', $this->router->call_handler('bar'));
$this->assertEquals('baz', $this->router->call_handler('baz'));
$this->assertEquals('barbaz', $this->router->call_handler('bazbar'));
function testCallHandlerSuccess() {
$this->assertEquals(true, $this->router->callHandler('foo'));
$this->assertEquals('bar', $this->router->callHandler('bar'));
$this->assertEquals('baz', $this->router->callHandler('baz'));
$this->assertEquals('barbaz', $this->router->callHandler('bazbar'));
}
function test_call_handler_failure() {
$this->assertFalse($this->router->call_handler('barfoo'));
function testCallHandlerFailure() {
$this->assertFalse($this->router->callHandler('barfoo'));
}
function test_call_handler_skip() {
function testCallHandlerSkip() {
$foo = 'foo';
$bar = function() use (&$foo) { $foo = 'bar'; return false; };
$baz = function() { return; };
$router = new Router(array('.*' => $bar, 'baz' => $baz));
$router->call_handler('baz');
$router->callHandler('baz');
$this->assertEquals('bar', $foo);
}
function test_add_route() {
$this->router->add_route('(foobar)', 'test_handler_arg');
$this->assertEquals('foobar', $this->router->call_handler('foobar'));
function testAddRoute() {
$this->router->addRoute('(foobar)', 'test_handler_arg');
$this->assertEquals('foobar', $this->router->callHandler('foobar'));
}
}
......
......@@ -24,10 +24,10 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
const INTERNATIONALIZATION_STRING = 'Itrntinliztin';
/**
* @depends test_add_root_success
* @depends testAddRootSuccess
*/
function setUp() {
Template::set_root(TEMPLATES_DIR);
Template::setRoot(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_failure() {
Template::add_root('non_existing_folder');
function testAddRootFailure() {
Template::addRoot('non_existing_folder');
}
function assert_include_path_equals($expected) {
function assertIncludePathEquals($expected) {
$include_path = new ReflectionProperty('webbasics\Template', 'include_path');
$include_path->setAccessible(true);
$this->assertEquals($expected, $include_path->getValue());
}
function test_clear_include_path() {
Template::clear_include_path();
$this->assert_include_path_equals(array());
function testClearIncludePath() {
Template::clearIncludePath();
$this->assertIncludePathEquals(array());
}
/**
* @depends test_clear_include_path
* @depends testClearIncludePath
*/
function test_add_root_success() {
Template::clear_include_path();
Template::add_root(TEMPLATES_DIR);
$this->assert_include_path_equals(array(TEMPLATES_DIR));
Template::add_root('tests/_files');
$this->assert_include_path_equals(array(TEMPLATES_DIR, 'tests/_files/'));
function testAddRootSuccess() {
Template::clearIncludePath();
Template::addRoot(TEMPLATES_DIR);
$this->assertIncludePathEquals(array(TEMPLATES_DIR));
Template::addRoot('tests/_files');
$this->assertIncludePathEquals(array(TEMPLATES_DIR, 'tests/_files/'));
}
/**
* @depends test_add_root_success
* @depends testAddRootSuccess
*/
function test_set_root() {
Template::clear_include_path();
Template::add_root(TEMPLATES_DIR);
Template::add_root('tests/_files');
Template::set_root(TEMPLATES_DIR);
$this->assert_include_path_equals(array(TEMPLATES_DIR));
function testSetRoot() {
Template::clearIncludePath();
Template::addRoot(TEMPLATES_DIR);
Template::addRoot('tests/_files');
Template::setRoot(TEMPLATES_DIR);
$this->assertIncludePathEquals(array(TEMPLATES_DIR));
}
/**
* @expectedException RuntimeException
*/
function test_non_existing_template() {
function testNonExistingTemplate() {
$bar = new Template('bar');
}
function test_other_root() {
Template::add_root('tests/_files/other_templates');
function testOtherRoot() {
Template::addRoot('tests/_files/other_templates');
new Template('bar');
}
function test_get_path() {
$this->assertEquals(TEMPLATES_DIR.'foo.tpl', $this->tpl->get_path());
function testGetPath() {
$this->assertEquals(TEMPLATES_DIR.'foo.tpl', $this->tpl->getPath());
}
function get_property($object, $property_name) {
function getProperty($object, $property_name) {
$rp = new ReflectionProperty($object, $property_name);
$rp->setAccessible(true);
return $rp->getValue($object);
}
static function strip_newlines($html) {
static function stripNewlines($html) {
return str_replace("\r\n", "\n", $html);
}
function assert_is_html_node($node, $content) {
$this->assertEquals('html', $node->get_name());
$this->assertEquals($content, self::strip_newlines($node->get('content')));
$this->assertEquals(array(), $node->get_children());
function assertIsHtmlNode($node, $content) {
$this->assertEquals('html', $node->getName());
$this->assertEquals($content, self::stripNewlines($node->get('content')));
$this->assertEquals(array(), $node->getChildren());
}
function assert_is_block_node($node, $block_name, $child_count) {
$this->assertEquals('block', $node->get_name());
function assertIsBlockNode($node, $block_name, $child_count) {
$this->assertEquals('block', $node->getName());
$this->assertSame($block_name, $node->get('name'));
$this->assertNull($node->get('content'));
$this->assertEquals($child_count, count($node->get_children()));
$this->assertEquals($child_count, count($node->getChildren()));
}
function assert_is_exp_node($node, $brackets_content) {
$this->assertEquals('expression', $node->get_name());
function assertIsExpNode($node, $brackets_content) {
$this->assertEquals('expression', $node->getName());
$this->assertEquals($brackets_content, $node->get('content'));
$this->assertEquals(array(), $node->get_children());
$this->assertEquals(array(), $node->getChildren());
}
function test_parse_blocks_simple() {
$root_block = $this->get_property($this->tpl, 'root_block');
$this->assert_is_block_node($root_block, null, 1);
function testParseBlocksSimple() {
$root_block = $this->getProperty($this->tpl, 'root_block');
$this->assertIsBlockNode($root_block, null, 1);
list($child) = $root_block->get_children();
$this->assert_is_html_node($child, 'test');
list($child) = $root_block->getChildren();
$this->assertIsHtmlNode($child, 'test');
}
/**
* @depends test_parse_blocks_simple
* @depends testParseBlocksSimple
*/
function test_parse_blocks_blocks() {
function testParseBlocksBlocks() {
$tpl = new Template('blocks');
$root_block = $this->get_property($tpl, 'root_block');
$this->assert_is_block_node($root_block, null, 2);
$root_block = $this->getProperty($tpl, 'root_block');
$this->assertIsBlockNode($root_block, null, 2);
list($before, $foo) = $root_block->get_children();
$this->assert_is_html_node($before, '');
$this->assert_is_block_node($foo, 'foo', 3);
list($before, $foo) = $root_block->getChildren();
$this->assertIsHtmlNode($before, '');
$this->assertIsBlockNode($foo, 'foo', 3);
list($foofoo, $bar, $foobaz) = $foo->get_children();
$this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
$this->assert_is_block_node($bar, 'bar', 1);
$this->assert_is_html_node($foobaz, "\nfoobaz\n");
list($foofoo, $bar, $foobaz) = $foo->getChildren();
$this->assertIsHtmlNode($foofoo, "\nfoofoo\n\t");
$this->assertIsBlockNode($bar, 'bar', 1);
$this->assertIsHtmlNode($foobaz, "\nfoobaz\n");
list($foobar) = $bar->get_children();
$this->assert_is_html_node($foobar, "\n\tfoobar\n\t");
list($foobar) = $bar->getChildren();
$this->assertIsHtmlNode($foobar, "\n\tfoobar\n\t");
}
/**
* @depends test_parse_blocks_blocks
* @depends testParseBlocksBlocks
* @expectedException webbasics\ParseError
* @expectedExceptionMessage Parse error in file tests/_files/templates/unexpected_end.tpl, line 5: unexpected {end}
*/
function test_parse_blocks_unexpected_end() {
function testParseBlocksUnexpectedEnd() {
new Template('unexpected_end');
}
/**
* @depends test_parse_blocks_blocks
* @depends testParseBlocksBlocks
* @expectedException webbasics\ParseError
* @expectedExceptionMessage Parse error in file tests/_files/templates/missing_end.tpl, line 6: missing {end}
*/
function test_parse_blocks_missing_end() {
function testParseBlocksMissingEnd() {
new Template('missing_end');
}
/**
* @depends test_parse_blocks_simple
* @depends testParseBlocksSimple
*/
function test_parse_blocks_variables() {
function testParseBlocksVariables() {
$tpl = new Template('variables');
$root_block = $this->get_property($tpl, 'root_block');
$this->assert_is_block_node($root_block, null, 5);
$root_block = $this->getProperty($tpl, 'root_block');
$this->assertIsBlockNode($root_block, null, 5);
list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->get_children();
$this->assert_is_html_node($foo, "foo\n");
$this->assert_is_exp_node($foobar, '$foobar');
$this->assert_is_html_node($bar, "\nbar\n");
$this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
$this->assert_is_html_node($baz, "\nbaz\n{\nno_variable\n}");
list($foo, $foobar, $bar, $foobaz, $baz) = $root_block->getChildren();
$this->assertIsHtmlNode($foo, "foo\n");
$this->assertIsExpNode($foobar, '$foobar');
$this->assertIsHtmlNode($bar, "\nbar\n");
$this->assertIsExpNode($foobaz, 'strtolower($foobaz)');
$this->assertIsHtmlNode($baz, "\nbaz\n{\nno_variable\n}");
}
/**
* @depends test_parse_blocks_blocks
* @depends test_parse_blocks_variables
* @depends testParseBlocksBlocks
* @depends testParseBlocksVariables
*/
function test_parse_blocks_full() {
function testParseBlocksFull() {
$tpl = new Template('full');
$root_block = $this->get_property($tpl, 'root_block');
$this->assert_is_block_node($root_block, null, 3);
$root_block = $this->getProperty($tpl, 'root_block');
$this->assertIsBlockNode($root_block, null, 3);
list($bar, $foo, $baz) = $root_block->get_children();
$this->assert_is_html_node($bar, "bar\n");
$this->assert_is_block_node($foo, 'foo', 5);
$this->assert_is_html_node($baz, "\nbaz");
list($bar, $foo, $baz) = $root_block->getChildren();
$this->assertIsHtmlNode($bar, "bar\n");
$this->assertIsBlockNode($foo, 'foo', 5);
$this->assertIsHtmlNode($baz, "\nbaz");
list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->get_children();
$this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
$this->assert_is_block_node($bar, 'bar', 3);
$this->assert_is_html_node($first_space, "\n");
$this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
$this->assert_is_html_node($second_space, "\n");
list($foofoo, $bar, $first_space, $foobaz, $second_space) = $foo->getChildren();
$this->assertIsHtmlNode($foofoo, "\nfoofoo\n\t");
$this->assertIsBlockNode($bar, 'bar', 3);
$this->assertIsHtmlNode($first_space, "\n");
$this->assertIsExpNode($foobaz, 'strtolower($foobaz)');
$this->assertIsHtmlNode($second_space, "\n");
list($space_before, $foobar, $space_after) = $bar->get_children();
$this->assert_is_html_node($space_before, "\n\t");
$this->assert_is_exp_node($foobar, '$foobar');
$this->assert_is_html_node($space_after, "\n\t");
list($space_before, $foobar, $space_after) = $bar->getChildren();
$this->assertIsHtmlNode($space_before, "\n\t");
$this->assertIsExpNode($foobar, '$foobar');
$this->assertIsHtmlNode($space_after, "\n\t");
}
function evaluate_expression() {
function evaluateExpression() {
$args = func_get_args();
$eval = new ReflectionMethod('webbasics\Template', 'evaluate_expression');
$eval = new ReflectionMethod('webbasics\Template', 'evaluateExpression');
$eval->setAccessible(true);
return $eval->invokeArgs(null, $args);
}
function assert_evaluates($expected, $expression) {
$this->assertEquals($expected, $this->evaluate_expression($expression, $this->data));
function assertEvaluates($expected, $expression) {
$this->assertEquals($expected, $this->evaluateExpression($expression, $this->data));
}
/**
* @expectedException \UnexpectedValueException
*/
function test_evaluate_variable_attribute_null() {
$this->evaluate_expression('$foobarbaz.foo', $this->data);
function testEvaluateVariableAttributeNull() {
$this->evaluateExpression('$foobarbaz.foo', $this->data);
}
/**
* @expectedException \UnexpectedValueException
*/
function test_evaluate_variable_attribute_no_such_attribute() {
$this->evaluate_expression('$object.foobar', $this->data);
function testEvaluateVariableAttributeNoSuchAttribute() {
$this->evaluateExpression('$object.foobar', $this->data);
}
/**
* @expectedException \UnexpectedValueException
*/
function test_evaluate_variable_attribute_no_array_or_object() {
$this->evaluate_expression('$foo.bar', $this->data);
function testEvaluateVariableAttributeNoArrayOrObject() {
$this->evaluateExpression('$foo.bar', $this->data);
}
/**
* @expectedException \UnexpectedValueException
*/
function test_evaluate_variable_method_null() {
$this->evaluate_expression('$foobarbaz.foo()', $this->data);
function testEvaluateVariableMethodNull() {
$this->evaluateExpression('$foobarbaz.foo()', $this->data);
}
/**
* @expectedException \BadMethodCallException
*/
function test_evaluate_variable_method_no_such_method() {
$this->evaluate_expression('$object.foo()', $this->data);
function testEvaluateVariableMethodNoSuchMethod() {
$this->evaluateExpression('$object.foo()', $this->data);
}
/**
* @expectedException \BadMethodCallException
*/
function test_evaluate_variable_method_no_object() {
$this->evaluate_expression('$foo.bar()', $this->data);
function testEvaluateVariableMethodNoObject() {
$this->evaluateExpression('$foo.bar()', $this->data);
}
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 testEvaluateVariableSuccess() {
$this->assertEvaluates('bar', '$array.foo');
$this->assertEvaluates('bar', '$foo');
$this->assertEvaluates('baz', '$bar');
$this->assertEvaluates('bar', '$object.foo');
$this->assertEvaluates('baz', '$object.bar');
$this->assertEvaluates('foobar', '$object.baz()');
}
/**
* @depends test_evaluate_variable_success
* @depends testEvaluateVariableSuccess
*/
function test_evaluate_variable_escape() {
$this->assert_evaluates('&lt;script&gt;&lt;/script&gt;', '$html');
$this->assert_evaluates('Itrntinliztin', '$internationalization');
//$this->assert_evaluates('I&ntilde;t&euml;rn&acirc;ti&ocirc;n&agrave;liz&aelig;ti&oslash;n', '$internationalization');
function testEvaluateVariableEscape() {
$this->assertEvaluates('&lt;script&gt;&lt;/script&gt;', '$html');
$this->assertEvaluates('Itrntinliztin', '$internationalization');
//$this->assertEvaluates('I&ntilde;t&euml;rn&acirc;ti&ocirc;n&agrave;liz&aelig;ti&oslash;n', '$internationalization');
}
/**
* @depends test_evaluate_variable_success
* @depends testEvaluateVariableSuccess
*/
function test_evaluate_variable_noescape() {
$this->assert_evaluates('<script></script>', '$$html');
$this->assert_evaluates('Itrntinliztin', '$$internationalization');
function testEvaluateVariableNoescape() {
$this->assertEvaluates('<script></script>', '$$html');
$this->assertEvaluates('Itrntinliztin', '$$internationalization');
}
function test_evaluate_constant() {
$this->assert_evaluates('foobar_const', 'FOOBAR');
$this->assert_evaluates('{NON_DEFINED_CONST}', 'NON_DEFINED_CONST');
function testEvaluateConstant() {
$this->assertEvaluates('foobar_const', 'FOOBAR');
$this->assertEvaluates('{NON_DEFINED_CONST}', 'NON_DEFINED_CONST');
}
function test_evaluate_no_expression() {
$this->assert_evaluates('{foo}', 'foo');
function testEvaluateNoExpression() {
$this->assertEvaluates('{foo}', 'foo');
}
function test_evaluate_condition_if() {
$this->assert_evaluates('bar', '$true?bar');
$this->assert_evaluates('', '$false?bar');
function testEvaluateConditionIf() {
$this->assertEvaluates('bar', '$true?bar');
$this->assertEvaluates('', '$false?bar');
}
function test_evaluate_condition_if_else() {
$this->assert_evaluates('bar', '$true?bar:baz');
$this->assert_evaluates('baz', '$false?bar:baz');
function testEvaluateConditionIfElse() {
$this->assertEvaluates('bar', '$true?bar:baz');
$this->assertEvaluates('baz', '$false?bar:baz');
}
/**
* @depends test_evaluate_condition_if
* @depends test_evaluate_condition_if_else
* @depends testEvaluateConditionIf
* @depends testEvaluateConditionIfElse
*/
function test_evaluate_condition_extended() {
$this->assert_evaluates(' bar ', '$true? bar : baz');
$this->assert_evaluates(' baz', '$false? bar : baz');
function testEvaluateConditionExtended() {
$this->assertEvaluates(' bar ', '$true? bar : baz');
$this->assertEvaluates(' baz', '$false? bar : baz');
$this->assert_evaluates(' bar ', '$true ? bar : baz');
$this->assert_evaluates(' baz', '$false ? bar : baz');
$this->assertEvaluates(' bar ', '$true ? bar : baz');
$this->assertEvaluates(' baz', '$false ? bar : baz');
$this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
$this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo');
$this->assertEvaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
$this->assertEvaluates(' Baz foo', '$false ? Foo bar : Baz foo');
$this->assert_evaluates('| bar', '$true ?| $foo');
$this->assertEvaluates('| bar', '$true ?| $foo');
}
/**
* @expectedException \BadFunctionCallException
*/
function test_evaluate_function_error() {
$this->evaluate_expression('undefined_function($foo)', $this->data);
function testEvaluateFunctionError() {
$this->evaluateExpression('undefined_function($foo)', $this->data);
}
function test_evaluate_function_success() {
$this->assert_evaluates('Bar', 'ucfirst($foo)');
$this->assert_evaluates('Bar', 'DataObject::foobar($foo)');
function testEvaluateFunctionSuccess() {
$this->assertEvaluates('Bar', 'ucfirst($foo)');
$this->assertEvaluates('Bar', 'DataObject::foobar($foo)');
}
/**
* @depends test_evaluate_function_success
* @depends testEvaluateFunctionSuccess
*/
function test_evaluate_function_nested() {
$this->assert_evaluates('Bar', 'ucfirst(strtolower($FOO))');
function testEvaluateFunctionNested() {
$this->assertEvaluates('Bar', 'ucfirst(strtolower($FOO))');
}
function test_evaluate_default_value() {
$this->assert_evaluates('bar', '$foo||fallback');
$this->assert_evaluates('fallback', '$foo.bar||fallback');
$this->assert_evaluates('', '$foo.bar||');
function testEvaluateDefaultValue() {
$this->assertEvaluates('bar', '$foo||fallback');
$this->assertEvaluates('fallback', '$foo.bar||fallback');
$this->assertEvaluates('', '$foo.bar||');
}
/**
* @depends test_evaluate_variable_success
* @depends test_evaluate_no_expression
* @depends test_evaluate_condition_extended
* @depends test_evaluate_function_success
* @depends test_evaluate_default_value
* @depends testEvaluateVariableSuccess
* @depends testEvaluateNoExpression
* @depends testEvaluateConditionExtended
* @depends testEvaluateFunctionSuccess
* @depends testEvaluateDefaultValue
*/
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 testEvaluateExpressionCombined() {
$this->assertEvaluates('Bar', '$true?ucfirst($foo)');
$this->assertEvaluates('', '$false?ucfirst($foo)');
$this->assertEvaluates('Bar', '$true?ucfirst($foo):baz');
$this->assertEvaluates('baz', '$false?ucfirst($foo):baz');
$this->assertEvaluates('Baz', 'ucfirst($array.bar)');
}
function assert_renders($expected_file, $tpl) {
function assertRenders($expected_file, $tpl) {
$expected_file = "tests/_files/rendered/$expected_file.html";
$this->assertEquals(self::strip_newlines(file_get_contents($expected_file)),
self::strip_newlines($tpl->render()));
$this->assertEquals(self::stripNewlines(file_get_contents($expected_file)),
self::stripNewlines($tpl->render()));
}
function test_render_simple() {
function testRenderSimple() {
$this->assertEquals('test', $this->tpl->render());
}
/**
* @depends test_evaluate_expression_combined
* @depends testEvaluateExpressionCombined
*/
function test_render_variable() {
function testRenderVariable() {
$tpl = new Template('variables');
$tpl->set(array(
'foobar' => 'my_foobar_variable',
'foobaz' => 'MY_FOOBAZ_VARIABLE'
));
$this->assert_renders('variables', $tpl);
$this->assertRenders('variables', $tpl);
}
/**
* @depends test_render_simple
* @depends testRenderSimple
*/
function test_render_blocks() {
function testRenderBlocks() {
$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_renders('blocks', $tpl);
$this->assertRenders('blocks', $tpl);
}
/**
* @depends test_render_variable
* @depends test_render_blocks
* @depends testRenderVariable
* @depends testRenderBlocks
*/
function test_render_full() {
function testRenderFull() {
$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_renders('full', $tpl);
$this->assertRenders('full', $tpl);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment