Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for static files located in the web folder in symfony with phpunit?

sysinfo:

  • PHPUnit 5.7.4
  • PHP 7.0.13
  • Symfony 3.2.1

i am trying to follow a link on a "download" page and verify the file is downloadable but when i follow the link $client->click($crawlerDownload->link()); i get a 404. is it not possible for the symfony $client to access a static file in the webdirectory? how can i test this?

the favicon test is the simplified version of the testcase.

public function testPressDownload()
{
    $client = static::createClient();
    $client->followRedirects(false);

    //create fixture file
    $kernelDir = $client->getKernel()->getRootDir();
    $file = "${kernelDir}/../web/download/example.zip";
    file_put_contents($file, "dummy content");


    $crawler = $client->request('GET', '/files');
    $this->assertEquals(200, $client->getResponse()->getStatusCode()); //ok

    $crawlerDownload = $crawler
        ->filter('a[title="example.zip"]')
    ;
    $this->assertEquals(1, $crawlerDownload->count()); //ok


    $client->click($crawlerDownload->link());
    $this->assertEquals(200, $client->getResponse()->getStatusCode()); //fails 404
}


public function testFavicon()
{    
    $crawler = $client->request('GET', '/favicon.ico');
    $this->assertEquals(200, $client->getResponse()->getStatusCode()); //fails 404
}
like image 434
c33s Avatar asked Oct 18 '22 19:10

c33s


1 Answers

You can't, tests are bootstraping the application, it's not a "real web server" so when requesting /favicon.ico, it searches for a route in the application corresponding to this path which is not found.

To verify this, create a fake route:

/**
 * @Route("/favicon.ico", name="fake_favicon_route")
 *
 * @return Response
 */

You will see that the test will now pass.

like image 94
COil Avatar answered Nov 01 '22 16:11

COil