Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,123 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
{
public function testSupportsRetrievesEngineFromTheContainer()
{
$container = $this->getContainerMock(array(
'engine.first' => $this->getEngineMock('template.php', false),
'engine.second' => $this->getEngineMock('template.php', true),
));
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
$this->assertTrue($delegatingEngine->supports('template.php'));
}
public function testGetExistingEngine()
{
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', true);
$container = $this->getContainerMock(array(
'engine.first' => $firstEngine,
'engine.second' => $secondEngine,
));
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
$this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php'));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No engine is able to work with the template "template.php"
*/
public function testGetInvalidEngine()
{
$firstEngine = $this->getEngineMock('template.php', false);
$secondEngine = $this->getEngineMock('template.php', false);
$container = $this->getContainerMock(array(
'engine.first' => $firstEngine,
'engine.second' => $secondEngine,
));
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
$delegatingEngine->getEngine('template.php');
}
public function testRenderResponseWithFrameworkEngine()
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$engine = $this->getFrameworkEngineMock('template.php', true);
$engine->expects($this->once())
->method('renderResponse')
->with('template.php', array('foo' => 'bar'))
->will($this->returnValue($response));
$container = $this->getContainerMock(array('engine' => $engine));
$delegatingEngine = new DelegatingEngine($container, array('engine'));
$this->assertSame($response, $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
}
public function testRenderResponseWithTemplatingEngine()
{
$engine = $this->getEngineMock('template.php', true);
$container = $this->getContainerMock(array('engine' => $engine));
$delegatingEngine = new DelegatingEngine($container, array('engine'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
}
private function getEngineMock($template, $supports)
{
$engine = $this->getMock('Symfony\Component\Templating\EngineInterface');
$engine->expects($this->once())
->method('supports')
->with($template)
->will($this->returnValue($supports));
return $engine;
}
private function getFrameworkEngineMock($template, $supports)
{
$engine = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$engine->expects($this->once())
->method('supports')
->with($template)
->will($this->returnValue($supports));
return $engine;
}
private function getContainerMock($services)
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$i = 0;
foreach ($services as $id => $service) {
$container->expects($this->at($i++))
->method('get')
->with($id)
->will($this->returnValue($service));
}
return $container;
}
}

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\Container;
class GlobalVariablesTest extends TestCase
{
private $container;
private $globals;
protected function setUp()
{
$this->container = new Container();
$this->globals = new GlobalVariables($this->container);
}
public function testGetUserNoTokenStorage()
{
$this->assertNull($this->globals->getUser());
}
public function testGetUserNoToken()
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$this->container->set('security.token_storage', $tokenStorage);
$this->assertNull($this->globals->getUser());
}
/**
* @dataProvider getUserProvider
*/
public function testGetUser($user, $expectedUser)
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$this->container->set('security.token_storage', $tokenStorage);
$token
->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue($token));
$this->assertSame($expectedUser, $this->globals->getUser());
}
public function getUserProvider()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$std = new \stdClass();
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
return array(
array($user, $user),
array($std, $std),
array($token, $token),
array('Anon.', null),
array(null, null),
array(10, null),
array(true, null),
);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
class AssetsHelperTest extends \PHPUnit_Framework_TestCase
{
private $helper;
protected function setUp()
{
$fooPackage = new Package(new StaticVersionStrategy('42', '%s?v=%s'));
$barPackage = new Package(new StaticVersionStrategy('22', '%s?%s'));
$packages = new Packages($fooPackage, array('bar' => $barPackage));
$this->helper = new AssetsHelper($packages);
}
public function testGetUrl()
{
$this->assertEquals('me.png?v=42', $this->helper->getUrl('me.png'));
$this->assertEquals('me.png?22', $this->helper->getUrl('me.png', 'bar'));
}
public function testGetVersion()
{
$this->assertEquals('42', $this->helper->getVersion('/'));
$this->assertEquals('22', $this->helper->getVersion('/', 'bar'));
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReference;
class StubTemplateNameParser implements TemplateNameParserInterface
{
private $root;
private $rootTheme;
public function __construct($root, $rootTheme)
{
$this->root = $root;
$this->rootTheme = $rootTheme;
}
public function parse($name)
{
list($bundle, $controller, $template) = explode(':', $name, 3);
if ($template[0] == '_') {
$path = $this->rootTheme.'/Custom/'.$template;
} elseif ($bundle === 'TestBundle') {
$path = $this->rootTheme.'/'.$controller.'/'.$template;
} else {
$path = $this->root.'/'.$controller.'/'.$template;
}
return new TemplateReference($path, 'php');
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures;
use Symfony\Component\Translation\TranslatorInterface;
class StubTranslator implements TranslatorInterface
{
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}
public function setLocale($locale)
{
}
public function getLocale()
{
}
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
{
/**
* @var PhpEngine
*/
protected $engine;
protected function getExtensions()
{
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
$root = realpath(dirname($reflClass->getFileName()).'/Resources/views');
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$this->engine = new PhpEngine($templateNameParser, $loader);
$this->engine->addGlobal('global', '');
$this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
return array_merge(parent::getExtensions(), array(
new TemplatingExtension($this->engine, $this->csrfTokenManager, array(
'FrameworkBundle:Form',
)),
));
}
protected function tearDown()
{
$this->engine = null;
parent::tearDown();
}
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
'method' => 'get',
'action' => '',
));
$html = $this->renderStart($form->createView());
$this->assertSame('<form name="form" method="get">', $html);
}
public function testStartTagHasActionAttributeWhenActionIsZero()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
'method' => 'get',
'action' => '0',
));
$html = $this->renderStart($form->createView());
$this->assertSame('<form name="form" method="get" action="0">', $html);
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->form($view, $vars);
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
return (string) $this->engine->get('form')->label($view, $label, $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->engine->get('form')->errors($view);
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->widget($view, $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->row($view, $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->start($view, $vars);
}
protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->end($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->engine->get('form')->setTheme($view, $themes);
}
public static function themeBlockInheritanceProvider()
{
return array(
array(array('TestBundle:Parent')),
);
}
public static function themeInheritanceProvider()
{
return array(
array(array('TestBundle:Parent'), array('TestBundle:Child')),
);
}
}

View File

@@ -0,0 +1,129 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
{
/**
* @var PhpEngine
*/
protected $engine;
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
'method' => 'get',
'action' => '',
));
$html = $this->renderStart($form->createView());
$this->assertSame('<form name="form" method="get">', $html);
}
public function testStartTagHasActionAttributeWhenActionIsZero()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
'method' => 'get',
'action' => '0',
));
$html = $this->renderStart($form->createView());
$this->assertSame('<form name="form" method="get" action="0">', $html);
}
protected function getExtensions()
{
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
$root = realpath(dirname($reflClass->getFileName()).'/Resources/views');
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$this->engine = new PhpEngine($templateNameParser, $loader);
$this->engine->addGlobal('global', '');
$this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
return array_merge(parent::getExtensions(), array(
new TemplatingExtension($this->engine, $this->csrfTokenManager, array(
'FrameworkBundle:Form',
'FrameworkBundle:FormTable',
)),
));
}
protected function tearDown()
{
$this->engine = null;
parent::tearDown();
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->form($view, $vars);
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
return (string) $this->engine->get('form')->label($view, $label, $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->engine->get('form')->errors($view);
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->widget($view, $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->row($view, $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->start($view, $vars);
}
protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->end($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->engine->get('form')->setTheme($view, $themes);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper;
class RequestHelperTest extends \PHPUnit_Framework_TestCase
{
protected $requestStack;
protected function setUp()
{
$this->requestStack = new RequestStack();
$request = new Request();
$request->initialize(array('foobar' => 'bar'));
$this->requestStack->push($request);
}
public function testGetParameter()
{
$helper = new RequestHelper($this->requestStack);
$this->assertEquals('bar', $helper->getParameter('foobar'));
$this->assertEquals('foo', $helper->getParameter('bar', 'foo'));
$this->assertNull($helper->getParameter('foo'));
}
public function testGetLocale()
{
$helper = new RequestHelper($this->requestStack);
$this->assertEquals('en', $helper->getLocale());
}
public function testGetName()
{
$helper = new RequestHelper($this->requestStack);
$this->assertEquals('request', $helper->getName());
}
}

View File

@@ -0,0 +1 @@
<label><?php echo $global ?>child</label>

View File

@@ -0,0 +1,2 @@
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<label>Custom name label: <?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>

View File

@@ -0,0 +1,4 @@
<?php if (!$label) {
$label = $view['form']->humanize($name);
} ?>
<label>Custom label: <?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>

View File

@@ -0,0 +1,3 @@
<div id="container">
<?php echo $view['form']->widget($form) ?>
</div>

View File

@@ -0,0 +1 @@
<label>parent</label>

View File

@@ -0,0 +1,2 @@
<?php $type = isset($type) ? $type : 'text' ?>
<input type="<?php echo $type ?>" <?php $view['form']->block($form, 'widget_attributes') ?> value="<?php echo $value ?>" rel="theme" />

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
class SessionHelperTest extends \PHPUnit_Framework_TestCase
{
protected $requestStack;
protected function setUp()
{
$request = new Request();
$session = new Session(new MockArraySessionStorage());
$session->set('foobar', 'bar');
$session->getFlashBag()->set('notice', 'bar');
$request->setSession($session);
$this->requestStack = new RequestStack();
$this->requestStack->push($request);
}
protected function tearDown()
{
$this->requestStack = null;
}
public function testFlash()
{
$helper = new SessionHelper($this->requestStack);
$this->assertTrue($helper->hasFlash('notice'));
$this->assertEquals(array('bar'), $helper->getFlash('notice'));
}
public function testGetFlashes()
{
$helper = new SessionHelper($this->requestStack);
$this->assertEquals(array('notice' => array('bar')), $helper->getFlashes());
}
public function testGet()
{
$helper = new SessionHelper($this->requestStack);
$this->assertEquals('bar', $helper->get('foobar'));
$this->assertEquals('foo', $helper->get('bar', 'foo'));
$this->assertNull($helper->get('foo'));
}
public function testGetName()
{
$helper = new SessionHelper($this->requestStack);
$this->assertEquals('session', $helper->getName());
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper;
class StopwatchHelperTest extends \PHPUnit_Framework_TestCase
{
public function testDevEnvironment()
{
$stopwatch = $this->getMock('Symfony\Component\Stopwatch\Stopwatch');
$stopwatch->expects($this->once())
->method('start')
->with('foo');
$helper = new StopwatchHelper($stopwatch);
$helper->start('foo');
}
public function testProdEnvironment()
{
$helper = new StopwatchHelper(null);
try {
$helper->start('foo');
} catch (\BadMethodCallException $e) {
$this->fail('Assumed stopwatch is not called when not provided');
}
}
}

View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Loader;
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
class TemplateLocatorTest extends TestCase
{
public function testLocateATemplate()
{
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
$fileLocator = $this->getFileLocator();
$fileLocator
->expects($this->once())
->method('locate')
->with($template->getPath())
->will($this->returnValue('/path/to/template'))
;
$locator = new TemplateLocator($fileLocator);
$this->assertEquals('/path/to/template', $locator->locate($template));
}
public function testThrowsExceptionWhenTemplateNotFound()
{
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
$fileLocator = $this->getFileLocator();
$errorMessage = 'FileLocator exception message';
$fileLocator
->expects($this->once())
->method('locate')
->will($this->throwException(new \InvalidArgumentException($errorMessage)))
;
$locator = new TemplateLocator($fileLocator);
try {
$locator->locate($template);
$this->fail('->locate() should throw an exception when the file is not found.');
} catch (\InvalidArgumentException $e) {
$this->assertContains(
$errorMessage,
$e->getMessage(),
'TemplateLocator exception should propagate the FileLocator exception message'
);
}
}
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsAnExceptionWhenTemplateIsNotATemplateReferenceInterface()
{
$locator = new TemplateLocator($this->getFileLocator());
$locator->locate('template');
}
protected function getFileLocator()
{
return $this
->getMockBuilder('Symfony\Component\Config\FileLocator')
->setMethods(array('locate'))
->setConstructorArgs(array('/path/to/fallback'))
->getMock()
;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
class PhpEngineTest extends TestCase
{
public function testEvaluateAddsAppGlobal()
{
$container = $this->getContainer();
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader, $app = new GlobalVariables($container));
$globals = $engine->getGlobals();
$this->assertSame($app, $globals['app']);
}
public function testEvaluateWithoutAvailableRequest()
{
$container = new Container();
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader, new GlobalVariables($container));
$container->set('request_stack', null);
$globals = $engine->getGlobals();
$this->assertEmpty($globals['app']->getRequest());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetInvalidHelper()
{
$container = $this->getContainer();
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader);
$engine->get('non-existing-helper');
}
/**
* Creates a Container with a Session-containing Request service.
*
* @return Container
*/
protected function getContainer()
{
$container = new Container();
$session = new Session(new MockArraySessionStorage());
$request = new Request();
$stack = new RequestStack();
$stack->push($request);
$request->setSession($session);
$container->set('request_stack', $stack);
return $container;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateFilenameParser;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
class TemplateFilenameParserTest extends TestCase
{
protected $parser;
protected function setUp()
{
$this->parser = new TemplateFilenameParser();
}
protected function tearDown()
{
$this->parser = null;
}
/**
* @dataProvider getFilenameToTemplateProvider
*/
public function testParseFromFilename($file, $ref)
{
$template = $this->parser->parse($file);
if ($ref === false) {
$this->assertFalse($template);
} else {
$this->assertEquals($template->getLogicalName(), $ref->getLogicalName());
}
}
public function getFilenameToTemplateProvider()
{
return array(
array('/path/to/section/name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
array('\\path\\to\\section\\name.format.engine', new TemplateReference('', '/path/to/section', 'name', 'format', 'engine')),
array('name.format.engine', new TemplateReference('', '', 'name', 'format', 'engine')),
array('name.format', false),
array('name', false),
);
}
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateReference as BaseTemplateReference;
class TemplateNameParserTest extends TestCase
{
protected $parser;
protected function setUp()
{
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundle')
->will($this->returnCallback(function ($bundle) {
if (in_array($bundle, array('SensioFooBundle', 'SensioCmsFooBundle', 'FooBundle'))) {
return true;
}
throw new \InvalidArgumentException();
}))
;
$this->parser = new TemplateNameParser($kernel);
}
protected function tearDown()
{
$this->parser = null;
}
/**
* @dataProvider parseProvider
*/
public function testParse($name, $logicalName, $path, $ref)
{
$template = $this->parser->parse($name);
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
$this->assertSame($logicalName, $template->getLogicalName());
$this->assertSame($path, $template->getPath());
}
public function parseProvider()
{
return array(
array('FooBundle:Post:index.html.php', 'FooBundle:Post:index.html.php', '@FooBundle/Resources/views/Post/index.html.php', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php')),
array('FooBundle:Post:index.html.twig', 'FooBundle:Post:index.html.twig', '@FooBundle/Resources/views/Post/index.html.twig', new TemplateReference('FooBundle', 'Post', 'index', 'html', 'twig')),
array('FooBundle:Post:index.xml.php', 'FooBundle:Post:index.xml.php', '@FooBundle/Resources/views/Post/index.xml.php', new TemplateReference('FooBundle', 'Post', 'index', 'xml', 'php')),
array('SensioFooBundle:Post:index.html.php', 'SensioFooBundle:Post:index.html.php', '@SensioFooBundle/Resources/views/Post/index.html.php', new TemplateReference('SensioFooBundle', 'Post', 'index', 'html', 'php')),
array('SensioCmsFooBundle:Post:index.html.php', 'SensioCmsFooBundle:Post:index.html.php', '@SensioCmsFooBundle/Resources/views/Post/index.html.php', new TemplateReference('SensioCmsFooBundle', 'Post', 'index', 'html', 'php')),
array(':Post:index.html.php', ':Post:index.html.php', 'views/Post/index.html.php', new TemplateReference('', 'Post', 'index', 'html', 'php')),
array('::index.html.php', '::index.html.php', 'views/index.html.php', new TemplateReference('', '', 'index', 'html', 'php')),
array('index.html.php', '::index.html.php', 'views/index.html.php', new TemplateReference('', '', 'index', 'html', 'php')),
array('FooBundle:Post:foo.bar.index.html.php', 'FooBundle:Post:foo.bar.index.html.php', '@FooBundle/Resources/views/Post/foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')),
array('@FooBundle/Resources/views/layout.html.twig', '@FooBundle/Resources/views/layout.html.twig', '@FooBundle/Resources/views/layout.html.twig', new BaseTemplateReference('@FooBundle/Resources/views/layout.html.twig', 'twig')),
array('@FooBundle/Foo/layout.html.twig', '@FooBundle/Foo/layout.html.twig', '@FooBundle/Foo/layout.html.twig', new BaseTemplateReference('@FooBundle/Foo/layout.html.twig', 'twig')),
array('/path/to/section/index.html.php', '/path/to/section/index.html.php', '/path/to/section/index.html.php', new BaseTemplateReference('/path/to/section/index.html.php', 'php')),
array('C:\\path\\to\\section\\name.html.php', 'C:path/to/section/name.html.php', 'C:path/to/section/name.html.php', new BaseTemplateReference('C:path/to/section/name.html.php', 'php')),
array('C:\\path\\to\\section\\name:foo.html.php', 'C:path/to/section/name:foo.html.php', 'C:path/to/section/name:foo.html.php', new BaseTemplateReference('C:path/to/section/name:foo.html.php', 'php')),
array('\\path\\to\\section\\name.html.php', '/path/to/section/name.html.php', '/path/to/section/name.html.php', new BaseTemplateReference('/path/to/section/name.html.php', 'php')),
array('/path/to/section/name.php', '/path/to/section/name.php', '/path/to/section/name.php', new BaseTemplateReference('/path/to/section/name.php', 'php')),
array('name.twig', 'name.twig', 'name.twig', new BaseTemplateReference('name.twig', 'twig')),
array('name', 'name', 'name', new BaseTemplateReference('name')),
array('default/index.html.php', '::default/index.html.php', 'views/default/index.html.php', new TemplateReference(null, null, 'default/index', 'html', 'php')),
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testParseValidNameWithNotFoundBundle()
{
$this->parser->parse('BarBundle:Post:index.html.php');
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
class TemplateReferenceTest extends TestCase
{
public function testGetPathWorksWithNamespacedControllers()
{
$reference = new TemplateReference('AcmeBlogBundle', 'Admin\Post', 'index', 'html', 'twig');
$this->assertSame(
'@AcmeBlogBundle/Resources/views/Admin/Post/index.html.twig',
$reference->getPath()
);
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
class TemplateTest extends TestCase
{
/**
* @dataProvider getTemplateToPathProvider
*/
public function testGetPathForTemplatesInABundle($template, $path)
{
if ($template->get('bundle')) {
$this->assertEquals($template->getPath(), $path);
}
}
/**
* @dataProvider getTemplateToPathProvider
*/
public function testGetPathForTemplatesOutOfABundle($template, $path)
{
if (!$template->get('bundle')) {
$this->assertEquals($template->getPath(), $path);
}
}
public function getTemplateToPathProvider()
{
return array(
array(new TemplateReference('FooBundle', 'Post', 'index', 'html', 'php'), '@FooBundle/Resources/views/Post/index.html.php'),
array(new TemplateReference('FooBundle', '', 'index', 'html', 'twig'), '@FooBundle/Resources/views/index.html.twig'),
array(new TemplateReference('', 'Post', 'index', 'html', 'php'), 'views/Post/index.html.php'),
array(new TemplateReference('', '', 'index', 'html', 'php'), 'views/index.html.php'),
);
}
}

View File

@@ -0,0 +1,116 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
class TimedPhpEngineTest extends TestCase
{
public function testThatRenderLogsTime()
{
$container = $this->getContainer();
$templateNameParser = $this->getTemplateNameParser();
$globalVariables = $this->getGlobalVariables();
$loader = $this->getLoader($this->getStorage());
$stopwatch = $this->getStopwatch();
$stopwatchEvent = $this->getStopwatchEvent();
$stopwatch->expects($this->once())
->method('start')
->with('template.php (index.php)', 'template')
->will($this->returnValue($stopwatchEvent));
$stopwatchEvent->expects($this->once())->method('stop');
$engine = new TimedPhpEngine($templateNameParser, $container, $loader, $stopwatch, $globalVariables);
$engine->render('index.php');
}
/**
* @return Container
*/
private function getContainer()
{
return $this->getMock('Symfony\Component\DependencyInjection\Container');
}
/**
* @return \Symfony\Component\Templating\TemplateNameParserInterface
*/
private function getTemplateNameParser()
{
$templateReference = $this->getMock('Symfony\Component\Templating\TemplateReferenceInterface');
$templateNameParser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
$templateNameParser->expects($this->any())
->method('parse')
->will($this->returnValue($templateReference));
return $templateNameParser;
}
/**
* @return GlobalVariables
*/
private function getGlobalVariables()
{
return $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables')
->disableOriginalConstructor()
->getMock();
}
/**
* @return \Symfony\Component\Templating\Storage\StringStorage
*/
private function getStorage()
{
return $this->getMockBuilder('Symfony\Component\Templating\Storage\StringStorage')
->disableOriginalConstructor()
->getMockForAbstractClass();
}
/**
* @param \Symfony\Component\Templating\Storage\StringStorage $storage
*
* @return \Symfony\Component\Templating\Loader\Loader
*/
private function getLoader($storage)
{
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
$loader->expects($this->once())
->method('load')
->will($this->returnValue($storage));
return $loader;
}
/**
* @return \Symfony\Component\Stopwatch\StopwatchEvent
*/
private function getStopwatchEvent()
{
return $this->getMockBuilder('Symfony\Component\Stopwatch\StopwatchEvent')
->disableOriginalConstructor()
->getMock();
}
/**
* @return \Symfony\Component\Stopwatch\Stopwatch
*/
private function getStopwatch()
{
return $this->getMock('Symfony\Component\Stopwatch\Stopwatch');
}
}