使用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);
它工作正常,但如果我连续运行两个不同的方案运行同一步骤,我得到错误:
“Warning: curl_setopt_array(): 3607 is not a valid File-Handle resource”
编辑:我试图不关闭$handle,然后它之后的每个场景只是跳过而不是运行.还尝试使用$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);
现在一切都很完美.