Skip to content

Commit

Permalink
Updating tests. Adding support for asynchronous results
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Oct 16, 2014
1 parent 8635fd5 commit fb89316
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 271 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"GuzzleHttp\\Command\\Guzzle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"GuzzleHttp\\Tests\\Command\\Guzzle\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.3-dev"
Expand Down
28 changes: 25 additions & 3 deletions src/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Command\Guzzle\Subscriber\ValidateInput;
use GuzzleHttp\Event\HasEmitterTrait;
use GuzzleHttp\Command\ServiceClientInterface;
use GuzzleHttp\Ring\Future\FutureArray;

/**
* Default Guzzle web service client implementation.
Expand Down Expand Up @@ -58,21 +59,42 @@ public function __construct(
public function getCommand($name, array $args = [])
{
$factory = $this->commandFactory;

// Determine if a future array should be returned.
if (!empty($args['@future'])) {
$future = !empty($args['@future']);
unset($args['@future']);
} else {
$future = false;
}

// Merge in default command options
$args += $this->getConfig('defaults');

if (!($command = $factory($name, $args, $this))) {
throw new \InvalidArgumentException("No operation found named $name");
if ($command = $factory($name, $args, $this)) {
$command->setFuture($future);
return $command;
}

return $command;
throw new \InvalidArgumentException("No operation found named $name");
}

public function getDescription()
{
return $this->description;
}

protected function createFutureResult(CommandTransaction $transaction)
{
return new FutureArray(
$transaction->response->then(function () use ($transaction) {
return $transaction->result;
}),
[$transaction->response, 'wait'],
[$transaction->response, 'cancel']
);
}

protected function serializeRequest(CommandTransaction $trans)
{
$fn = $this->serializer;
Expand Down
2 changes: 1 addition & 1 deletion src/RequestLocation/XmlLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function after(
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$additional->setName($key);
$this->visitWithValue($value, $additional, $command);
$this->visitWithValue($value, $additional, $operation);
}
}
$additional->setName(null);
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function prepareRequest(
/* @var Parameter $param */
$location = $param->getLocation();
// Skip parameters that have not been set or are URI location
if ($location == 'uri' || !$operation->hasParam($name)) {
if ($location == 'uri' || !$trans->command->hasParam($name)) {
continue;
}
if (!isset($this->requestLocations[$location])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Subscriber/ValidateInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function __construct(

public function getEvents()
{
return ['prepare' => ['onPrepare']];
return ['init' => ['onInit']];
}

public function onPrepare(InitEvent $event)
public function onInit(InitEvent $event)
{
$command = $event->getCommand();
$errors = [];
Expand Down
25 changes: 0 additions & 25 deletions tests/CommandTest.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/DescriptionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Command\Guzzle\Description;
Expand Down
91 changes: 6 additions & 85 deletions tests/GuzzleClientTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php

namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Client;
use GuzzleHttp\Command\CommandTransaction;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Command\Event\PrepareEvent;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Event\BeforeEvent;
Expand All @@ -31,8 +27,6 @@ public function testHasConfig()
$this->assertEquals([], $guzzle->getConfig('defaults'));
$guzzle->setConfig('abc/123', 'listen');
$this->assertEquals('listen', $guzzle->getConfig('abc/123'));

$this->assertCount(2, $guzzle->getEmitter()->listeners('prepare'));
$this->assertCount(1, $guzzle->getEmitter()->listeners('process'));
}

Expand All @@ -44,7 +38,6 @@ public function testAddsSubscribersWhenTrue()
'validate' => true,
'process' => true
]);
$this->assertCount(2, $guzzle->getEmitter()->listeners('prepare'));
$this->assertCount(1, $guzzle->getEmitter()->listeners('process'));
}

Expand All @@ -56,13 +49,12 @@ public function testDisablesSubscribersWhenFalse()
'validate' => false,
'process' => false
]);
$this->assertCount(1, $guzzle->getEmitter()->listeners('prepare'));
$this->assertCount(0, $guzzle->getEmitter()->listeners('process'));
}

