Переглянути джерело

[FrameworkBundle] Fixed previous commit and added some tests for PHP globals

Christophe Coevoet 14 роки тому
батько
коміт
8dc6464aa4

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

@@ -23,6 +23,7 @@
             <argument type="service" id="templating.name_parser" />
             <argument type="service" id="service_container" />
             <argument type="service" id="templating.loader" />
+            <argument type="service" id="templating.globals" />
             <call method="setCharset"><argument>%kernel.charset%</argument></call>
         </service>
 

+ 62 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/PhpEngineTest.php

@@ -0,0 +1,62 @@
+<?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\Session;
+use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
+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, $app = new GlobalVariables($container));
+
+        $container->set('request', null);
+
+        $globals = $engine->getGlobals();
+        $this->assertEmpty($globals['app']->getRequest());
+    }
+
+    /**
+     * Creates a Container with a Session-containing Request service.
+     *
+     * @return Container
+     */
+    protected function getContainer()
+    {
+        $container = new Container();
+        $request = new Request();
+        $session = new Session(new ArraySessionStorage());
+
+        $request->setSession($session);
+        $container->set('request', $request);
+
+        return $container;
+    }
+}