Testing the Twig Template

I started thinking about continuous integration for branch templates in Symfony.

  • The template is independent logic.
  • There are errors in the templates. But in the process of development, I do not want to be distracted by a visual inspection.

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

+6
source share
3 answers

In addition to checking the structural and syntactic correctness of your Twig template files using them, you can also unit test the logic of your Twig templates.

For example, you can check the correct behavior of the if-elseif-else-endif , and you can do it in isolation, that is, without having to go through controllers with all their dependencies.

Take a look at https://github.com/journeymonitor/control/blob/2645c69/src/AppBundle/Resources/views/testcases/_testresults-overview-testresult-label.html.twig . This is a very simple template, but it has a number of actions - depending on the value of testresult.exitCode , different label names should be displayed.

At https://github.com/journeymonitor/control/blob/bc42e78/tests/AppBundle/Resources/views/testcases/_testresults-overview-testresult-label.html.twig.test.php you can see a unit test case for this template.

In the test example, a new Twig_Environment is created that allows you to load the Twig template file and render it with different Testresult objects passed as a parameter. The result of the rendering operation can then be claimed, as in any other PHPUnit test:

 $loader = new Twig_Loader_Filesystem(__DIR__ . '/../../../../../src/AppBundle/Resources/views/testcases/'); $twig = new Twig_Environment($loader, array( 'cache' => '/var/tmp/journeymonitor-twig-tests-cache', )); $template = $twig->loadTemplate('_testresults-overview-testresult-label.html.twig'); $testresult = new \AppBundle\Entity\Testresult(); $testresult->setExitCode(0); $this->assertSame('success', $template->render(['testresult' => $testresult])); 

Pay attention to shell commands on lines 8 and 9 - the Twig environment caches its template files, and in order to have reliable tests, you need to make sure that the cache location has been wiped before each run:

 `rm -rf /var/tmp/journeymonitor-twig-tests-cache`; `mkdir -p /var/tmp/journeymonitor-twig-tests-cache`; 
+4
source

Testing syntax errors in branch templates:

You can use the command line to test all branch templates in one set with:

 php app/console twig:lint @name of Bundle 

Example:

 php app/console twig:lint @AcmeDemoBundle 

The result will be:

if there is no 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 a string whose syntax error and the reason for the syntax error are:

 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 
+7
source

Use symfony to test a separate twig file as follows:

 ./app/console twig:lint /yourproject/yourtwigs/views/yourtwig.html.twig 

Result if OK:

 OK in /yourproject/yourtwigs/views/yourtwig.html.twig 

The result, if not OK (I added curly braces where it did not belong):

 /yourproject/yourtwigs/views/yourtwig.html.twig (line 2) 1 {% include 'YourBundle:Includes:jquery.html.twig' %} >> 2 {{% include 'YourBundle:Includes:datatables.html.twig' %} >> Unexpected "}" 3 <script> 4 $(document).ready(function() 
+1
source

Source: https://habr.com/ru/post/946967/


All Articles