使用Behat测试涉及下载文件的某些行为。使用Goutte和Guzzle拦截文件下载,以便我可以在另一个步骤中与它交互。
//Where to put the file
$tmpFile = 'download.zip';
$handle = fopen($tmpFile, 'w');
$goutteDriver = $this->getSession()->getDriver();
$goutteClient = $goutteDriver->getClient();
/** @var 'Guzzle'Http'Client $guzzleClient */
$guzzleClient = $goutteClient->getClient();
$guzzleClient->getConfig()->set('curl.options', [CURLOPT_FILE => $handle]);
$guzzleClient->setSslVerification(false);
$goutteDriver->visit($url);
fclose($handle);
它工作得很好,但如果我在一行中运行两个不同的场景,运行相同的步骤,我得到错误:
"警告:curl_setopt_array(): 3607不是有效的文件处理资源"
编辑:我试着不关闭$句柄,然后每个场景后,它只是跳过而不是运行。还尝试使用$guzzleClient->getConfig()->remove('curl.options');
,导致后来的步骤不工作。
Edit2:问题示例:
我去掉了所有其他步骤,除了我在这里包含代码的那个步骤,即下载zip文件。
我的特性现在看起来是这样的:
Background:
Given I am logged in as an admin
Scenario: A
When I click "Export All"
Scenario: B
When I click "Export All"
当我运行它时,输出看起来像这样:
Background:
Given I am logged in as an admin
Scenario: A
When I click "Export All"
Scenario: B
Warning: curl_setopt_array(): supplied argument is not a valid File-Handle resource in C:'wamp'www'cems2'vendor'guzzle'guzzle'src'Guzzle'Http'Curl'CurlHandle.php on line 219
Call Stack:
0.0000 131776 1. {main}() C:'wamp'www'cems2'vendor'behat'behat'bin'behat:0
0.0360 1699576 2. Symfony'Component'Console'Application->run() C:'wamp'www'cems2'vendor'behat'behat'bin'behat:32
When I click "Export All" (skipped)
后面跟着一个堆栈跟踪,我在其中找不到任何对我的代码的引用。完整的堆栈跟踪在这里:http://pastebin.com/Fv48gdYm
我删除了设置curl opt文件的部分,而只是将响应的内容读取到文件句柄中。
//Where to put the file
$tmpFile = 'download.zip';
$handle = fopen($tmpFile, 'w');
$goutteDriver = $this->getSession()->getDriver();
$goutteClient = $goutteDriver->getClient();
/** @var 'Guzzle'Http'Client $guzzleClient */
$guzzleClient = $goutteClient->getClient();
//Remove this
//$guzzleClient->getConfig()->set('curl.options', [CURLOPT_FILE => $handle]);
$guzzleClient->setSslVerification(false);
$goutteDriver->visit($url);
//Add this
fwrite($handle, $goutteDriver->getContent());
fclose($handle);
现在一切都很顺利。