如何使用phpunit测试symfony中位于web文件夹中的静态文件? [英] 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
我正在尝试点击下载"页面上的链接,并验证文件是否可下载,但是当我点击链接$client->click($crawlerDownload->link());
时,我得到了404
. symfony $client
是否无法访问Web目录中的静态文件?我该如何测试?
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?
favicon测试是测试用例的简化版本.
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
}
推荐答案
您不能,测试正在引导应用程序,它不是真实的网络服务器",因此当请求/favicon.ico
时,它将在找不到与此路径对应的应用程序.
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.
这篇关于如何使用phpunit测试symfony中位于web文件夹中的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!