Beta J-602 !
Logo PhpUnit

Test phpunit - tester url publiques et sécurisées

Par Julie, le 23/06/2018

First list all routes

bin/console debug:router

Then in order to see details's route

bin/console debug:router routeName

Create a specific Test For a Controller route (respect the tree)

<?php

namespace App\Tests\Controller\Core;


use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecurityControllerTest extends WebTestCase
{
    public function testLoginPageIsUp()
    {
        $client = static::createClient();
        $client->request('GET', '/security/login');

        $this->assertSame(200, $client->getResponse()->getStatusCode());
    }
}

You also write this test as this 

<?php

namespace App\Tests\Controller\Core;


use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class SecurityControllerTest extends WebTestCase
{
    private $client = null;

    public function setUp() {
        $this->client = static::createClient();
    }

    public function testLoginPageIsUp()
    {
        $this->client->request('GET', '/security/login');

        static::assertEquals(Response::HTTP_OK,
            $this->client->getResponse()->getStatusCode());
    }
}

Create a default class to test all routes

<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class DefaultControllerTest extends WebTestCase
{
    /**
     * Search the url in th getPublicUrls Function
     * @dataProvider getPublicUrls
     */
    public function testPublicUrls($url)
    {
        $client = static::createClient();
        $client->request('GET', $url);

        $this->assertSame(
            Response::HTTP_OK,
            $client->getResponse()->getStatusCode(),
            sprintf('The %s public URL loads correctly.', $url)
        );
    }

    /**
     * Test the home page (the only who is public)
     * @return \Generator
     */
    public function getPublicUrls()
    {
        yield ['/security/login']; 
    }

    /**
     * Search in the getSecureUrls the url needed to be logged
     * If user not logged, check if the redirect to the login is ok
     * @dataProvider getSecureUrls
     */
    public function testSecureUrls($url)
    {
        $client = self::createClient(array(), array(
            'HTTP_HOST' => 'mon-site.test',
        ));
        $client->request('GET', $url);

        $response = $client->getResponse();
        $this->assertSame(Response::HTTP_FOUND, $response->getStatusCode());
        $this->assertSame(
            'http://mon-site.test/security/login',
            $response->getTargetUrl(),
            sprintf('The %s secure URL redirects to the login form.', $url)
        );
    }

    /**
     * Test the pages who need to be logged
     * @return \Generator
     */
    public function getSecureUrls()
    {
        yield ['/'];
        yield ['/annuaire'];
        yield ['/service'];
        yield ['/courrier'];
        // & many Others
    }
}

Cet article est paru dans les catégories suivantes: Tests