Skip to content

Commit

Permalink
fixing TwigFormulaLoader against new Twig 2.x with B/C for Twig 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-andreyev committed Nov 21, 2016
1 parent 77a768e commit c82c10f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Assetic/TwigFormulaLoader.php
Expand Up @@ -56,7 +56,12 @@ public function load(ResourceInterface $resource)

try {
// determine if the template has any errors
$this->twig->tokenize($resource->getContent());
if (class_exists('Twig_Source')) {
$content = new \Twig_Source($resource->getContent(), (string) $resource->getContent());
} else {
$content = $resource->getContent();
}
$this->twig->tokenize($content);

// delegate the formula loading to the parent
$formulae += parent::load($resource);
Expand Down
25 changes: 23 additions & 2 deletions Tests/Assetic/TwigFormulaLoaderTest.php
Expand Up @@ -12,13 +12,32 @@
namespace Liip\ThemeBundle\Tests\Assetic;

use Liip\ThemeBundle\Assetic\TwigFormulaLoader;
use Prophecy\Argument;

class TwigFormulaLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Prophecy\Prophecy\ObjectProphecy
*/
private $twig;

/**
* @var \Prophecy\Prophecy\ObjectProphecy
*/
private $activeTheme;

/**
* @var \Prophecy\Prophecy\ObjectProphecy
*/
private $logger;

/**
* @var \Prophecy\Prophecy\ObjectProphecy
*/
private $resource;

private $resourceContent;

public function setUp()
{
$this->twig = $this->prophesize('Twig_Environment');
Expand All @@ -45,7 +64,9 @@ public function testLoader()
$this->activeTheme->setName('theme1')->shouldBeCalled();
$this->activeTheme->setName('theme2')->shouldBeCalled();

$this->twig->tokenize($this->resourceContent)->shouldBeCalled();
$this->twig->tokenize(Argument::any(), Argument::any())->shouldBeCalled()->willReturn(new \Twig_TokenStream(array()));
$this->twig->parse(Argument::any())->shouldBeCalled()->willReturn(new \Twig_Node);

$this->loader->load($this->resource->reveal());
}

Expand All @@ -55,7 +76,7 @@ public function testExceptions()
'theme1',
));
$this->activeTheme->setName('theme1')->shouldBeCalled();
$this->twig->tokenize($this->resourceContent)->willThrow(new \Exception('foobar'));
$this->twig->tokenize(Argument::any())->willThrow(new \Exception('foobar'));
$this->logger->error('The template "foo" contains an error: "foobar"')->shouldBeCalled();

$this->loader->load($this->resource->reveal());
Expand Down

0 comments on commit c82c10f

Please sign in to comment.