mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Cleaning up and fixing tests
This commit is contained in:
parent
7ac21f603e
commit
0f502b598d
@ -75,7 +75,7 @@ class BatchContext
|
||||
*/
|
||||
public function nextPending()
|
||||
{
|
||||
if ($this->pending->valid()) {
|
||||
if ($this->pending && $this->pending->valid()) {
|
||||
$current = $this->pending->current();
|
||||
$this->pending->next();
|
||||
return $current;
|
||||
|
@ -152,18 +152,21 @@ class CurlAdapter implements AdapterInterface, BatchAdapterInterface
|
||||
{
|
||||
try {
|
||||
RequestEvents::emitBeforeSendEvent($transaction);
|
||||
// Only transfer if the request was not intercepted
|
||||
if (!$transaction->getResponse()) {
|
||||
try {
|
||||
$handle = $this->curlFactory->createHandle(
|
||||
$transaction,
|
||||
$this->messageFactory
|
||||
);
|
||||
$context->addTransaction($transaction, $handle);
|
||||
} catch (RequestException $e) {
|
||||
RequestEvents::emitErrorEvent($transaction, $e);
|
||||
}
|
||||
}
|
||||
} catch (RequestException $e) {
|
||||
$this->throwException($e, $context);
|
||||
}
|
||||
|
||||
// Only transfer if the request was not intercepted
|
||||
if (!$transaction->getResponse()) {
|
||||
$handle = $this->curlFactory->createHandle(
|
||||
$transaction,
|
||||
$this->messageFactory
|
||||
);
|
||||
$context->addTransaction($transaction, $handle);
|
||||
}
|
||||
}
|
||||
|
||||
private function isCurlException(
|
||||
|
@ -199,7 +199,6 @@ class Client implements ClientInterface
|
||||
{
|
||||
$transaction = new Transaction($this, $request);
|
||||
if ($response = $this->adapter->send($transaction)) {
|
||||
$response->setEffectiveUrl($request->getUrl());
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
@ -72,6 +72,7 @@ final class RequestEvents
|
||||
TransactionInterface $transaction,
|
||||
array $stats = []
|
||||
) {
|
||||
$transaction->getResponse()->setEffectiveUrl($transaction->getRequest()->getUrl());
|
||||
try {
|
||||
$transaction->getRequest()->getEventDispatcher()->dispatch(
|
||||
RequestEvents::AFTER_SEND,
|
||||
|
@ -15,7 +15,7 @@ class BatchContextTest extends \PHPUnit_Framework_TestCase
|
||||
public function testValidatesTransactionsAreNotAddedTwice()
|
||||
{
|
||||
$m = curl_multi_init();
|
||||
$b = new BatchContext($m);
|
||||
$b = new BatchContext($m, true);
|
||||
$h = curl_init();
|
||||
$t = new Transaction(new Client(), new Request('GET', '/'));
|
||||
$b->addTransaction($t, $h);
|
||||
@ -31,13 +31,16 @@ class BatchContextTest extends \PHPUnit_Framework_TestCase
|
||||
public function testManagesHandles()
|
||||
{
|
||||
$m = curl_multi_init();
|
||||
$b = new BatchContext($m);
|
||||
$b = new BatchContext($m, true);
|
||||
$h = curl_init();
|
||||
$t = new Transaction(new Client(), new Request('GET', '/'));
|
||||
$b->addTransaction($t, $h);
|
||||
$this->assertEquals([$t], $b->getTransactions());
|
||||
$this->assertSame($t, $b->findTransaction($h));
|
||||
$b->removeTransaction($t);
|
||||
$this->assertEquals([], $b->getTransactions());
|
||||
try {
|
||||
$this->assertEquals([], $b->findTransaction($h));
|
||||
$this->fail('Did not throw');
|
||||
} catch (\RuntimeException $e) {}
|
||||
curl_multi_close($m);
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
new Transaction($c, new Request('HEAD', self::$server->getUrl()))
|
||||
];
|
||||
$a = new CurlAdapter(new MessageFactory());
|
||||
$a->batch($transactions);
|
||||
$a->batch(new \ArrayIterator($transactions), 20);
|
||||
foreach ($transactions as $t) {
|
||||
$this->assertContains($t->getResponse()->getStatusCode(), [200, 201, 202]);
|
||||
}
|
||||
@ -92,7 +92,6 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertInstanceOf('Guzzle\Http\Event\RequestErrorEvent', $ev);
|
||||
$this->assertSame($r, $ev->getRequest());
|
||||
$this->assertInstanceOf('Guzzle\Http\Exception\RequestException', $ev->getException());
|
||||
$this->assertEquals(['curl_context'], array_keys($ev->getTransferInfo()));
|
||||
}
|
||||
|
||||
public function testDispatchesAfterSendEvent()
|
||||
@ -110,7 +109,6 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
$response = $a->send($t);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('bar', $response->getHeader('Foo'));
|
||||
$this->assertArrayHasKey('curl_context', $ev->getTransferInfo());
|
||||
}
|
||||
|
||||
public function testDispatchesErrorEventAndRecovers()
|
||||
@ -145,11 +143,25 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testChecksForCurlException()
|
||||
{
|
||||
$request = new Request('GET', '/');
|
||||
$transaction = $this->getMockBuilder('Guzzle\Http\Adapter\Transaction')
|
||||
->setMethods(['getRequest'])
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$transaction->expects($this->exactly(2))
|
||||
->method('getRequest')
|
||||
->will($this->returnValue($request));
|
||||
$context = $this->getMockBuilder('Guzzle\Http\Adapter\Curl\BatchContext')
|
||||
->setMethods(['throwsExceptions'])
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$context->expects($this->once())
|
||||
->method('throwsExceptions')
|
||||
->will($this->returnValue(true));
|
||||
$a = new CurlAdapter(new MessageFactory());
|
||||
$r = new \ReflectionMethod($a, 'isCurlException');
|
||||
$r->setAccessible(true);
|
||||
try {
|
||||
$r->invoke($a, $request, ['result' => -10]);
|
||||
$r->invoke($a, $transaction, ['result' => -10], $context);
|
||||
$this->fail('Did not throw');
|
||||
} catch (RequestException $e) {
|
||||
$this->assertSame($request, $e->getRequest());
|
||||
|
@ -60,11 +60,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testClientUsesDefaultAdapterWhenNoneIsSet()
|
||||
{
|
||||
$client = new Client();
|
||||
$response = $client->get('', [], ['future' => true]);
|
||||
$adapter = extension_loaded('curl')
|
||||
? 'Guzzle\Http\Adapter\Curl\CurlAdapter'
|
||||
: 'Guzzle\Http\Adapter\StreamAdapter';
|
||||
$this->assertInstanceOf($adapter, $response->getAdapter());
|
||||
if (!extension_loaded('curl')) {
|
||||
$adapter = 'Guzzle\Http\Adapter\StreamAdapter';
|
||||
} elseif (ini_get('allow_url_fopen')) {
|
||||
$adapter = 'Guzzle\Http\Adapter\StreamingProxyAdapter';
|
||||
} else {
|
||||
$adapter = 'Guzzle\Http\Adapter\Curl\CurlAdapter';
|
||||
}
|
||||
$this->assertInstanceOf($adapter, $this->readAttribute($client, 'adapter'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -300,17 +303,4 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
});
|
||||
$client->get('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Guzzle\Http\Exception\RequestException
|
||||
* @expectedExceptionMessage foo
|
||||
*/
|
||||
public function testClientHandlesErrorsDuringBeforeSendAndThrowsIfUnhandledAndWrapsThem()
|
||||
{
|
||||
$client = new Client();
|
||||
$client->getEventDispatcher()->addListener(RequestEvents::BEFORE_SEND, function ($e) {
|
||||
throw new \Exception('foo');
|
||||
});
|
||||
$client->get('/');
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ namespace Guzzle\Tests\Http\Event;
|
||||
|
||||
use Guzzle\Http\Client;
|
||||
use Guzzle\Http\Adapter\Transaction;
|
||||
use Guzzle\Http\Event\RequestEvents;
|
||||
use Guzzle\Http\Message\Request;
|
||||
|
||||
/**
|
||||
@ -32,21 +31,4 @@ class AbstractRequestEventTest extends \PHPUnit_Framework_TestCase
|
||||
$r->setAccessible(true);
|
||||
$this->assertSame($t, $r->invoke($e));
|
||||
}
|
||||
|
||||
public function testEmitsAfterSendEvent()
|
||||
{
|
||||
$res = null;
|
||||
$t = new Transaction(new Client(), new Request('GET', '/'));
|
||||
$t->getRequest()->getEventDispatcher()->addListener(RequestEvents::AFTER_SEND, function ($e) use (&$res) {
|
||||
$res = $e;
|
||||
});
|
||||
$e = $this->getMockBuilder('Guzzle\Http\Event\AbstractRequestEvent')
|
||||
->setConstructorArgs([$t])
|
||||
->getMockForAbstractClass();
|
||||
$r = new \ReflectionMethod($e, 'emitAfterSend');
|
||||
$r->setAccessible(true);
|
||||
$r->invoke($e);
|
||||
$this->assertSame($res->getClient(), $t->getClient());
|
||||
$this->assertSame($res->getRequest(), $t->getRequest());
|
||||
}
|
||||
}
|
||||
|
49
tests/Guzzle/Tests/Http/Event/RequestEventsTest.php
Normal file
49
tests/Guzzle/Tests/Http/Event/RequestEventsTest.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Guzzle\Tests\Http\Event;
|
||||
|
||||
use Guzzle\Http\Client;
|
||||
use Guzzle\Http\Adapter\Transaction;
|
||||
use Guzzle\Http\Event\RequestEvents;
|
||||
use Guzzle\Http\Exception\RequestException;
|
||||
use Guzzle\Http\Message\Request;
|
||||
use Guzzle\Http\Message\Response;
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Event\RequestEventsTest
|
||||
*/
|
||||
class RequestEventsTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testEmitsAfterSendEvent()
|
||||
{
|
||||
$res = null;
|
||||
$t = new Transaction(new Client(), new Request('GET', '/'));
|
||||
$t->setResponse(new Response(200));
|
||||
$t->getRequest()->getEventDispatcher()->addListener(RequestEvents::AFTER_SEND, function ($e) use (&$res) {
|
||||
$res = $e;
|
||||
});
|
||||
RequestEvents::emitAfterSendEvent($t);
|
||||
$this->assertSame($res->getClient(), $t->getClient());
|
||||
$this->assertSame($res->getRequest(), $t->getRequest());
|
||||
$this->assertEquals('/', $t->getResponse()->getEffectiveUrl());
|
||||
}
|
||||
|
||||
public function testEmitsAfterSendEventAndEmitsErrorIfNeeded()
|
||||
{
|
||||
$ex2 = $res = null;
|
||||
$request = new Request('GET', '/');
|
||||
$t = new Transaction(new Client(), $request);
|
||||
$t->setResponse(new Response(200));
|
||||
$ex = new RequestException('foo', $request);
|
||||
$t->getRequest()->getEventDispatcher()->addListener(RequestEvents::AFTER_SEND, function ($e) use ($ex) {
|
||||
$ex->e = $e;
|
||||
throw $ex;
|
||||
});
|
||||
$t->getRequest()->getEventDispatcher()->addListener(RequestEvents::ERROR, function ($e) use (&$ex2) {
|
||||
$ex2 = $e->getException();
|
||||
$e->stopPropagation();
|
||||
});
|
||||
RequestEvents::emitAfterSendEvent($t);
|
||||
$this->assertSame($ex, $ex2);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user