Search code examples
phpsymfonyphpunitfunctional-testing

PHPUnit path test for download files in Symfony2


I wrote a test to check for wrong/dead urls in my Symfony2 application.

I use functional tests as suggested in http://symfony.com/doc/current/book/testing.html

The test opens a path, checks all

    <a...></a>

tags and then creates a request for these links:

    $client = (new ClientProvider())->getClient();
    $crawler = $client->request('GET', $route);
    $linkList = $crawler->filter('a');
    foreach ($linkList as $link) {
        $url = $link->getAttribute('href');
        $crawler = $client->request('GET', $link->getAttribute('href'));
        //... some assertion
    }

The Problem is, that some a-tags contain paths to files, e.g. "/files/sample.pdf". (filepath is web/files/...)

I want to check if those files exist, but I get a 404-error from PHPUnit.

I assume this is because phpunit does not use a webserver but works directly with the app and so it can't access files that are under "web/files/...".

Is that correct, or is there a way to test this?


Solution

  • Its correct, phpunit is not going to your webserver wich means there is no rewrite/serving files going on. What you can do is regex the URL and look for a file pattern, if found you use another method to test for files, like one which goes into your public folder to look for it.

    Another option is to have an action to control the files distribution, this way you have more secutiry and control, but requires you to change your application.