Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished the conversions of unit tests to PHPUnit
- Loading branch information
Showing
124 changed files
with
916 additions
and
11,054 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
tests/Symfony/Tests/Components/Templating/EngineTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Components\Templating; | ||
|
||
require_once __DIR__.'/../../bootstrap.php'; | ||
|
||
require_once __DIR__.'/../../../../lib/SymfonyTests/Components/Templating/SimpleHelper.php'; | ||
|
||
use Symfony\Components\Templating\Engine; | ||
use Symfony\Components\Templating\Loader\Loader; | ||
use Symfony\Components\Templating\Loader\CompilableLoaderInterface; | ||
use Symfony\Components\Templating\Renderer\Renderer; | ||
use Symfony\Components\Templating\Renderer\PhpRenderer; | ||
use Symfony\Components\Templating\Storage\Storage; | ||
use Symfony\Components\Templating\Storage\StringStorage; | ||
use Symfony\Components\Templating\Helper\SlotsHelper; | ||
|
||
class EngineTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
static protected $loader, $renderer; | ||
|
||
static public function setUpBeforeClass() | ||
{ | ||
self::$loader = new ProjectTemplateLoader(); | ||
self::$renderer = new ProjectTemplateRenderer(); | ||
} | ||
|
||
public function testConstructor() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader); | ||
$this->assertEquals($engine->getLoader(), self::$loader, '__construct() takes a loader instance as its second first argument'); | ||
$this->assertEquals(array_keys($engine->getRenderers()), array('php'), '__construct() automatically registers a PHP renderer if none is given'); | ||
|
||
$engine = new ProjectTemplateEngine(self::$loader, array('foo' => self::$renderer)); | ||
$this->assertEquals(array_keys($engine->getRenderers()), array('foo', 'php'), '__construct() takes an array of renderers as its third argument'); | ||
$this->assertTrue(self::$renderer->getEngine() === $engine, '__construct() registers itself on all renderers'); | ||
|
||
$engine = new ProjectTemplateEngine(self::$loader, array('php' => self::$renderer)); | ||
$this->assertTrue($engine->getRenderers() === array('php' => self::$renderer), '__construct() can overridde the default PHP renderer'); | ||
} | ||
|
||
public function testMagicGet() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader); | ||
$engine->set($helper = new \SimpleHelper('bar'), 'foo'); | ||
$this->assertEquals($engine->foo, $helper, '->__get() returns the value of a helper'); | ||
|
||
try | ||
{ | ||
$engine->bar; | ||
$this->fail('->__get() throws an InvalidArgumentException if the helper is not defined'); | ||
} | ||
catch (\InvalidArgumentException $e) | ||
{ | ||
} | ||
} | ||
|
||
public function testGetSetHas() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader); | ||
$foo = new \SimpleHelper('foo'); | ||
$engine->set($foo); | ||
$this->assertEquals($engine->get('foo'), $foo, '->set() sets a helper'); | ||
|
||
$engine->set($foo, 'bar'); | ||
$this->assertEquals($engine->get('bar'), $foo, '->set() takes an alias as a second argument'); | ||
|
||
try | ||
{ | ||
$engine->get('foobar'); | ||
$this->fail('->get() throws an InvalidArgumentException if the helper is not defined'); | ||
} | ||
catch (\InvalidArgumentException $e) | ||
{ | ||
} | ||
|
||
$this->assertTrue($engine->has('foo'), '->has() returns true if the helper exists'); | ||
$this->assertTrue(!$engine->has('foobar'), '->has() returns false if the helper does not exist'); | ||
} | ||
|
||
public function testExtendRender() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper())); | ||
try | ||
{ | ||
$engine->render('name'); | ||
$this->fail('->render() throws an InvalidArgumentException if the template does not exist'); | ||
} | ||
catch (\InvalidArgumentException $e) | ||
{ | ||
} | ||
|
||
try | ||
{ | ||
self::$loader->setTemplate('name.foo', 'foo'); | ||
$engine->render('foo:name'); | ||
$this->fail('->render() throws an InvalidArgumentException if no renderer is registered for the given renderer'); | ||
} | ||
catch (\InvalidArgumentException $e) | ||
{ | ||
} | ||
|
||
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper())); | ||
$engine->set(new \SimpleHelper('bar')); | ||
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $view->foo.$foo ?>'); | ||
self::$loader->setTemplate('layout.php', '-<?php echo $view->slots->get("_content") ?>-'); | ||
$this->assertEquals($engine->render('foo', array('foo' => 'foo')), '-barfoo-', '->render() uses the decorator to decorate the template'); | ||
|
||
$engine = new ProjectTemplateEngine(self::$loader, array(), array(new SlotsHelper())); | ||
$engine->set(new \SimpleHelper('bar')); | ||
self::$loader->setTemplate('bar.php', 'bar'); | ||
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>'); | ||
self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-'); | ||
$this->assertEquals($engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), 'bar-foo-', '->render() supports render() calls in templates'); | ||
|
||
// compilable templates | ||
$engine = new ProjectTemplateEngine(new CompilableTemplateLoader(), array('foo' => new FooTemplateRenderer())); | ||
$this->assertEquals($engine->render('index'), 'foo', '->load() takes into account the renderer embedded in the Storage instance if not null'); | ||
} | ||
|
||
public function testEscape() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader); | ||
$this->assertEquals($engine->escape('<br />'), '<br />', '->escape() escapes strings'); | ||
$this->assertEquals($engine->escape($foo = new \stdClass()), $foo, '->escape() does nothing on non strings'); | ||
} | ||
|
||
public function testGetSetCharset() | ||
{ | ||
$engine = new ProjectTemplateEngine(self::$loader); | ||
$this->assertEquals($engine->getCharset(), 'UTF-8', '->getCharset() returns UTF-8 by default'); | ||
$engine->setCharset('ISO-8859-1'); | ||
$this->assertEquals($engine->getCharset(), 'ISO-8859-1', '->setCharset() changes the default charset to use'); | ||
} | ||
} | ||
|
||
class ProjectTemplateEngine extends Engine | ||
{ | ||
public function getLoader() | ||
{ | ||
return $this->loader; | ||
} | ||
|
||
public function getRenderers() | ||
{ | ||
return $this->renderers; | ||
} | ||
} | ||
|
||
class ProjectTemplateRenderer extends PhpRenderer | ||
{ | ||
public function getEngine() | ||
{ | ||
return $this->engine; | ||
} | ||
} | ||
|
||
class ProjectTemplateLoader extends Loader | ||
{ | ||
public $templates = array(); | ||
|
||
public function setTemplate($name, $template) | ||
{ | ||
$this->templates[$name] = $template; | ||
} | ||
|
||
public function load($template, array $options = array()) | ||
{ | ||
if (isset($this->templates[$template.'.'.$options['renderer']])) | ||
{ | ||
return new StringStorage($this->templates[$template.'.'.$options['renderer']]); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
class CompilableTemplateLoader extends Loader implements CompilableLoaderInterface | ||
{ | ||
public function load($template, array $options = array()) | ||
{ | ||
return new StringStorage($template, 'foo'); | ||
} | ||
|
||
public function compile($template) | ||
{ | ||
return 'COMPILED'; | ||
} | ||
} | ||
|
||
class FooTemplateRenderer extends Renderer | ||
{ | ||
public function evaluate(Storage $template, array $parameters = array()) | ||
{ | ||
return 'foo'; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
tests/Symfony/Tests/Components/Templating/Helper/AssetsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Components\Templating\Helper; | ||
|
||
require_once __DIR__.'/../../../bootstrap.php'; | ||
|
||
use Symfony\Components\Templating\Helper\AssetsHelper; | ||
|
||
class AssetsHelperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$helper = new AssetsHelper('foo', 'http://www.example.com', 'abcd'); | ||
$this->assertEquals($helper->getBasePath(), '/foo/', '__construct() takes a base path as its first argument'); | ||
$this->assertEquals($helper->getBaseURLs(), array('http://www.example.com'), '__construct() takes a base URL as its second argument'); | ||
$this->assertEquals($helper->getVersion(), 'abcd', '__construct() takes a version as its thrid argument'); | ||
} | ||
|
||
public function testGetSetBasePath() | ||
{ | ||
$helper = new AssetsHelper(); | ||
$helper->setBasePath('foo/'); | ||
$this->assertEquals($helper->getBasePath(), '/foo/', '->setBasePath() prepends a / if needed'); | ||
$helper->setBasePath('/foo'); | ||
$this->assertEquals($helper->getBasePath(), '/foo/', '->setBasePath() appends a / is needed'); | ||
$helper->setBasePath(''); | ||
$this->assertEquals($helper->getBasePath(), '/', '->setBasePath() returns / if no base path is defined'); | ||
$helper->setBasePath('0'); | ||
$this->assertEquals($helper->getBasePath(), '/0/', '->setBasePath() returns /0/ if 0 is given'); | ||
} | ||
|
||
public function testGetSetVersion() | ||
{ | ||
$helper = new AssetsHelper(); | ||
$helper->setVersion('foo'); | ||
$this->assertEquals($helper->getVersion(), 'foo', '->setVersion() sets the version'); | ||
} | ||
|
||
public function testSetGetBaseURLs() | ||
{ | ||
$helper = new AssetsHelper(); | ||
$helper->setBaseURLs('http://www.example.com/'); | ||
$this->assertEquals($helper->getBaseURLs(), array('http://www.example.com'), '->setBaseURLs() removes the / at the of an absolute base path'); | ||
$helper->setBaseURLs(array('http://www1.example.com/', 'http://www2.example.com/')); | ||
$URLs = array(); | ||
for ($i = 0; $i < 20; $i++) | ||
{ | ||
$URLs[] = $helper->getBaseURL($i); | ||
} | ||
$URLs = array_values(array_unique($URLs)); | ||
sort($URLs); | ||
$this->assertEquals($URLs, array('http://www1.example.com', 'http://www2.example.com'), '->getBaseURL() returns a random base URL if several are given'); | ||
$helper->setBaseURLs(''); | ||
$this->assertEquals($helper->getBaseURL(1), '', '->getBaseURL() returns an empty string if no base URL exist'); | ||
} | ||
|
||
public function testGetUrl() | ||
{ | ||
$helper = new AssetsHelper(); | ||
$this->assertEquals($helper->getUrl('http://example.com/foo.js'), 'http://example.com/foo.js', '->getUrl() does nothing if an absolute URL is given'); | ||
|
||
$helper = new AssetsHelper(); | ||
$this->assertEquals($helper->getUrl('foo.js'), '/foo.js', '->getUrl() appends a / on relative paths'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), '/foo.js', '->getUrl() does nothing on absolute paths'); | ||
|
||
$helper = new AssetsHelper('/foo'); | ||
$this->assertEquals($helper->getUrl('foo.js'), '/foo/foo.js', '->getUrl() appends the basePath on relative paths'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), '/foo.js', '->getUrl() does not append the basePath on absolute paths'); | ||
|
||
$helper = new AssetsHelper(null, 'http://assets.example.com/'); | ||
$this->assertEquals($helper->getUrl('foo.js'), 'http://assets.example.com/foo.js', '->getUrl() prepends the base URL'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), 'http://assets.example.com/foo.js', '->getUrl() prepends the base URL'); | ||
|
||
$helper = new AssetsHelper(null, 'http://www.example.com/foo'); | ||
$this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL with a path'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL with a path'); | ||
|
||
$helper = new AssetsHelper('/foo', 'http://www.example.com/'); | ||
$this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL and the base path if defined'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo.js', '->getUrl() prepends the base URL but not the base path on absolute paths'); | ||
|
||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo'); | ||
$this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/bar/foo.js', '->getUrl() prepends the base URL and the base path if defined'); | ||
$this->assertEquals($helper->getUrl('/foo.js'), 'http://www.example.com/foo/foo.js', '->getUrl() prepends the base URL but not the base path on absolute paths'); | ||
|
||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd'); | ||
$this->assertEquals($helper->getUrl('foo.js'), 'http://www.example.com/foo/bar/foo.js?abcd', '->getUrl() appends the version if defined'); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Symfony/Tests/Components/Templating/Helper/HelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Components\Templating\Helper; | ||
|
||
require_once __DIR__.'/../../../bootstrap.php'; | ||
|
||
use Symfony\Components\Templating\Helper\Helper; | ||
|
||
class HelperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGetSetCharset() | ||
{ | ||
$helper = new ProjectTemplateHelper(); | ||
$helper->setCharset('ISO-8859-1'); | ||
$this->assertTrue('ISO-8859-1' === $helper->getCharset(), '->setCharset() sets the charset set related to this helper'); | ||
} | ||
} | ||
|
||
class ProjectTemplateHelper extends Helper | ||
{ | ||
public function getName() | ||
{ | ||
return 'foo'; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/Symfony/Tests/Components/Templating/Helper/JavascriptsHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Components\Templating\Helper; | ||
|
||
require_once __DIR__.'/../../../bootstrap.php'; | ||
|
||
use Symfony\Components\Templating\Helper\AssetsHelper; | ||
use Symfony\Components\Templating\Helper\JavascriptsHelper; | ||
use Symfony\Components\Templating\Loader\FilesystemLoader; | ||
|
||
class JavascriptsHelperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAdd() | ||
{ | ||
$assetHelper = new AssetsHelper(); | ||
$helper = new JavascriptsHelper($assetHelper); | ||
$helper->add('foo'); | ||
$this->assertEquals($helper->get(), array('/foo' => array()), '->add() adds a JavaScript'); | ||
$helper->add('/foo'); | ||
$this->assertEquals($helper->get(), array('/foo' => array()), '->add() does not add the same JavaScript twice'); | ||
$helper = new JavascriptsHelper($assetHelper); | ||
$assetHelper->setBaseURLs('http://assets.example.com/'); | ||
$helper->add('foo'); | ||
$this->assertEquals($helper->get(), array('http://assets.example.com/foo' => array()), '->add() converts the JavaScript to a public path'); | ||
} | ||
|
||
public function testMagicToString() | ||
{ | ||
$assetHelper = new AssetsHelper(); | ||
$assetHelper->setBaseURLs(''); | ||
$helper = new JavascriptsHelper($assetHelper); | ||
$helper->add('foo', array('class' => 'ba>')); | ||
$this->assertEquals($helper->__toString(), '<script type="text/javascript" src="/foo" class="ba>"></script>'."\n", '->__toString() converts the JavaScript configuration to HTML'); | ||
} | ||
} |
Oops, something went wrong.