public function testCanUseCustomConfigFactory()
{
$mock = $this->getMockBuilder('GuzzleHttp\\Command\\Guzzle\\Command')
$mock = $this->getMockBuilder('GuzzleHttp\\Command\\Command')
->disableOriginalConstructor()
->getMock();
$client = new Client();
Expand All @@ -78,8 +70,8 @@ public function testCanUseCustomConfigFactory()

public function testMagicMethodExecutesCommands()
{
$mock = $this->getMockBuilder('GuzzleHttp\\Command\\Guzzle\\Command')
->disableOriginalConstructor()
$mock = $this->getMockBuilder('GuzzleHttp\\Command\\Command')
->setConstructorArgs(['foo'])
->getMock();
$client = new Client();
$description = new Description([]);
Expand Down Expand Up @@ -122,56 +114,13 @@ public function testDefaultFactoryChecksWithUppercaseToo()
$c = new GuzzleClient(new Client(), $description);
$f = GuzzleClient::defaultCommandFactory($description);
$command1 = $f('foo', [], $c);
$this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Command', $command1);
$this->assertInstanceOf('GuzzleHttp\\Command\\Command', $command1);
$this->assertEquals('Foo', $command1->getName());
$command2 = $f('Foo', [], $c);
$this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Command', $command2);
$this->assertInstanceOf('GuzzleHttp\\Command\\Command', $command2);
$this->assertEquals('Foo', $command2->getName());
}

/**
* @expectedException \GuzzleHttp\Command\Exception\CommandException
*/
public function testPassesCommandExceptionsThrough()
{
$description = new Description(['operations' => ['Foo' => []]]);
$guzzle = new GuzzleClient(new Client(), $description);
$command = $guzzle->getCommand('foo');
$command->getEmitter()->on('prepare', function(PrepareEvent $event) {
throw new CommandException(
'foo',
$event->getTransaction()
);
}, 1);
$guzzle->execute($command);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage msg
*/
public function testWrapsExceptionsInCommandExceptions()
{
$description = new Description(['operations' => ['Foo' => []]]);
$guzzle = new GuzzleClient(new Client(), $description);
$command = $guzzle->getCommand('foo');
$command->getEmitter()->on('prepare', function(PrepareEvent $event) {
throw new \Exception('msg');
}, 1);
$guzzle->execute($command);
}

public function testReturnsInterceptedResult()
{
$description = new Description(['operations' => ['Foo' => []]]);
$guzzle = new GuzzleClient(new Client(), $description);
$command = $guzzle->getCommand('foo');
$command->getEmitter()->on('prepare', function(PrepareEvent $event) {
$event->setResult('test');
}, 1);
$this->assertEquals('test', $guzzle->execute($command));
}

public function testReturnsProcessedResponse()
{
$client = new Client();
Expand All @@ -193,36 +142,8 @@ public function testReturnsProcessedResponse()
]);
$guzzle = new GuzzleClient($client, $description);
$command = $guzzle->getCommand('foo');
$command->getEmitter()->on('prepare', function(PrepareEvent $event) {
$event->setRequest($event->getClient()->getHttpClient()->createRequest('GET', 'http://httbin.org'));
}, 1);
$result = $guzzle->execute($command);
$this->assertInstanceOf('GuzzleHttp\\Command\\Model', $result);
$this->assertInternalType('array', $result);
$this->assertEquals(201, $result['code']);
}

public function testExecutesCommandsInParallel()
{
$client = $this->getMockBuilder('GuzzleHttp\\Client')
->setMethods(['sendAll'])
->getMock();

$description = new Description(['operations' => ['Foo' => []]]);
$guzzle = new GuzzleClient($client, $description);
$command = $guzzle->getCommand('foo');
$request = $client->createRequest('GET', 'http://httbin.org');
$command->getEmitter()->on('prepare', function (PrepareEvent $e) use ($request) {
$e->setRequest($request);
}, 1);

$client->expects($this->once())
->method('sendAll')
->will($this->returnCallback(function ($requests, $options) use ($request) {
$this->assertEquals(10, $options['parallel']);
$this->assertTrue($requests->valid());
$this->assertSame($request, $requests->current());
}));

$guzzle->executeAll([$command], ['parallel' => 10]);
}
}
1 change: 0 additions & 1 deletion tests/OperationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Guzzle\Tests\Service\Description;

use GuzzleHttp\Command\Guzzle\Operation;
Expand Down
1 change: 0 additions & 1 deletion tests/ParameterTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Guzzle\Tests\Service\Description;

use GuzzleHttp\Command\Guzzle\Parameter;
Expand Down
18 changes: 0 additions & 18 deletions tests/RequestLocation/AbstractLocationTest.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/RequestLocation/BodyLocationTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation;
use GuzzleHttp\Message\Request;

/**
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\BodyLocation
*/
class BodyLocationTest extends AbstractLocationTest
class BodyLocationTest extends \PHPUnit_Framework_TestCase
{
public function testVisitsLocation()
{
$location = new BodyLocation('body');
$command = $this->getCommand();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$location->visit($command, $request, $param, []);
Expand Down
8 changes: 4 additions & 4 deletions tests/RequestLocation/HeaderLocationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace GuzzleHttp\Tests\Command\Guzzle;

use GuzzleHttp\Command\Command;
use GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Command\Guzzle\Parameter;
Expand All @@ -12,12 +12,12 @@
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\HeaderLocation
* @covers \GuzzleHttp\Command\Guzzle\RequestLocation\AbstractLocation
*/
class HeaderLocationTest extends AbstractLocationTest
class HeaderLocationTest extends \PHPUnit_Framework_TestCase
{
public function testVisitsLocation()
{
$location = new HeaderLocation('header');
$command = $this->getCommand();
$command = new Command('foo', ['foo' => 'bar']);
$request = new Request('POST', 'http://httbin.org');
$param = new Parameter(['name' => 'foo']);
$location->visit($command, $request, $param, []);
Expand All @@ -27,7 +27,7 @@ public function testVisitsLocation()
public function testAddsAdditionalProperties()
{
$location = new HeaderLocation('header');
$command = $this->getCommand();
$command = new Command('foo', ['foo' => 'bar']);
$command['add'] = 'props';
$operation = new Operation([
'additionalParameters' => [
Expand Down
Loading

0 comments on commit fb89316

Please sign in to comment.