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

[FrameworkBundle] Add unit tests for the CacheTemplateLocator class

Victor Berchet 14 роки тому
батько
коміт
c45d5c89cc

+ 14 - 4
src/Symfony/Bundle/FrameworkBundle/Templating/Loader/CachedTemplateLocator.php

@@ -55,10 +55,20 @@ class CachedTemplateLocator extends TemplateLocator
     {
         $key = $template->getSignature();
 
-        if (!isset($this->templates[$key])) {
-            return parent::locate($template, $currentPath, $first);
-        }
+        $path = $this->getCachedTemplatePath($key);
+
+        return $path === null ? parent::locate($template) : $path;
+    }
 
-        return $this->templates[$key];
+    /**
+     * Returns the template path from the cache
+     * 
+     * @param string $key The template signature
+     * 
+     * @return string|null The path when it is present in the call, false otherwise
+     */
+    protected function getCachedTemplatePath($key)
+    {
+        return isset($this->templates[$key]) ? $this->templates[$key] : null;
     }
 }

+ 41 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/CachedTemplateLocatorTest.php

@@ -0,0 +1,41 @@
+<?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\CachedTemplateLocator;
+use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
+use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
+use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
+
+class CachedTemplateLocatorTest extends TestCase
+{
+    public function testLocateACachedTemplate()
+    {
+        $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
+        
+        $locator = $this
+            ->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\Loader\CachedTemplateLocator')
+            ->setMethods(array('getCachedTemplatePath'))
+            ->disableOriginalConstructor()
+            ->getMock()
+        ;
+
+        $locator
+            ->expects($this->once())
+            ->method('getCachedTemplatePath')
+            ->with($template->getSignature())
+            ->will($this->returnValue('/cached/path/to/template'))
+        ;
+
+        $this->assertEquals('/cached/path/to/template', $locator->locate($template));
+    }
+}