-2

I was browsing a little and was not able to find any solutions for something I want to accomplish.

I am in Symofny 5 project and I am writing some Unit tests.

The goal of my tests is to check, if the messages will appear as they are defined. The messages are rendered by the twig template. I am not that familiar with Symfony and twig regrading to tests. I do not know, if I somehow can render the output of a view using that twig template for just that entry and then check, if the message contains the expected text in the right manor.

Is it something like this doable?

Can some help and give me guidance as example for something like this?

I think it can be beneficial to other coders out there in the future.

I have an entity like User, than want to get the status, like:

$user->getStatus('ready); 

in twig:

{% if item.status == 'ready' %}

  <p>The status of user is {{ item.status }}</p>

{% endif %}

So, I want to assert that twig templates are rendering correct output for given status.

makeda202
  • 19
  • 4

1 Answers1

0

Update 2:

According to your comment, you are probably trying to test multiple things at once. While not inherently bad, it's probably not what you would call Unit Test. In the ideal application, you want to separate your data persistence logic from the rendering logic completely.

I cannot provide you with any meaningful code sample without knowing what you want to do, but I can give you some general pointers:

  • You can test your persistence logic by itself without twig. If you want to test if your data correctly persists with some default values, you can simply assert the objects retrieved from the database.
  • Inversely, if you need to check your template being rendered correctly, you can mock all the data you need, provide dummy objects and such, everything should work the same.
  • If you really want to bring it all together and test the application as a whole, then you should look into proper integration tests that actually call your endpoints with proper HTTP Requests and assert received HTTP Responses.

Update 1: I've updated the example according to your updated question

I am not exactly sure what are you trying to accomplish exactly, but it seems that you want to assert that twig templates are rendered correctly.

First, you need to figure out the end result. First let's assume your template is:

<html>
    {% if user.status is not empty %}
        <p>The status of user is {{ user.status }}</p>
    {% endif %}
</html>

Then your test code would be something like this:

public function testTwigRender(): void
{
     $expected=<<<'EXPECTED'
     <html>
          <p>The status of user is ready</p>
     </html>
     EXPECTED;

     $user = new User();
     $user->setStatus('ready');

     $twig = self::$kernel->getContainer()->get('twig');
     $actual = $twig->render('AppBundle::app/template.html.twig', ['user' => $user]);
     self::assertEquals($expected, $actual);
}
  • Thank you so much! To be more clear about what I want I updated my post. Can you please check.. @CrazyMuffin – makeda202 Oct 22 '21 at 12:30
  • @makeda202 Can you provide an example of the entire template you want to test? You shouldn't make any templates specific to testing. I'm still not sure what are you trying to achieve and what is not clear to you. – CrazyMuffin Oct 22 '21 at 13:12
  • The template is quite big. I want to test that that specific entity data that is persisted to the database is rendered in the twig template by that status like I showed in the post above. @CrazyMuffin – makeda202 Oct 22 '21 at 13:15