2013-02-15 01:49:26 +04:00
|
|
|
<?php
|
2013-04-04 10:56:44 +04:00
|
|
|
namespace Cytro;
|
|
|
|
use Cytro, Cytro\FSProvider as FS;
|
2013-02-15 01:49:26 +04:00
|
|
|
|
|
|
|
class TestCase extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
2013-04-04 10:56:44 +04:00
|
|
|
* @var Cytro
|
2013-02-15 01:49:26 +04:00
|
|
|
*/
|
2013-04-04 10:56:44 +04:00
|
|
|
public $cytro;
|
|
|
|
|
|
|
|
public $values = array(
|
|
|
|
"one" => 1,
|
|
|
|
"two" => 2,
|
|
|
|
"three" => 3,
|
|
|
|
1 => "one value",
|
|
|
|
2 => "two value",
|
|
|
|
3 => "three value",
|
|
|
|
);
|
2013-02-15 01:49:26 +04:00
|
|
|
|
|
|
|
public function setUp() {
|
2013-04-04 10:56:44 +04:00
|
|
|
if(!file_exists(CYTRO_RESOURCES.'/compile')) {
|
|
|
|
mkdir(CYTRO_RESOURCES.'/compile', 0777, true);
|
2013-02-15 01:49:26 +04:00
|
|
|
} else {
|
2013-04-04 10:56:44 +04:00
|
|
|
FS::clean(CYTRO_RESOURCES.'/compile/');
|
2013-02-15 01:49:26 +04:00
|
|
|
}
|
2013-04-04 10:56:44 +04:00
|
|
|
$this->cytro = Cytro::factory(CYTRO_RESOURCES.'/template', CYTRO_RESOURCES.'/compile');
|
2013-02-15 01:49:26 +04:00
|
|
|
}
|
|
|
|
|
2013-02-20 18:03:53 +04:00
|
|
|
public static function setUpBeforeClass() {
|
2013-04-04 10:56:44 +04:00
|
|
|
if(!file_exists(CYTRO_RESOURCES.'/template')) {
|
|
|
|
mkdir(CYTRO_RESOURCES.'/template', 0777, true);
|
2013-02-20 18:03:53 +04:00
|
|
|
} else {
|
2013-04-04 10:56:44 +04:00
|
|
|
FS::clean(CYTRO_RESOURCES.'/template/');
|
2013-02-20 18:03:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tpl($name, $code) {
|
2013-04-04 10:56:44 +04:00
|
|
|
file_put_contents(CYTRO_RESOURCES.'/template/'.$name, $code);
|
2013-02-20 18:03:53 +04:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @param bool $dump dump source and result code (for debug)
|
|
|
|
*/
|
|
|
|
public function exec($code, $vars, $result, $dump = false) {
|
2013-04-04 10:56:44 +04:00
|
|
|
$tpl = $this->cytro->compileCode($code, "runtime.tpl");
|
2013-02-15 01:49:26 +04:00
|
|
|
if($dump) {
|
|
|
|
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
|
|
|
|
}
|
|
|
|
$this->assertSame(Modifier::strip($result), Modifier::strip($tpl->fetch($vars), true), "Test $code");
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:03:53 +04:00
|
|
|
public function execTpl($name, $code, $vars, $result, $dump = false) {
|
|
|
|
$this->tpl($name, $code);
|
2013-04-04 10:56:44 +04:00
|
|
|
$tpl = $this->cytro->getTemplate($name);
|
2013-02-20 18:03:53 +04:00
|
|
|
if($dump) {
|
|
|
|
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
|
|
|
|
}
|
|
|
|
$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-04-04 10:56:44 +04:00
|
|
|
* @param int $options Cytro's options
|
2013-02-15 01:49:26 +04:00
|
|
|
*/
|
|
|
|
public function execError($code, $exception, $message, $options = 0) {
|
2013-04-04 10:56:44 +04:00
|
|
|
$opt = $this->cytro->getOptions();
|
|
|
|
$this->cytro->setOptions($options);
|
2013-02-15 01:49:26 +04:00
|
|
|
try {
|
2013-04-04 10:56:44 +04:00
|
|
|
$this->cytro->compileCode($code, "inline.tpl");
|
2013-02-15 01:49:26 +04:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
$this->assertSame($exception, get_class($e), "Exception $code");
|
|
|
|
$this->assertStringStartsWith($message, $e->getMessage());
|
2013-04-04 10:56:44 +04:00
|
|
|
$this->cytro->setOptions($opt);
|
2013-02-15 01:49:26 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-04-04 10:56:44 +04:00
|
|
|
$this->cytro->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
|
|
|
|
|
|
|
public function assertRender($tpl, $result) {
|
|
|
|
$template = $this->cytro->compileCode($tpl);
|
|
|
|
$this->assertSame($result, $template->fetch($this->values));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fake implements \ArrayAccess {
|
|
|
|
public $vars;
|
|
|
|
|
|
|
|
public function offsetExists($offset) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetGet($offset) {
|
|
|
|
if($offset == "object") {
|
|
|
|
return new self();
|
|
|
|
} else {
|
|
|
|
return new self($offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetSet($offset, $value) {
|
|
|
|
$this->vars[$offset] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetUnset($offset) {
|
|
|
|
unset($this->vars[$offset]);
|
|
|
|
}
|
2013-02-15 01:49:26 +04:00
|
|
|
}
|