From b9a4586b24d671a43160cebd58f51bcc278af139 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 21 Oct 2016 18:08:29 -0700 Subject: [PATCH 1/2] [TwigBridge] fixed Twig_Source required argument --- .../Bridge/Twig/Tests/Extension/RoutingExtensionTest.php | 2 +- src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php | 2 +- .../Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php | 2 +- src/Symfony/Bridge/Twig/Translation/TwigExtractor.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/RoutingExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/RoutingExtensionTest.php index 87b6052f6f79..9733cd7b8ace 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/RoutingExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/RoutingExtensionTest.php @@ -23,7 +23,7 @@ public function testEscaping($template, $mustBeEscaped) $twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0)); $twig->addExtension(new RoutingExtension($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'))); - $nodes = $twig->parse($twig->tokenize(new \Twig_Source($template))); + $nodes = $twig->parse($twig->tokenize(new \Twig_Source($template, ''))); $this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter); } diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php index a90b556c9e3f..502cad38dec0 100644 --- a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php @@ -25,7 +25,7 @@ public static function getModule($content) new \Twig_Node_Expression_Array(array(), 0), new \Twig_Node_Expression_Array(array(), 0), null, - new \Twig_Source('') + new \Twig_Source('', '') ); } diff --git a/src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php b/src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php index aa9913249833..6b6a92abf143 100644 --- a/src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php +++ b/src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php @@ -23,7 +23,7 @@ public function testCompile($source, $expected) { $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); $env->addTokenParser(new FormThemeTokenParser()); - $stream = $env->tokenize(new \Twig_Source($source)); + $stream = $env->tokenize(new \Twig_Source($source, '')); $parser = new \Twig_Parser($env); $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)); diff --git a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php index 69ec1d915500..917687ad34ee 100644 --- a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php +++ b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php @@ -85,7 +85,7 @@ protected function extractTemplate($template, MessageCatalogue $catalogue) $visitor = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\TranslationExtension')->getTranslationNodeVisitor(); $visitor->enable(); - $this->twig->parse($this->twig->tokenize(new \Twig_Source($template))); + $this->twig->parse($this->twig->tokenize(new \Twig_Source($template, ''))); foreach ($visitor->getMessages() as $message) { $catalogue->set(trim($message[0]), $this->prefix.trim($message[0]), $message[1] ?: $this->defaultDomain); From 317d46f249893dbb5b0f7b7f4acf2243d3da9127 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 21 Oct 2016 18:11:17 -0700 Subject: [PATCH 2/2] [TwigBundle] fixed usage of getSource in tests --- .../Tests/Loader/FilesystemLoaderTest.php | 6 +++--- .../LegacyRenderTokenParserTest.php | 2 +- .../Profiler/TemplateManager.php | 6 +++++- .../Tests/Profiler/TemplateManagerTest.php | 20 +++++++------------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php index 269e029a0858..9804c08a1923 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php @@ -17,7 +17,7 @@ class FilesystemLoaderTest extends TestCase { - public function testGetSource() + public function testGetSourceContext() { $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface'); $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface'); @@ -30,10 +30,10 @@ public function testGetSource() $loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace'); // Twig-style - $this->assertEquals("This is a layout\n", $loader->getSource('@namespace/layout.html.twig')); + $this->assertEquals("This is a layout\n", $loader->getSourceContext('@namespace/layout.html.twig')->getCode()); // Symfony-style - $this->assertEquals("This is a layout\n", $loader->getSource('TwigBundle::layout.html.twig')); + $this->assertEquals("This is a layout\n", $loader->getSourceContext('TwigBundle::layout.html.twig')->getCode()); } public function testExists() diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TokenParser/LegacyRenderTokenParserTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TokenParser/LegacyRenderTokenParserTest.php index b1f81ec8eef5..6a5806cd7a96 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TokenParser/LegacyRenderTokenParserTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TokenParser/LegacyRenderTokenParserTest.php @@ -27,7 +27,7 @@ public function testCompile($source, $expected) { $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); $env->addTokenParser(new RenderTokenParser()); - $stream = $env->tokenize(new \Twig_Source($source)); + $stream = $env->tokenize(new \Twig_Source($source, '')); $parser = new \Twig_Parser($env); $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)); diff --git a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php index 5d0fd277c51d..415034a9f80c 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php @@ -126,7 +126,11 @@ protected function templateExists($template) } try { - $loader->getSource($template); + if ($loader instanceof \Twig_SourceContextLoaderInterface) { + $loader->getSourceContext($template); + } else { + $loader->getSource($template); + } return true; } catch (\Twig_Error_Loader $e) { diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php index 29238a21c439..c9b199ea18b9 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php @@ -31,11 +31,6 @@ class TemplateManagerTest extends TestCase */ protected $profiler; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $profile; - /** * @var \Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager */ @@ -129,11 +124,7 @@ public function profileHasCollectorCallback($panel) protected function mockProfile() { - $this->profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile') - ->disableOriginalConstructor() - ->getMock(); - - return $this->profile; + return $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')->disableOriginalConstructor()->getMock(); } protected function mockTwigEnvironment() @@ -144,9 +135,12 @@ protected function mockTwigEnvironment() ->method('loadTemplate') ->will($this->returnValue('loadedTemplate')); - $this->twigEnvironment->expects($this->any()) - ->method('getLoader') - ->will($this->returnValue($this->getMock('\Twig_LoaderInterface'))); + if (interface_exists('\Twig_SourceContextLoaderInterface')) { + $loader = $this->getMock('\Twig_SourceContextLoaderInterface'); + } else { + $loader = $this->getMock('\Twig_LoaderInterface'); + } + $this->twigEnvironment->expects($this->any())->method('getLoader')->will($this->returnValue($loader)); return $this->twigEnvironment; }