Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig Template Unit Testing

I began to think about Continuous Integration for the twig templates in Symfony.

  1. A template is independent logic.
  2. There are mistakes in the templates. But in the process of development I do not want to be distracted by visual inspection.

Are there any ready-made solutions for unit testing a twig file in Symfony?

like image 841
nonlux Avatar asked Jun 10 '13 14:06

nonlux


2 Answers

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());
like image 128
max4ever Avatar answered Sep 22 '22 22:09

max4ever


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
like image 25
ahmed hamdy Avatar answered Sep 25 '22 22:09

ahmed hamdy