Skip to content

Commit

Permalink
Fix renderer tests (#1228)
Browse files Browse the repository at this point in the history
Fix renderer tests and make sure Symfony 4 working properly.
  • Loading branch information
isometriks authored Dec 23, 2017
1 parent df0e426 commit d30a6d8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ language: php
sudo: false

php:
- 5.6
- 7.0
- 7.1

env:
- SYMFONY_VERSION=~2.7.0
- SYMFONY_VERSION=~2.8.0
- SYMFONY_VERSION=~3.0
- SYMFONY_VERSION=~4.0
- DEPENDENCIES=beta

matrix:
fast_finish: true
exclude:
- php: 5.6
env: DEPENDENCIES=beta
- php: 7.0
env: SYMFONY_VERSION=~4.0
- php: 7.0
env: DEPENDENCIES=beta

Expand Down
73 changes: 50 additions & 23 deletions Tests/Form/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
use Mopa\Bundle\BootstrapBundle\Twig\IconExtension;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Form\FormRenderer;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\FormIntegrationTestCase;
use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;

abstract class AbstractDivLayoutTest extends FormIntegrationTestCase
{
protected $renderer;
protected $rendererEngine;
protected $environment;
protected $tabFactory;
protected $formTypeMap = array(
'form' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
Expand All @@ -46,7 +50,8 @@ protected function setUp()

parent::setUp();

$reflection = new \ReflectionClass('Symfony\Bridge\Twig\Form\TwigRenderer');
$reflectionClass = class_exists('Symfony\Bridge\Twig\Form\TwigRenderer') ? 'Symfony\Bridge\Twig\Form\TwigRenderer' : 'Symfony\Bridge\Twig\Form\TwigRendererEngine';
$reflection = new \ReflectionClass($reflectionClass);
$bridgeDirectory = dirname($reflection->getFileName()).'/../Resources/views/Form';

$loader = new \Twig_Loader_Filesystem(array(
Expand All @@ -56,41 +61,63 @@ protected function setUp()

$loader->addPath(__DIR__.'/../../Resources/views', 'MopaBootstrap');

$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new IconExtension('fontawesome'));
$environment->addExtension(new TwigFormExtension());
$environment->addGlobal('global', '');
$this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
$this->environment->addExtension(new TranslationExtension(new StubTranslator()));
$this->environment->addExtension(new IconExtension('fontawesome'));
$this->environment->addExtension(new TwigFormExtension());
$this->environment->addGlobal('global', '');

$rendererEngine = new TwigRendererEngine(array(
$this->rendererEngine = new TwigRendererEngine(array(
'form_div_layout.html.twig',
'fields.html.twig',
), $environment);
), $this->environment);

if (interface_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')) {
$csrfProviderInterface = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface';
if (version_compare(SymfonyKernel::VERSION, '3.0.0', '<')) {
$this->setUpVersion2();
} else {
$csrfProviderInterface = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
$this->setUpVersion3Plus();
}

$csrfProvider = $this->getMockBuilder($csrfProviderInterface)->getMock();
$this->renderer = new TwigRenderer($rendererEngine, $csrfProvider);
$environment->addExtension($extension = new FormExtension($this->renderer));
$extension->initRuntime($environment);
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

protected function registerTwigRuntimeLoader(\Twig_Environment $environment, TwigRenderer $renderer)
private function setUpVersion2()
{
if (!method_exists($environment, 'addRuntimeLoader')) {
return;
}
$csrfProvider = $this->getMockBuilder('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')->getMock();
$this->renderer = new TwigRenderer($this->rendererEngine, $csrfProvider);
$this->environment->addExtension($extension = new FormExtension($this->renderer));
$extension->initRuntime($this->environment);

// Add runtime loader
$loader = $this->getMockBuilder('Twig_RuntimeLoaderInterface')->getMock();
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
array('Symfony\Bridge\Twig\Form\TwigRenderer', $renderer),
array('Symfony\Bridge\Twig\Form\TwigRenderer', $this->renderer),
)));
$environment->addRuntimeLoader($loader);
$this->environment->addRuntimeLoader($loader);
}

private function setUpVersion3Plus()
{
$csrfProvider = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
$loaders = array(
'Symfony\Component\Form\FormRenderer' => function() use ($csrfProvider) {
return new FormRenderer($this->rendererEngine, $csrfProvider);
},
);

$runtime = 'Symfony\Component\Form\FormRenderer';

if (class_exists('Symfony\Bridge\Twig\Form\TwigRenderer')) {
$loaders['Symfony\Bridge\Twig\Form\TwigRenderer'] = function() use ($csrfProvider) {
return new TwigRenderer($this->rendererEngine, $csrfProvider);
};

$runtime = 'Symfony\Bridge\Twig\Form\TwigRenderer';
}

// Add runtime loader
$this->environment->addRuntimeLoader(new \Twig_FactoryRuntimeLoader($loaders));
$this->renderer = $this->environment->getRuntime($runtime);

$this->environment->addExtension(new FormExtension());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"mopa/composer-bridge": "~1.3"
},
"require-dev": {
"phpunit/phpunit": "~4.6",
"phpunit/phpunit": "~6.0",
"symfony/security-bundle": "~2.3|~3.0|^4.0",
"symfony/translation": "~2.3|~3.0|^4.0"
},
"conflict": {
"symfony/twig-bridge": "<2.3|^4.0"
"symfony/twig-bridge": "<2.3"
},
"suggest": {
"twbs/bootstrap": ">2.0,<4.0-dev",
Expand Down

0 comments on commit d30a6d8

Please sign in to comment.