2011-01-13 13:58:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-13 13:58:17 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
2014-12-08 10:32:45 +00:00
|
|
|
use Symfony\Component\Templating\TemplateReference as BaseTemplateReference;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
class TemplateNameParserTest extends TestCase
|
|
|
|
{
|
2011-02-10 17:20:44 +00:00
|
|
|
protected $parser;
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
protected function setUp()
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2011-04-21 20:49:05 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundle')
|
|
|
|
->will($this->returnCallback(function ($bundle) {
|
2018-07-05 12:24:53 +01:00
|
|
|
if (\in_array($bundle, array('SensioFooBundle', 'SensioCmsFooBundle', 'FooBundle'))) {
|
2011-04-21 20:49:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}))
|
|
|
|
;
|
2011-02-10 17:20:44 +00:00
|
|
|
$this->parser = new TemplateNameParser($kernel);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->parser = null;
|
2011-02-10 17:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-14 14:59:01 +01:00
|
|
|
* @dataProvider parseProvider
|
2011-02-10 17:20:44 +00:00
|
|
|
*/
|
2015-07-14 14:59:01 +01:00
|
|
|
public function testParse($name, $logicalName, $path, $ref)
|
2011-02-10 17:20:44 +00:00
|
|
|
{
|
|
|
|
$template = $this->parser->parse($name);
|
|
|
|
|
2015-07-14 14:59:01 +01:00
|
|
|
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
|
|
|
|
$this->assertSame($logicalName, $template->getLogicalName());
|
|
|
|
$this->assertSame($path, $template->getPath());
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 14:59:01 +01:00
|
|
|
public function parseProvider()
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
|
|
|
return array(
|
2015-07-14 14:59:01 +01:00
|
|
|
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')),
|
2015-07-15 08:00:45 +01:00
|
|
|
array('index.html.php', '::index.html.php', 'views/index.html.php', new TemplateReference('', '', 'index', 'html', 'php')),
|
2015-07-14 14:59:01 +01:00
|
|
|
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')),
|
2016-02-22 18:48:09 +00:00
|
|
|
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')),
|
2015-07-14 14:59:01 +01:00
|
|
|
array('name.twig', 'name.twig', 'name.twig', new BaseTemplateReference('name.twig', 'twig')),
|
|
|
|
array('name', 'name', 'name', new BaseTemplateReference('name')),
|
2015-07-15 08:00:45 +01:00
|
|
|
array('default/index.html.php', '::default/index.html.php', 'views/default/index.html.php', new TemplateReference(null, null, 'default/index', 'html', 'php')),
|
2011-01-13 13:58:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
2014-12-08 10:32:45 +00:00
|
|
|
public function testParseValidNameWithNotFoundBundle()
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
2014-12-08 10:32:45 +00:00
|
|
|
$this->parser->parse('BarBundle:Post:index.html.php');
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
2016-03-06 18:36:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group legacy
|
|
|
|
* @dataProvider provideAbsolutePaths
|
2016-10-20 14:28:37 +01:00
|
|
|
* @expectedDeprecation Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.
|
2016-03-06 18:36:57 +00:00
|
|
|
*/
|
|
|
|
public function testAbsolutePathsAreDeprecated($name, $logicalName, $path, $ref)
|
|
|
|
{
|
2016-10-20 14:28:37 +01:00
|
|
|
$template = $this->parser->parse($name);
|
2016-03-24 13:26:59 +00:00
|
|
|
|
2016-10-20 14:28:37 +01:00
|
|
|
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
|
|
|
|
$this->assertSame($logicalName, $template->getLogicalName());
|
|
|
|
$this->assertSame($path, $template->getPath());
|
2016-03-06 18:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideAbsolutePaths()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
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')),
|
|
|
|
);
|
|
|
|
}
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|