fenom/tests/TestCase.php

334 lines
9.6 KiB
PHP
Raw Normal View History

2013-02-15 01:49:26 +04:00
<?php
2013-06-28 11:53:53 +04:00
namespace Fenom;
2014-02-27 16:30:44 +04:00
2013-06-28 11:53:53 +04:00
use Fenom, Fenom\Provider as FS;
2013-02-15 01:49:26 +04:00
2017-04-22 16:20:55 +03:00
class TestCase extends \PHPUnit_Framework_TestCase
2013-07-29 14:58:14 +04:00
{
public $template_path = 'template';
2013-02-15 01:49:26 +04:00
/**
2013-06-28 11:53:53 +04:00
* @var Fenom
2013-02-15 01:49:26 +04:00
*/
2013-06-28 11:53:53 +04:00
public $fenom;
2013-04-04 10:56:44 +04:00
2014-05-08 12:56:37 +04:00
public $values;
2013-02-15 01:49:26 +04:00
2013-07-29 14:58:14 +04:00
public static function getVars()
{
2013-07-22 18:03:43 +04:00
return array(
2014-05-06 14:22:58 +04:00
"zero" => 0,
"one" => 1,
"two" => 2,
2013-07-22 18:03:43 +04:00
"three" => 3,
"float" => 4.5,
2014-05-06 14:22:58 +04:00
"bool" => true,
"obj" => new \StdClass,
"list" => array(
"a" => 1,
2013-08-03 18:56:17 +04:00
"one" => 1,
2014-05-06 14:22:58 +04:00
"b" => 2,
2013-08-03 18:56:17 +04:00
"two" => 2
2013-07-22 18:03:43 +04:00
),
2015-02-01 00:58:40 +03:00
"level1" => array(
"level2" => array(
"one" => 1,
"two" => 2
)
),
2014-05-08 12:56:37 +04:00
"num" => array(
1 => "one",
2 => "two",
3 => "three",
4 => "four"
),
2014-05-06 14:22:58 +04:00
0 => "empty value",
1 => "one value",
2 => "two value",
3 => "three value",
2013-07-22 18:03:43 +04:00
);
}
2016-06-09 16:00:18 +03:00
public function getCompilePath()
{
return FENOM_RESOURCES . '/compile';
}
2013-07-29 14:58:14 +04:00
public function setUp()
{
2016-06-09 16:00:18 +03:00
if (!file_exists($this->getCompilePath())) {
mkdir($this->getCompilePath(), 0777, true);
2013-02-15 01:49:26 +04:00
} else {
2016-06-09 16:00:18 +03:00
FS::clean($this->getCompilePath());
2013-02-15 01:49:26 +04:00
}
2013-07-22 18:03:43 +04:00
2016-06-09 16:00:18 +03:00
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/' . $this->template_path, $this->getCompilePath());
2014-05-06 14:22:58 +04:00
$this->fenom->addProvider('persist', new Provider(FENOM_RESOURCES . '/provider'));
2013-07-29 14:58:14 +04:00
$this->fenom->addModifier('dots', __CLASS__ . '::dots');
$this->fenom->addModifier('concat', __CLASS__ . '::concat');
2013-08-29 11:29:34 +04:00
$this->fenom->addModifier('append', __CLASS__ . '::append');
2013-07-29 14:58:14 +04:00
$this->fenom->addFunction('test_function', __CLASS__ . '::inlineFunction');
$this->fenom->addBlockFunction('test_block_function', __CLASS__ . '::blockFunction');
2014-05-08 12:56:37 +04:00
$this->values = $this->getVars();
}
2013-07-29 14:58:14 +04:00
public static function dots($value)
{
return $value . "...";
2013-02-15 01:49:26 +04:00
}
2013-07-29 14:58:14 +04:00
public static function concat()
{
return call_user_func_array('var_export', func_get_args());
}
2013-05-30 20:00:52 +04:00
2013-08-29 11:29:34 +04:00
public static function append()
{
return implode("", func_get_args());
}
2013-07-29 14:58:14 +04:00
public static function inlineFunction($params)
{
2013-07-07 01:34:19 +04:00
return isset($params["text"]) ? $params["text"] : "";
}
2013-07-29 14:58:14 +04:00
public static function blockFunction($params, $text)
{
2013-07-07 01:34:19 +04:00
return $text;
}
2013-07-29 14:58:14 +04:00
public static function setUpBeforeClass()
{
if (!file_exists(FENOM_RESOURCES . '/template')) {
mkdir(FENOM_RESOURCES . '/template', 0777, true);
2013-02-20 18:03:53 +04:00
} else {
2013-07-29 14:58:14 +04:00
FS::clean(FENOM_RESOURCES . '/template/');
2013-02-20 18:03:53 +04:00
}
}
2013-07-29 14:58:14 +04:00
public function tpl($name, $code)
{
$dir = dirname($name);
2013-07-29 14:58:14 +04:00
if ($dir != "." && !is_dir(FENOM_RESOURCES . '/template/' . $dir)) {
mkdir(FENOM_RESOURCES . '/template/' . $dir, 0777, true);
}
2013-07-29 14:58:14 +04:00
file_put_contents(FENOM_RESOURCES . '/template/' . $name, $code);
2013-09-14 19:55:53 +04:00
return filemtime(FENOM_RESOURCES . '/template/' . $name);
2013-02-20 18:03:53 +04:00
}
2016-10-09 23:40:37 +03:00
public function tpls(array $list) {
foreach($list as $name => $tpl) {
$this->tpl($name, $tpl);
}
}
2013-02-15 01:49:26 +04:00
/**
* Compile and execute template
*
* @param string $code source of template
* @param array $vars variables of template
* @param string $result expected result.
2013-07-20 21:27:31 +04:00
* @param int $options
2013-02-15 01:49:26 +04:00
* @param bool $dump dump source and result code (for debug)
2013-09-14 11:24:23 +04:00
* @return \Fenom\Template
2013-02-15 01:49:26 +04:00
*/
2013-07-29 14:58:14 +04:00
public function exec($code, $vars, $result, $options = 0, $dump = false)
{
2013-07-20 21:27:31 +04:00
$this->fenom->setOptions($options);
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->compileCode($code, "runtime.tpl");
2013-07-29 14:58:14 +04:00
if ($dump) {
2014-05-06 14:22:58 +04:00
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody(
) . "\n========= DUMP END =============\n";
2013-02-15 01:49:26 +04:00
}
2014-02-27 16:30:44 +04:00
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test $code");
2013-09-14 11:24:23 +04:00
return $tpl;
2013-02-15 01:49:26 +04:00
}
2013-07-29 14:58:14 +04:00
public function execTpl($name, $code, $vars, $result, $dump = false)
{
2013-02-20 18:03:53 +04:00
$this->tpl($name, $code);
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->getTemplate($name);
2013-07-29 14:58:14 +04:00
if ($dump) {
2014-05-06 14:22:58 +04:00
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody(
) . "\n========= DUMP END =============\n";
2013-02-20 18:03:53 +04:00
}
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test tpl $name");
}
2013-02-15 01:49:26 +04:00
/**
* Try to compile the invalid template
* @param string $code source of the template
* @param string $exception exception class
* @param string $message exception message
2013-06-28 11:53:53 +04:00
* @param int $options Fenom's options
2013-02-15 01:49:26 +04:00
*/
2013-07-29 14:58:14 +04:00
public function execError($code, $exception, $message, $options = 0)
{
2013-06-28 11:53:53 +04:00
$opt = $this->fenom->getOptions();
$this->fenom->setOptions($options);
2013-02-15 01:49:26 +04:00
try {
2013-06-28 11:53:53 +04:00
$this->fenom->compileCode($code, "inline.tpl");
2013-07-29 14:58:14 +04:00
} catch (\Exception $e) {
2013-02-15 01:49:26 +04:00
$this->assertSame($exception, get_class($e), "Exception $code");
$this->assertStringStartsWith($message, $e->getMessage());
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions($opt);
2013-02-15 01:49:26 +04:00
return;
}
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions($opt);
2013-02-15 01:49:26 +04:00
$this->fail("Code $code must be invalid");
}
2013-04-04 10:56:44 +04:00
2014-10-15 01:01:55 +04:00
public function assertRender($tpl, $result, array $vars = array(), $debug = false)
2013-07-29 14:58:14 +04:00
{
2013-06-28 11:53:53 +04:00
$template = $this->fenom->compileCode($tpl);
2013-07-29 14:58:14 +04:00
if ($debug) {
2013-09-14 19:55:53 +04:00
print_r("\nDEBUG $tpl:\n" . $template->getBody());
}
2014-10-15 01:01:55 +04:00
$this->assertSame($result, $template->fetch($vars + $this->values));
2013-09-14 19:55:53 +04:00
return $template;
2013-04-04 10:56:44 +04:00
}
2013-07-29 14:58:14 +04:00
public static function providerNumbers()
{
return array(
array('0', 0),
array('77', 77),
array('-33', -33),
array('0.2', 0.2),
array('-0.3', -0.3),
array('1e6', 1e6),
array('-2e6', -2e6),
);
}
2013-07-29 14:58:14 +04:00
public static function providerStrings()
{
return array(
array('"str"', 'str'),
array('"str\nand\nmany\nlines"', "str\nand\nmany\nlines"),
array('"str and \'substr\'"', "str and 'substr'"),
array('"str and \"substr\""', 'str and "substr"'),
array("'str'", 'str'),
array("'str\\nin\\none\\nline'", 'str\nin\none\nline'),
array("'str and \"substr\"'", 'str and "substr"'),
array("'str and \'substr\''", "str and 'substr'"),
array('"$one"', '1'),
array('"$one $two"', '1 2'),
array('"$one and $two"', '1 and 2'),
array('"a $one and $two b"', 'a 1 and 2 b'),
array('"{$one}"', '1'),
array('"a {$one} b"', 'a 1 b'),
array('"{$one + 2}"', '3'),
array('"{$one * $two + 1}"', '3'),
array('"{$one} and {$two}"', '1 and 2'),
array('"$one and {$two}"', '1 and 2'),
array('"{$one} and $two"', '1 and 2'),
array('"a {$one} and {$two} b"', 'a 1 and 2 b'),
array('"{$one+1} and {$two-1}"', '2 and 1'),
array('"a {$one+1} and {$two-1} b"', 'a 2 and 1 b'),
array('"a {$one|dots} and {$two|dots} b"', 'a 1... and 2... b'),
array('"a {$one|dots} and $two b"', 'a 1... and 2 b'),
array('"a $one and {$two|dots} b"', 'a 1 and 2... b'),
);
}
2013-07-29 14:58:14 +04:00
public function providerVariables()
{
2013-08-03 18:56:17 +04:00
return array(
array('$one', 1),
array('$list.one', 1),
array('$list[$$.DEVELOP]', 1),
);
2013-07-29 14:58:14 +04:00
}
2013-07-29 14:58:14 +04:00
public static function providerObjects()
{
return array();
}
2013-07-29 14:58:14 +04:00
public static function providerArrays()
{
$scalars = array();
2014-05-06 14:22:58 +04:00
$data = array(
2013-07-29 14:58:14 +04:00
array('[]', array()),
array('[[],[]]', array(array(), array())),
);
foreach (self::providerScalars() as $scalar) {
$scalars[0][] = $scalar[0];
$scalars[1][] = $scalar[1];
2013-07-29 14:58:14 +04:00
$data[] = array(
"[" . $scalar[0] . "]",
array($scalar[1])
);
$data[] = array(
"['some_key' =>" . $scalar[0] . "]",
array('some_key' => $scalar[1])
);
}
$data[] = array(
"[" . implode(", ", $scalars[0]) . "]",
$scalars[1]
);
return $data;
}
2013-07-29 14:58:14 +04:00
public static function providerScalars()
{
return array_merge(
self::providerNumbers(),
self::providerStrings()
);
}
2013-07-29 14:58:14 +04:00
public static function providerValues()
{
return array_merge(
self::providerScalars(),
self::providerArrays(),
self::providerVariables(),
self::providerObjects()
);
}
2013-04-04 10:56:44 +04:00
}
2013-12-01 19:30:09 +04:00
const HELPER_CONSTANT = 'helper.const';
2014-02-27 16:30:44 +04:00
class Helper
{
2013-12-01 19:30:09 +04:00
const CONSTANT = "helper.class.const";
2013-12-01 19:30:09 +04:00
public $word = 'helper';
2014-02-27 16:30:44 +04:00
public function __construct($word)
{
2013-12-01 19:30:09 +04:00
$this->word = $word;
$this->self = $this;
}
2014-08-23 11:41:21 +04:00
public static function method() {
return new \ArrayObject(array("page" => new \ArrayObject(array("title" => "test page"), \ArrayObject::ARRAY_AS_PROPS)), \ArrayObject::ARRAY_AS_PROPS);
}
2014-02-27 16:30:44 +04:00
public function chunk()
{
2013-12-01 19:30:09 +04:00
return $this;
}
2014-02-27 16:30:44 +04:00
public function __toString()
{
2013-12-01 19:30:09 +04:00
return $this->word;
}
2014-07-03 11:28:59 +04:00
public function getArray() {
return array(1,2,3);
}
2013-12-01 19:30:09 +04:00
}
2014-08-23 11:41:21 +04:00
function helper_func($string, $pad = 10) {
return str_pad($string, $pad, ".");
}