5

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
}
c33s
  • 2,612
  • 1
  • 30
  • 43

3 Answers3

2

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.

COil
  • 7,201
  • 2
  • 50
  • 98
  • i suspected that. the problem is with a fake route i can't test if the file is really on the correct location, i want to prevent 404 results if the upload dir config is wrong. so the questsion is still how to test file uploads? – c33s Jan 08 '17 at 20:03
  • Of course. You must use another functional test framework like sélénium. It can't be done directly with symfony. – COil Jan 08 '17 at 20:07
  • testing a fake route is useless, so the test should be deleted – Sebastian Viereck Aug 09 '19 at 08:38
  • @SebastianViereck You didn't understood the answer. The answer is showing why a test on a static asset can't be done. I have never written to write such a test in an application, check out the start of the answer "You can't". – COil Aug 12 '19 at 18:19
  • No problem Sebastian. :) – COil Aug 14 '19 at 09:03
1

I've found that testing to see if the file exists (favicon.ico) using assertFileExists works well with Symfony.

/**
 * Tests to ensure a favicon exists.
 */
public function testFaviconExists()
{
    $this->assertFileExists('./public/favicon.ico');
}
ahinkle
  • 2,117
  • 3
  • 29
  • 58
0

You must use a browser test framework like panther to test static files on a webserver:

https://github.com/symfony/panther

Sebastian Viereck
  • 5,455
  • 53
  • 53