1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321:
<?php
namespace Guzzle\Tests\Service;
use Guzzle\Inflection\Inflector;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Service\Description\Operation;
use Guzzle\Service\Client;
use Guzzle\Service\Exception\CommandTransferException;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Tests\Service\Mock\Command\MockCommand;
use Guzzle\Service\Resource\ResourceIteratorClassFactory;
use Guzzle\Service\Command\AbstractCommand;
class ClientTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $service;
protected $serviceTest;
public function setUp()
{
$this->serviceTest = new ServiceDescription(array(
'test_command' => new Operation(array(
'doc' => 'documentationForCommand',
'method' => 'DELETE',
'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand',
'args' => array(
'bucket' => array(
'required' => true
),
'key' => array(
'required' => true
)
)
))
));
$this->service = ServiceDescription::factory(__DIR__ . '/../TestData/test_service.json');
}
public function testAllowsCustomClientParameters()
{
$client = new Mock\MockClient(null, array(
Client::COMMAND_PARAMS => array(AbstractCommand::RESPONSE_PROCESSING => 'foo')
));
$command = $client->getCommand('mock_command');
$this->assertEquals('foo', $command->get(AbstractCommand::RESPONSE_PROCESSING));
}
public function testFactoryCreatesClient()
{
$client = Client::factory(array(
'base_url' => 'http://www.test.com/',
'test' => '123'
));
$this->assertEquals('http://www.test.com/', $client->getBaseUrl());
$this->assertEquals('123', $client->getConfig('test'));
}
public function testFactoryDoesNotRequireBaseUrl()
{
$client = Client::factory();
}
public function testDescribesEvents()
{
$this->assertInternalType('array', Client::getAllEvents());
}
public function testExecutesCommands()
{
$this->getServer()->flush();
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$client = new Client($this->getServer()->getUrl());
$cmd = new MockCommand();
$client->execute($cmd);
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $cmd->getResponse());
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $cmd->getResult());
$this->assertEquals(1, count($this->getServer()->getReceivedRequests(false)));
}
public function testExecutesCommandsWithArray()
{
$client = new Client('http://www.test.com/');
$client->getEventDispatcher()->addSubscriber(new MockPlugin(array(
new Response(200),
new Response(200)
)));
$set = array(new MockCommand(), new MockCommand());
$client->execute($set);
$this->assertTrue($set[0]->isExecuted());
$this->assertTrue($set[1]->isExecuted());
}
public function testThrowsExceptionWhenInvalidCommandIsExecuted()
{
$client = new Client();
$client->execute(new \stdClass());
}
public function testThrowsExceptionWhenMissingCommand()
{
$client = new Client();
$mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\FactoryInterface');
$mock->expects($this->any())
->method('factory')
->with($this->equalTo('test'))
->will($this->returnValue(null));
$client->setCommandFactory($mock);
$client->getCommand('test');
}
public function testCreatesCommandsUsingCommandFactory()
{
$mockCommand = new MockCommand();
$client = new Mock\MockClient();
$mock = $this->getMock('Guzzle\\Service\\Command\\Factory\\FactoryInterface');
$mock->expects($this->any())
->method('factory')
->with($this->equalTo('foo'))
->will($this->returnValue($mockCommand));
$client->setCommandFactory($mock);
$command = $client->getCommand('foo', array('acl' => '123'));
$this->assertSame($mockCommand, $command);
$command = $client->getCommand('foo', array('acl' => '123'));
$this->assertSame($mockCommand, $command);
$this->assertSame($client, $command->getClient());
}
public function testOwnsServiceDescription()
{
$client = new Mock\MockClient();
$this->assertNull($client->getDescription());
$description = $this->getMock('Guzzle\\Service\\Description\\ServiceDescription');
$this->assertSame($client, $client->setDescription($description));
$this->assertSame($description, $client->getDescription());
}
public function testOwnsResourceIteratorFactory()
{
$client = new Mock\MockClient();
$method = new \ReflectionMethod($client, 'getResourceIteratorFactory');
$method->setAccessible(TRUE);
$rf1 = $method->invoke($client);
$rf = $this->readAttribute($client, 'resourceIteratorFactory');
$this->assertInstanceOf('Guzzle\\Service\\Resource\\ResourceIteratorClassFactory', $rf);
$this->assertSame($rf1, $rf);
$rf = new ResourceIteratorClassFactory('Guzzle\Tests\Service\Mock');
$client->setResourceIteratorFactory($rf);
$this->assertNotSame($rf1, $rf);
}
public function testClientResetsRequestsBeforeExecutingCommands()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nHi",
"HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\nI"
));
$client = new Mock\MockClient($this->getServer()->getUrl());
$command = $client->getCommand('mock_command');
$client->execute($command);
$client->execute($command);
$this->assertEquals('I', $command->getResponse()->getBody(true));
}
public function testClientCreatesIterators()
{
$client = new Mock\MockClient();
$iterator = $client->getIterator('mock_command', array(
'foo' => 'bar'
), array(
'limit' => 10
));
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator);
$this->assertEquals(10, $this->readAttribute($iterator, 'limit'));
$command = $this->readAttribute($iterator, 'originalCommand');
$this->assertEquals('bar', $command->get('foo'));
}
public function testClientCreatesIteratorsWithNoOptions()
{
$client = new Mock\MockClient();
$iterator = $client->getIterator('mock_command');
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator);
}
public function testClientCreatesIteratorsWithCommands()
{
$client = new Mock\MockClient();
$command = new MockCommand();
$iterator = $client->getIterator($command);
$this->assertInstanceOf('Guzzle\Tests\Service\Mock\Model\MockCommandIterator', $iterator);
$iteratorCommand = $this->readAttribute($iterator, 'originalCommand');
$this->assertSame($command, $iteratorCommand);
}
public function testClientHoldsInflector()
{
$client = new Mock\MockClient();
$this->assertInstanceOf('Guzzle\Inflection\MemoizingInflector', $client->getInflector());
$inflector = new Inflector();
$client->setInflector($inflector);
$this->assertSame($inflector, $client->getInflector());
}
public function testClientAddsGlobalCommandOptions()
{
$client = new Mock\MockClient('http://www.foo.com', array(
Client::COMMAND_PARAMS => array(
'mesa' => 'bar'
)
));
$command = $client->getCommand('mock_command');
$this->assertEquals('bar', $command->get('mesa'));
}
public function testSupportsServiceDescriptionBaseUrls()
{
$description = new ServiceDescription(array('baseUrl' => 'http://foo.com'));
$client = new Client();
$client->setDescription($description);
$this->assertEquals('http://foo.com', $client->getBaseUrl());
}
public function testMergesDefaultCommandParamsCorrectly()
{
$client = new Mock\MockClient('http://www.foo.com', array(
Client::COMMAND_PARAMS => array(
'mesa' => 'bar',
'jar' => 'jar'
)
));
$command = $client->getCommand('mock_command', array('jar' => 'test'));
$this->assertEquals('bar', $command->get('mesa'));
$this->assertEquals('test', $command->get('jar'));
}
public function testWrapsSingleCommandExceptions()
{
$client = new Mock\MockClient('http://foobaz.com');
$mock = new MockPlugin(array(new Response(401)));
$client->addSubscriber($mock);
$client->execute(new MockCommand());
}
public function testWrapsMultipleCommandExceptions()
{
$client = new Mock\MockClient('http://foobaz.com');
$mock = new MockPlugin(array(new Response(200), new Response(200), new Response(404), new Response(500)));
$client->addSubscriber($mock);
$cmds = array(new MockCommand(), new MockCommand(), new MockCommand(), new MockCommand());
try {
$client->execute($cmds);
} catch (CommandTransferException $e) {
$this->assertEquals(2, count($e->getFailedRequests()));
$this->assertEquals(2, count($e->getSuccessfulRequests()));
$this->assertEquals(2, count($e->getFailedCommands()));
$this->assertEquals(2, count($e->getSuccessfulCommands()));
foreach ($e->getSuccessfulCommands() as $c) {
$this->assertTrue($c->getResponse()->isSuccessful());
}
foreach ($e->getFailedCommands() as $c) {
$this->assertFalse($c->getRequest()->getResponse()->isSuccessful());
}
}
}
public function testGetCommandAfterTwoSetDescriptions()
{
$service1 = ServiceDescription::factory(__DIR__ . '/../TestData/test_service.json');
$service2 = ServiceDescription::factory(__DIR__ . '/../TestData/test_service_3.json');
$client = new Mock\MockClient();
$client->setDescription($service1);
$client->getCommand('foo_bar');
$client->setDescription($service2);
$client->getCommand('baz_qux');
}
}