如何使用phpunit测试位于symfony中Web文件夹中的静态文件?

c33*_*33s 5 php phpunit symfony

sysinfo:

  • PHPUnit 5.7.4
  • PHP 7.0.13
  • Symfony 3.2.1

我试图点击“下载”页面上的链接,并验证文件是否可下载,但是当我点击链接时,$client->click($crawlerDownload->link());我得到了404。symfony $client无法访问Web目录中的静态文件吗?我该如何测试?

Favicon测试是测试用例的简化版本。

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
}
Run Code Online (Sandbox Code Playgroud)

COi*_*Oil 2

你不能,测试正在引导应用程序,它不是“真正的网络服务器”,因此在请求时/favicon.ico,它会在应用程序中搜索与该路径相对应的路由,但未找到。

要验证这一点,请创建一条假路由:

/**
 * @Route("/favicon.ico", name="fake_favicon_route")
 *
 * @return Response
 */
Run Code Online (Sandbox Code Playgroud)

您将看到测试现在将通过。