3

I'm trying to achieve generated routes in my functional tests. I want them to be generated dynamically,preferably by name, but I cannot find a proper way how to do it. Point me please.

A simple test:

  public function testIndex()
    {
        // I have service container
        $container = $this->client->getContainer();
        $crawler = $client->request('GET', helper_function("route_name", $params));
        $heading = $crawler->filter('h1')->eq(0)->text();
        $this->assertEquals('Application list', $heading);
    }

What that helper_function() should be?

Matteo
  • 37,680
  • 11
  • 100
  • 115
Vit Kos
  • 5,535
  • 5
  • 44
  • 58

2 Answers2

4

Since you have access to service container you can get router (which by default will return Symfony\Component\Routing) service and call generate method on it.

$route = $container->get('router')->generate($routeName, $params);
Tomasz Madeyski
  • 10,742
  • 3
  • 50
  • 62
3

In the setup of the WebTestCase class you can take an instance of the router component then use it as usually.

As Example:

 class AcmeDemoTestCase extends WebTestCase


    protected $router;


        protected function setUp()
    {
        ........
        $this->client = static::createClient();
        $this->router = $this->client->getContainer()->get('router');
         .....
     }


         public function testIndex()
        {
        $crawler = $this->client->request('GET', $this->router->generate($routeName, $params););

       }

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115