changed coding standards

This commit is contained in:
Fabien Potencier
2010-05-11 10:38:48 +02:00
parent 276a3804c5
commit 4d8fc51112
96 changed files with 4625 additions and 5063 deletions
+7 -7
View File
@@ -13,12 +13,12 @@ require_once dirname(__FILE__).'/bootstrap.php';
class Twig_Tests_AutoloaderTest extends PHPUnit_Framework_TestCase
{
public function testAutoload()
{
$this->assertFalse(class_exists('FooBarFoo'), '->autoload() does not try to load classes that does not begin with Twig');
public function testAutoload()
{
$this->assertFalse(class_exists('FooBarFoo'), '->autoload() does not try to load classes that does not begin with Twig');
$autoloader = new Twig_Autoloader();
$this->assertTrue($autoloader->autoload('Twig_Parser'), '->autoload() returns true if it is able to load a class');
$this->assertFalse($autoloader->autoload('Foo'), '->autoload() returns false if it is not able to load a class');
}
$autoloader = new Twig_Autoloader();
$this->assertTrue($autoloader->autoload('Twig_Parser'), '->autoload() returns true if it is able to load a class');
$this->assertFalse($autoloader->autoload('Foo'), '->autoload() returns false if it is not able to load a class');
}
}
+90 -105
View File
@@ -13,126 +13,111 @@ require_once dirname(__FILE__).'/../bootstrap.php';
class Twig_Tests_Extension_SandboxTest extends PHPUnit_Framework_TestCase
{
static protected $params, $templates;
static protected $params, $templates;
public function setUp()
{
self::$params = array(
'name' => 'Fabien',
'obj' => new Object(),
);
self::$templates = array(
'1_basic1' => '{{ obj.foo }}',
'1_basic2' => '{{ name|upper }}',
'1_basic3' => '{% if name %}foo{% endif %}',
'1_basic4' => '{{ obj.bar }}',
'1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
}
public function testSandboxGloballySet()
{
$twig = $this->getEnvironment(false, self::$templates);
$this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally');
$twig = $this->getEnvironment(true, self::$templates);
try
{
$twig->loadTemplate('1_basic1')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
}
catch (Twig_Sandbox_SecurityError $e)
public function setUp()
{
self::$params = array(
'name' => 'Fabien',
'obj' => new Object(),
);
self::$templates = array(
'1_basic1' => '{{ obj.foo }}',
'1_basic2' => '{{ name|upper }}',
'1_basic3' => '{% if name %}foo{% endif %}',
'1_basic4' => '{{ obj.bar }}',
'1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
}
$twig = $this->getEnvironment(true, self::$templates);
try
{
$twig->loadTemplate('1_basic2')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
}
catch (Twig_Sandbox_SecurityError $e)
public function testSandboxGloballySet()
{
$twig = $this->getEnvironment(false, self::$templates);
$this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally');
$twig = $this->getEnvironment(true, self::$templates);
try {
$twig->loadTemplate('1_basic1')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
} catch (Twig_Sandbox_SecurityError $e) {
}
$twig = $this->getEnvironment(true, self::$templates);
try {
$twig->loadTemplate('1_basic2')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
} catch (Twig_Sandbox_SecurityError $e) {
}
$twig = $this->getEnvironment(true, self::$templates);
try {
$twig->loadTemplate('1_basic3')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
} catch (Twig_Sandbox_SecurityError $e) {
}
$twig = $this->getEnvironment(true, self::$templates);
try {
$twig->loadTemplate('1_basic4')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
} catch (Twig_Sandbox_SecurityError $e) {
}
$twig = $this->getEnvironment(true, self::$templates, array(), array(), array('Object' => 'foo'));
$this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods');
$twig = $this->getEnvironment(true, self::$templates, array(), array('upper'));
$this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters');
$twig = $this->getEnvironment(true, self::$templates, array('if'));
$this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags');
$twig = $this->getEnvironment(true, self::$templates, array(), array(), array(), array('Object' => 'bar'));
$this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties');
}
$twig = $this->getEnvironment(true, self::$templates);
try
{
$twig->loadTemplate('1_basic3')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
}
catch (Twig_Sandbox_SecurityError $e)
public function testSandboxLocallySetForAnInclude()
{
self::$templates = array(
'2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
'2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
$twig = $this->getEnvironment(false, self::$templates);
$this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
self::$templates = array(
'3_basic' => '{{ obj.foo }}{% include "3_included" sandboxed %}{{ obj.foo }}',
'3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
$twig = $this->getEnvironment(false, self::$templates);
$twig = $this->getEnvironment(true, self::$templates);
try {
$twig->loadTemplate('3_basic')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
} catch (Twig_Sandbox_SecurityError $e) {
}
}
$twig = $this->getEnvironment(true, self::$templates);
try
protected function getEnvironment($sandboxed, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array())
{
$twig->loadTemplate('1_basic4')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'debug' => true));
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties);
$twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
return $twig;
}
catch (Twig_Sandbox_SecurityError $e)
{
}
$twig = $this->getEnvironment(true, self::$templates, array(), array(), array('Object' => 'foo'));
$this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods');
$twig = $this->getEnvironment(true, self::$templates, array(), array('upper'));
$this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters');
$twig = $this->getEnvironment(true, self::$templates, array('if'));
$this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags');
$twig = $this->getEnvironment(true, self::$templates, array(), array(), array(), array('Object' => 'bar'));
$this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties');
}
public function testSandboxLocallySetForAnInclude()
{
self::$templates = array(
'2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
'2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
$twig = $this->getEnvironment(false, self::$templates);
$this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
self::$templates = array(
'3_basic' => '{{ obj.foo }}{% include "3_included" sandboxed %}{{ obj.foo }}',
'3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
$twig = $this->getEnvironment(false, self::$templates);
$twig = $this->getEnvironment(true, self::$templates);
try
{
$twig->loadTemplate('3_basic')->render(self::$params);
$this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
}
catch (Twig_Sandbox_SecurityError $e)
{
}
}
protected function getEnvironment($sandboxed, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array())
{
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'debug' => true));
$policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties);
$twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
return $twig;
}
}
class Object
{
public $bar = 'bar';
public $bar = 'bar';
public function foo()
{
return 'foo';
}
public function foo()
{
return 'foo';
}
}
+55 -59
View File
@@ -13,78 +13,74 @@ require_once dirname(__FILE__).'/bootstrap.php';
class Twig_Tests_TokenStreamTest extends PHPUnit_Framework_TestCase
{
static protected $tokens;
static protected $tokens;
public function setUp()
{
self::$tokens = array(
new Twig_Token(Twig_Token::TEXT_TYPE, 1, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 2, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 3, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 4, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 5, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 6, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 7, 0),
new Twig_Token(Twig_Token::EOF_TYPE, 0, 0),
);
}
public function testNext()
{
$stream = new Twig_TokenStream(self::$tokens, '', false);
$repr = array();
while (!$stream->isEOF())
public function setUp()
{
$token = $stream->next();
$repr[] = $token->getValue();
self::$tokens = array(
new Twig_Token(Twig_Token::TEXT_TYPE, 1, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 2, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 3, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 4, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 5, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 6, 0),
new Twig_Token(Twig_Token::TEXT_TYPE, 7, 0),
new Twig_Token(Twig_Token::EOF_TYPE, 0, 0),
);
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() returns the next token in the stream');
}
public function testLook()
{
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$repr = array();
while (!$stream->isEOF())
public function testNext()
{
$token = $stream->next();
$stream = new Twig_TokenStream(self::$tokens, '', false);
$repr = array();
while (!$stream->isEOF()) {
$token = $stream->next();
$repr[] = $token->getValue();
$repr[] = $token->getValue();
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() returns the next token in the stream');
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$repr = array();
while (!$stream->isEOF())
public function testLook()
{
$token = $stream->next();
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$repr = array();
while (!$stream->isEOF()) {
$token = $stream->next();
$repr[] = $token->getValue();
$repr[] = $token->getValue();
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$repr = array();
while (!$stream->isEOF()) {
$token = $stream->next();
$repr[] = $token->getValue();
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
}
public function testRewind()
{
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$stream->rewind();
$repr = array();
while (!$stream->isEOF())
public function testRewind()
{
$token = $stream->next(false);
$stream = new Twig_TokenStream(self::$tokens, '', false);
$this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
$this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
$stream->rewind();
$repr = array();
while (!$stream->isEOF()) {
$token = $stream->next(false);
$repr[] = $token->getValue();
$repr[] = $token->getValue();
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->rewind() pushes all pushed tokens to the token array');
}
$this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->rewind() pushes all pushed tokens to the token array');
}
}
+72 -79
View File
@@ -13,98 +13,91 @@ require_once dirname(__FILE__).'/bootstrap.php';
class Twig_Tests_IntegrationTest extends PHPUnit_Framework_TestCase
{
static protected $fixturesDir;
static protected $fixturesDir;
public function setUp()
{
self::$fixturesDir = realpath(dirname(__FILE__).'/../../fixtures/');
}
public function testIntegration()
{
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
public function setUp()
{
if (!preg_match('/\.test$/', $file))
{
continue;
}
$test = file_get_contents($file->getRealpath());
if (!preg_match('/--TEST--\s*(.*?)\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match))
{
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace(self::$fixturesDir.'/', '', $file)));
}
$message = $match[1];
$templates = array();
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
foreach ($matches as $match)
{
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
}
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
$template = $twig->loadTemplate('index.twig');
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
{
$output = trim($template->render(eval($match[1].';')), "\n ");
$expected = trim($match[2], "\n ");
$this->assertEquals($expected, $output, $message);
if ($output != $expected)
{
echo 'Compiled template that failed:';
foreach (array_keys($templates) as $name)
{
$source = $loader->getSource($name);
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
}
}
}
self::$fixturesDir = realpath(dirname(__FILE__).'/../../fixtures/');
}
public function testIntegration()
{
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!preg_match('/\.test$/', $file)) {
continue;
}
$test = file_get_contents($file->getRealpath());
if (!preg_match('/--TEST--\s*(.*?)\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace(self::$fixturesDir.'/', '', $file)));
}
$message = $match[1];
$templates = array();
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
}
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
$template = $twig->loadTemplate('index.twig');
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$output = trim($template->render(eval($match[1].';')), "\n ");
$expected = trim($match[2], "\n ");
$this->assertEquals($expected, $output, $message);
if ($output != $expected) {
echo 'Compiled template that failed:';
foreach (array_keys($templates) as $name) {
$source = $loader->getSource($name);
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
}
}
}
}
}
}
}
class Foo
{
public function bar($param1 = null, $param2 = null)
{
return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
}
public function bar($param1 = null, $param2 = null)
{
return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
}
public function getFoo()
{
return 'foo';
}
public function getFoo()
{
return 'foo';
}
public function getSelf()
{
return $this;
}
public function getSelf()
{
return $this;
}
}
class TestExtension extends Twig_Extension
{
public function getFilters()
{
return array('nl2br' => new Twig_Filter_Method($this, 'nl2br'));
}
public function getFilters()
{
return array('nl2br' => new Twig_Filter_Method($this, 'nl2br'));
}
public function nl2br($value, $sep = '<br />')
{
return str_replace("\n", $sep."\n", $value);
}
public function nl2br($value, $sep = '<br />')
{
return str_replace("\n", $sep."\n", $value);
}
public function getName()
{
return 'test';
}
public function getName()
{
return 'test';
}
}