I began to think about Continuous Integration for the twig templates in Symfony.
Are there any ready-made solutions for unit testing a twig file in Symfony?
Inside a WebTestCase
(extension of a phpunit TestCase
since Symfony 2.0) / KernelTestCase
(extension of phpunit TestCase
since Symfony 2.5)
$twig = self::$kernel->getContainer()->get('twig');
$html = $twig->render('AppBundle::app/something.html.twig', ['content' => 'I am some variable value']);
self::assertEquals($html, $response->getContent());
Testing for syntax error within twig templates:
You can use the command line to test on all twig templates into one Bundle by using:
php app/console twig:lint @name of Bundle
Example:
php app/console twig:lint @AcmeDemoBundle
The result will be:
if there isn't any syntax error:
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/layout.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/contact.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/index.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/layout.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/login.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/helloadmin.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/hello.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Welcome/index.html.twig
If there is a syntax error, it will detect line which syntax error in it and reason for Syntax error:
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/layout.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig
KO in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/contact.html.twig (line 6)
4
5 {% block content %}
>> 6 <form action="{{ ath('_demo_contact') }}" method="POST" id="contact_form">
>> The function "ath" does not exist. Did you mean "path", "logout_path"
7 {{ form_errors(form) }}
8
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Demo/index.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/layout.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/login.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/helloadmin.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Secured/hello.html.twig
OK in /var/www/SymBlog/src/Acme/DemoBundle/Resources/views/Welcome/index.html.twig
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With