Skip to content

Commit

Permalink
Update twig extension to use supported Twig classes instead of legacy…
Browse files Browse the repository at this point in the history
… ones (#374)
  • Loading branch information
Lustmored authored and helios-ag committed Nov 21, 2019
1 parent 9c5d0ec commit cd377c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
45 changes: 26 additions & 19 deletions src/Twig/Extension/FMElfinderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

namespace FM\ElfinderBundle\Twig\Extension;

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Class FMElfinderExtension.
*/
class FMElfinderExtension extends \Twig_Extension
class FMElfinderExtension extends AbstractExtension
{
/**
* @var \Twig_Environment
* @var Environment
*/
protected $twig;

/**
* @param \Twig_Environment $twig
* @param Environment $twig
*/
public function __construct(\Twig_Environment $twig)
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
Expand All @@ -30,9 +37,9 @@ public function getFunctions()
$options = array('is_safe' => array('html'));

return array(
new \Twig_SimpleFunction('elfinder_tinymce_init', array($this, 'tinymce'), $options),
new \Twig_SimpleFunction('elfinder_tinymce_init4', array($this, 'tinymce4'), $options),
new \Twig_SimpleFunction('elfinder_summernote_init', array($this, 'summernote'), $options),
new TwigFunction('elfinder_tinymce_init', array($this, 'tinymce'), $options),
new TwigFunction('elfinder_tinymce_init4', array($this, 'tinymce4'), $options),
new TwigFunction('elfinder_summernote_init', array($this, 'summernote'), $options),
);
}

Expand All @@ -42,14 +49,14 @@ public function getFunctions()
*
* @return mixed
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function tinymce($instance = 'default', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0'))
{
if (!is_string($instance)) {
throw new \Twig_Error_Runtime('The function can be applied to strings only.');
throw new RuntimeError('The function can be applied to strings only.');
}

return $this->twig->render('@FMElfinder/Elfinder/helper/_tinymce.html.twig',
Expand All @@ -67,14 +74,14 @@ public function tinymce($instance = 'default', $parameters = array('width' => 90
*
* @return mixed
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function tinymce4($instance = 'default', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0'))
{
if (!is_string($instance)) {
throw new \Twig_Error_Runtime('The function can be applied to strings only.');
throw new RuntimeError('The function can be applied to strings only.');
}

return $this->twig->render('@FMElfinder/Elfinder/helper/_tinymce4.html.twig',
Expand All @@ -93,14 +100,14 @@ public function tinymce4($instance = 'default', $parameters = array('width' => 9
*
* @return mixed
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function summernote($instance = 'default', $selector = '.summenote', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0'))
{
if (!is_string($instance)) {
throw new \Twig_Error_Runtime('The function can be applied to strings only.');
throw new RuntimeError('The function can be applied to strings only.');
}

return $this->twig->render('@FMElfinder/Elfinder/helper/_summernote.html.twig',
Expand Down
36 changes: 14 additions & 22 deletions tests/Twig/Extension/FMElfinderExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\Template;

class FMElfinderExtensionTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -21,15 +24,15 @@ class FMElfinderExtensionTest extends \PHPUnit\Framework\TestCase
/** @var \FM\ElfinderBundle\Twig\Extension\FMElFinderExtension */
protected $extension;

/** @var \Twig_Environment */
/** @var Environment */
protected $twig;

/** @var \Twig_Template */
/** @var Template */
protected $template;

protected function setUp()
{
$this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__.'/../../../src/Resources/views/Elfinder/helper')));
$this->twig = new Environment(new FilesystemLoader(array(__DIR__.'/../../../src/Resources/views/Elfinder/helper')));
$this->extension = new FMElfinderExtension($this->twig);
$this->twig->addExtension($this->extension);
$loader = new YamlFileLoader(new FileLocator(__DIR__.'/../../../src/Resources/config'));
Expand All @@ -41,8 +44,7 @@ protected function setUp()

public function testRenderTinyMCE3()
{
$this->template = $this->twig->loadTemplate('_tinymce.html.twig');
$testData = $this->renderTemplate(array('instance' => 'minimal'));
$testData = $this->twig->render('_tinymce.html.twig', array('instance' => 'minimal'));

$expected = <<<'EOF'
<script type="text/javascript">
Expand Down Expand Up @@ -72,8 +74,7 @@ function elFinderBrowser (field_name, url, type, win) {

public function testRenderTinyMCE4()
{
$this->template = $this->twig->loadTemplate('_tinymce4.html.twig');
$testData = $this->renderTemplate(array('instance' => 'minimal'));
$testData = $this->twig->render('_tinymce4.html.twig', array('instance' => 'minimal'));

$expected = <<<'EOF'
<script type="text/javascript">
Expand All @@ -99,8 +100,7 @@ function elFinderBrowser (field_name, url, type, win) {

public function testRenderSummernote()
{
$this->template = $this->twig->loadTemplate('_summernote.html.twig');
$testData = $this->renderTemplate(array('instance' => 'minimal'));
$testData = $this->twig->render('_summernote.html.twig', array('instance' => 'minimal'));

$expected = <<<'EOF'
<script type="text/javascript">
Expand Down Expand Up @@ -131,14 +131,6 @@ protected function tearDown()
unset($this->twig);
}

/**
* {@inheritdoc}
*/
protected function renderTemplate(array $context = array())
{
return $this->template->render($context);
}

/**
* Normalizes the output by removing the heading whitespaces.
*
Expand All @@ -155,11 +147,11 @@ public function testSubClassOfTwigExtension()
{
$rc = new \ReflectionClass('FM\ElfinderBundle\Twig\Extension\FMElfinderExtension');

$this->assertTrue($rc->isSubclassOf('Twig_Extension'));
$this->assertTrue($rc->isSubclassOf('Twig\Extension\AbstractExtension'));
}

/**
* @expectedException \Twig_Error_Runtime
* @expectedException \Twig\Error\RuntimeError
* @expectedExceptionMessage The function can be applied to strings only.
*/
public function testSummernoteInstanceNotString()
Expand All @@ -168,7 +160,7 @@ public function testSummernoteInstanceNotString()
}

/**
* @expectedException \Twig_Error_Runtime
* @expectedException \Twig\Error\RuntimeError
* @expectedExceptionMessage The function can be applied to strings only.
*/
public function testTinyMCEInstanceNotString()
Expand All @@ -177,7 +169,7 @@ public function testTinyMCEInstanceNotString()
}

/**
* @expectedException \Twig_Error_Runtime
* @expectedException \Twig\Error\RuntimeError
* @expectedExceptionMessage The function can be applied to strings only.
*/
public function testTinyMCE4InstanceNotString()
Expand All @@ -190,7 +182,7 @@ public function testGetFunctions()
$twigFunctions = $this->extension->getFunctions();

foreach ($twigFunctions as $twigFunction) {
$this->assertInstanceOf('Twig_SimpleFunction', $twigFunction);
$this->assertInstanceOf('Twig\TwigFunction', $twigFunction);
}
}
}

0 comments on commit cd377c6

Please sign in to comment.