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