Skip to content

Commit 13872a0

Browse files
moufmoufrenatomefi
authored andcommitted
Fix curl reset (php-vcr#248)
### Context When using PHP-VCR with Guzzle 6 with async requests, the first request is playing correctly but the next request is putting Guzzle in an infinite loop. It took me a whole day but I traced back this problem to the way the "curl_reset" hook is written. It clears the request but not the response. Hence, when another request is performed (even on a different URL) the same response is sent. This weird behaviour is causing an infinite loop in Guzzle 6. This problem is probably exactly the same as the issue php-vcr#211. This PR should fix it. ### What has been done The first commit of this PR contains only the failing test (to showcase the problem). You see that the test never completes (because of the infinite loop): https://travis-ci.org/php-vcr/php-vcr/jobs/366140696 The second commit is the fix. I simply added one line in the curl_reset hook to remove the response as well as the request. Note: this PR is based on the branch from PR php-vcr#246 since I need unit tests from Guzzle 6 that are not yet merged.
1 parent 7501430 commit 13872a0

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/VCR/LibraryHooks/CurlHook.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public static function curlReset($curlHandle)
182182
\curl_reset($curlHandle);
183183
self::$requests[(int) $curlHandle] = new Request('GET', null);
184184
self::$curlOptions[(int) $curlHandle] = array();
185+
unset(self::$responses[(int) $curlHandle]);
185186
}
186187

187188
/**

tests/integration/guzzle/6/phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<testsuites>
99
<testsuite>
1010
<directory>../test</directory>
11+
<directory>test</directory>
1112
</testsuite>
1213
</testsuites>
1314

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace VCR\Example;
4+
5+
use GuzzleHttp\Client;
6+
use org\bovigo\vfs\vfsStream;
7+
8+
/**
9+
* Tests example request.
10+
*/
11+
class AsyncTest extends \PHPUnit_Framework_TestCase
12+
{
13+
const TEST_GET_URL = 'http://api.chew.pro/trbmb';
14+
const TEST_GET_URL_2 = 'http://api.chew.pro/trbmb?foo=42';
15+
16+
public function setUp()
17+
{
18+
vfsStream::setup('testDir');
19+
\VCR\VCR::configure()->setCassettePath(vfsStream::url('testDir'));
20+
}
21+
22+
public function testAsyncLock()
23+
{
24+
\VCR\VCR::turnOn();
25+
\VCR\VCR::insertCassette('test-cassette.yml');
26+
27+
$client = new Client();
28+
$promise = $client->getAsync(self::TEST_GET_URL);
29+
$response = $promise->wait();
30+
$promise = $client->getAsync(self::TEST_GET_URL_2);
31+
$promise->wait();
32+
// Let's check that we can perform 2 async request on different URLs without locking.
33+
// Solves https://github.com/php-vcr/php-vcr/issues/211
34+
35+
$this->assertValidGETResponse(\GuzzleHttp\json_decode($response->getBody()));
36+
37+
\VCR\VCR::turnOff();
38+
}
39+
40+
protected function assertValidGETResponse($info)
41+
{
42+
$this->assertInternalType('array', $info, 'Response is not an array.');
43+
$this->assertArrayHasKey('0', $info, 'API did not return any value.');
44+
}
45+
}

0 commit comments

Comments
 (0)