Merge branch '2.2' into 2.3

* 2.2:
  return 0 if there is no valid data
  [Tests] Tests on php 5.5 should pass
  [Twig] fixed TwigEngine::exists() method when a template contains a syntax error (closes #88546)
This commit is contained in:
Fabien Potencier 2013-07-27 07:01:40 +02:00
commit 9d53905b88
3 changed files with 76 additions and 2 deletions

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\Bridge\Twig\Tests;
use Symfony\Bridge\Twig\TwigEngine;
class TwigEngineTest extends TestCase
{
public function testExistsWithTemplateInstances()
{
$engine = $this->getTwig();
$this->assertTrue($engine->exists($this->getMockForAbstractClass('Twig_Template', array(), '', false)));
}
public function testExistsWithNonExistentTemplates()
{
$engine = $this->getTwig();
$this->assertFalse($engine->exists('foobar'));
}
public function testExistsWithTemplateWithSyntaxErrors()
{
$engine = $this->getTwig();
$this->assertTrue($engine->exists('error'));
}
public function testExists()
{
$engine = $this->getTwig();
$this->assertTrue($engine->exists('index'));
}
protected function getTwig()
{
$twig = new \Twig_Environment(new \Twig_Loader_Array(array(
'index' => 'foo',
'error' => '{{ foo }',
)));
$parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface');
return new TwigEngine($twig, $parser);
}
}

View File

@ -75,9 +75,19 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface
*/
public function exists($name)
{
if ($name instanceof \Twig_Template) {
return true;
}
$loader = $this->environment->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) {
return $loader->exists($name);
}
try {
$this->load($name);
} catch (\InvalidArgumentException $e) {
$loader->getSource($name);
} catch (\Twig_Error_Loader $e) {
return false;
}

View File

@ -78,6 +78,10 @@ class TimeDataCollector extends DataCollector
*/
public function getDuration()
{
if (!isset($this->data['events']['__section__'])) {
return 0;
}
$lastEvent = $this->data['events']['__section__'];
return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
@ -92,6 +96,10 @@ class TimeDataCollector extends DataCollector
*/
public function getInitTime()
{
if (!isset($this->data['events']['__section__'])) {
return 0;
}
return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
}