Skip to content

Commit

Permalink
Fixed tests: Use Twig namespaced classes and removed usage deprecated…
Browse files Browse the repository at this point in the history
… interface (#32)

* Use Twig namespaced classes and removed usage of the deprecated Twig_Extension_InitRuntimeInterface.

* Bump minimal Twig version

* Fixed tests, inject Twig environment into the functions directly

* Use namspaced Twig classes in tests
  • Loading branch information
Devristo authored and norberttech committed Nov 26, 2019
1 parent a91c02c commit 306b7e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
12 changes: 5 additions & 7 deletions Tests/Twig/Extension/AceEditorExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ private function getExtension($autoinclude = true, $basePath = '', $mode = '')
}

/**
* @return \Twig_Environment
* @return \Twig\Environment
*/
private function getTwigEnvironment()
{
return $this->getMockBuilder(\Twig_Environment::class)
return $this->getMockBuilder(\Twig\Environment::class)
->disableOriginalConstructor()
->getMock();
}
Expand Down Expand Up @@ -53,8 +53,7 @@ public function testIncludeAceEditorTwigNoExtensionAsset()
$extension = $this->getExtension();
$environment->method('hasExtension')->with(AssetExtension::class)->willReturn(false);

$extension->initRuntime($environment);
$extension->includeAceEditor();
$extension->includeAceEditor($environment);
}

public function testIncludeAceEditorTwig()
Expand All @@ -72,18 +71,17 @@ public function testIncludeAceEditorTwig()
});

$environment->method('getExtension')->with(AssetExtension::class)->willReturn($asset);
$extension->initRuntime($environment);

ob_start();
$extension->includeAceEditor();
$extension->includeAceEditor($environment);
$text = ob_get_clean();
$this->assertSame(
'<script src="//ace.js" charset="utf-8" type="text/javascript"></script><script src="//ext-language_tools.js" charset="utf-8" type="text/javascript"></script>',
$text
);

ob_start();
$extension->includeAceEditor();
$extension->includeAceEditor($environment);
$text = ob_get_clean();
$this->assertSame('', $text);
}
Expand Down
27 changes: 9 additions & 18 deletions Twig/Extension/AceEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Norzechowicz\AceEditorBundle\Twig\Extension;

use Symfony\Bridge\Twig\Extension\AssetExtension;
use Twig\Extension\AbstractExtension;
use Twig\Extension\InitRuntimeInterface;
use Twig\Environment;
use Twig\TwigFunction;

class AceEditorExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
class AceEditorExtension extends AbstractExtension
{
/**
* Should we include the ace.js?
Expand All @@ -24,11 +28,6 @@ class AceEditorExtension extends \Twig_Extension implements \Twig_Extension_Init
*/
private $mode;

/**
* @var \Twig_Environment
*/
private $environment;

/**
* @param bool $autoinclude means if the bundle should inclue the JS
* @param string $basePath
Expand All @@ -41,14 +40,6 @@ public function __construct($autoinclude, $basePath, $mode)
$this->mode = ltrim($mode, '/');
}

/**
* {@inheritdoc}
*/
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;
}

/**
* {@inheritdoc}
*/
Expand All @@ -63,7 +54,7 @@ public function getName()
public function getFunctions()
{
return [
'include_ace_editor' => new \Twig_SimpleFunction('include_ace_editor', [$this, 'includeAceEditor'], ['is_safe' => ['html']]),
'include_ace_editor' => new TwigFunction('include_ace_editor', [$this, 'includeAceEditor'], ['is_safe' => ['html'], 'needs_environment' => true]),
];
}

Expand All @@ -72,20 +63,20 @@ public function getFunctions()
*
* @throws \LogicException if asset extension is not available and Ace editor must be included
*/
public function includeAceEditor()
public function includeAceEditor(Environment $environment)
{
if ($this->editorIncluded) {
return;
}

if (!$this->environment->hasExtension(AssetExtension::class)) {
if (!$environment->hasExtension(AssetExtension::class)) {
throw new \LogicException('"asset" extension is mandatory if you don\'t include Ace editor by yourself.');
}

if (!$this->editorIncluded) {
foreach (['ace', 'ext-language_tools'] as $file) {
/** @var AssetExtension $extension */
$extension = $this->environment->getExtension(AssetExtension::class);
$extension = $environment->getExtension(AssetExtension::class);
$jsPath = $extension->getAssetUrl($this->basePath.'/'.$this->mode.'/'.$file.'.js');

printf('<script src="%s" charset="utf-8" type="text/javascript"></script>', $jsPath);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": ">=5.6",
"twig/twig": "^1.23|^2.0",
"twig/twig": "^1.34|^2.0",
"symfony/framework-bundle": "^3.0|^4.0",
"symfony/form": "^3.0|^4.0",
"symfony/twig-bridge": "^3.0|^4.0",
Expand Down

0 comments on commit 306b7e3

Please sign in to comment.