Open Journal Systems  3.3.0
CommandTest.php
1 <?php
2 
4 
16 
21 {
23  {
24  $command = new MockCommand();
25  $this->assertEquals('123', $command->get('test'));
26  $this->assertFalse($command->isPrepared());
27  $this->assertFalse($command->isExecuted());
28  }
29 
30  public function testDeterminesShortName()
31  {
32  $api = new Operation(array('name' => 'foobar'));
33  $command = new MockCommand(array(), $api);
34  $this->assertEquals('foobar', $command->getName());
35 
36  $command = new MockCommand();
37  $this->assertEquals('mock_command', $command->getName());
38 
39  $command = new Sub();
40  $this->assertEquals('sub.sub', $command->getName());
41  }
42 
47  {
48  $command = new MockCommand();
49  $command->getRequest();
50  }
51 
53  {
54  $response = new Response(200);
55  $client = $this->getClient();
56  $this->setMockResponse($client, array($response));
57  $command = new MockCommand();
58  $command->setClient($client);
59  $this->assertSame($response, $command->getResponse());
60  $this->assertSame($response, $command->getResponse());
61  }
62 
64  {
65  $response = new Response(200);
66  $client = $this->getClient();
67  $this->setMockResponse($client, array($response));
68  $command = new MockCommand();
69  $command->setClient($client);
70  $this->assertSame($response, $command->getResult());
71  $this->assertSame($response, $command->getResult());
72  }
73 
74  public function testSetClient()
75  {
76  $command = new MockCommand();
77  $client = $this->getClient();
78 
79  $command->setClient($client);
80  $this->assertEquals($client, $command->getClient());
81 
82  unset($client);
83  unset($command);
84 
85  $command = new MockCommand();
86  $client = $this->getClient();
87 
88  $command->setClient($client)->prepare();
89  $this->assertEquals($client, $command->getClient());
90  $this->assertTrue($command->isPrepared());
91  }
92 
93  public function testExecute()
94  {
95  $client = $this->getClient();
96  $response = new Response(200, array(
97  'Content-Type' => 'application/xml'
98  ), '<xml><data>123</data></xml>');
99  $this->setMockResponse($client, array($response));
100  $command = new MockCommand();
101  $this->assertSame($command, $command->setClient($client));
102 
103  // Returns the result of the command
104  $this->assertInstanceOf('SimpleXMLElement', $command->execute());
105 
106  $this->assertTrue($command->isPrepared());
107  $this->assertTrue($command->isExecuted());
108  $this->assertSame($response, $command->getResponse());
109  $this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $command->getRequest());
110  // Make sure that the result was automatically set to a SimpleXMLElement
111  $this->assertInstanceOf('SimpleXMLElement', $command->getResult());
112  $this->assertEquals('123', (string) $command->getResult()->data);
113  }
114 
116  {
117  $client = $this->getClient();
118  $this->setMockResponse($client, array(
119  new \Guzzle\Http\Message\Response(200, array(
120  'Content-Type' => 'application/json'
121  ), '{ "key": "Hi!" }'
122  )
123  ));
124  $command = new MockCommand();
125  $command->setClient($client);
126  $command->execute();
127  $this->assertEquals(array(
128  'key' => 'Hi!'
129  ), $command->getResult());
130  }
131 
136  {
137  $json = '{ "key": "Hi!" }invalid';
138  // Some implementations of php-json extension are not strict enough
139  // and allow to parse invalid json ignoring invalid parts
140  // See https://github.com/remicollet/pecl-json-c/issues/5
141  if (json_decode($json) && JSON_ERROR_NONE === json_last_error()) {
142  $this->markTestSkipped('php-pecl-json library regression issues');
143  }
144 
145  $client = $this->getClient();
146  $this->setMockResponse($client, array(
147  new \Guzzle\Http\Message\Response(200, array(
148  'Content-Type' => 'application/json'
149  ), $json
150  )
151  ));
152  $command = new MockCommand();
153  $command->setClient($client);
154  $command->execute();
155  }
156 
157  public function testProcessResponseIsNotXml()
158  {
159  $client = $this->getClient();
160  $this->setMockResponse($client, array(
161  new Response(200, array(
162  'Content-Type' => 'application/octet-stream'
163  ), 'abc,def,ghi')
164  ));
165  $command = new MockCommand();
166  $client->execute($command);
167 
168  // Make sure that the result was not converted to XML
169  $this->assertFalse($command->getResult() instanceof \SimpleXMLElement);
170  }
171 
176  {
177  $command = new MockCommand();
178  $command->execute();
179  }
180 
185  {
186  $command = new MockCommand();
187  $command->prepare();
188  }
189 
191  {
192  $command = new MockCommand();
193  $command->getRequestHeaders()->set('test', '123');
194  $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
195  $this->assertEquals('123', $command->getRequestHeaders()->get('test'));
196 
197  $command->setClient($this->getClient())->prepare();
198  $this->assertEquals('123', (string) $command->getRequest()->getHeader('test'));
199  }
200 
202  {
203  $command = new MockCommand(array(AbstractCommand::HEADERS_OPTION => array('Foo' => 'Bar')));
204  $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
205  $this->assertEquals('Bar', $command->getRequestHeaders()->get('Foo'));
206  }
207 
208  private function getOperation()
209  {
210  return new Operation(array(
211  'name' => 'foobar',
212  'httpMethod' => 'POST',
213  'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand',
214  'parameters' => array(
215  'test' => array(
216  'default' => '123',
217  'type' => 'string'
218  )
219  )));
220  }
221 
222  public function testCommandsUsesOperation()
223  {
224  $api = $this->getOperation();
225  $command = new MockCommand(array(), $api);
226  $this->assertSame($api, $command->getOperation());
227  $command->setClient($this->getClient())->prepare();
228  $this->assertEquals('123', $command->get('test'));
229  $this->assertSame($api, $command->getOperation($api));
230  }
231 
232  public function testCloneMakesNewRequest()
233  {
234  $client = $this->getClient();
235  $command = new MockCommand(array(), $this->getOperation());
236  $command->setClient($client);
237 
238  $command->prepare();
239  $this->assertTrue($command->isPrepared());
240 
241  $command2 = clone $command;
242  $this->assertFalse($command2->isPrepared());
243  }
244 
245  public function testHasOnCompleteMethod()
246  {
247  $that = $this;
248  $called = 0;
249 
250  $testFunction = function($command) use (&$called, $that) {
251  $called++;
252  $that->assertInstanceOf('Guzzle\Service\Command\CommandInterface', $command);
253  };
254 
255  $client = $this->getClient();
256  $command = new MockCommand(array(
257  'command.on_complete' => $testFunction
258  ), $this->getOperation());
259  $command->setClient($client);
260 
261  $command->prepare()->setResponse(new Response(200), true);
262  $command->execute();
263  $this->assertEquals(1, $called);
264  }
265 
270  {
271  $client = $this->getClient();
272  $command = new MockCommand();
273  $command->setOnComplete('foo');
274  }
275 
276  public function testCanSetResultManually()
277  {
278  $client = $this->getClient();
279  $client->getEventDispatcher()->addSubscriber(new MockPlugin(array(
280  new Response(200)
281  )));
282  $command = new MockCommand();
283  $client->execute($command);
284  $command->setResult('foo!');
285  $this->assertEquals('foo!', $command->getResult());
286  }
287 
288  public function testCanInitConfig()
289  {
290  $command = $this->getMockBuilder('Guzzle\\Service\\Command\\AbstractCommand')
291  ->setConstructorArgs(array(array(
292  'foo' => 'bar'
293  ), new Operation(array(
294  'parameters' => array(
295  'baz' => new Parameter(array(
296  'default' => 'baaar'
297  ))
298  )
299  ))))
300  ->getMockForAbstractClass();
301 
302  $this->assertEquals('bar', $command['foo']);
303  $this->assertEquals('baaar', $command['baz']);
304  }
305 
307  {
308  $command = new MockCommand(array(
309  'foo' => 'bar',
310  'curl.options' => array('CURLOPT_PROXYPORT' => 8080)
311  ));
312  $client = new Client();
313  $command->setClient($client);
314  $request = $command->prepare();
315  $this->assertEquals(8080, $request->getCurlOptions()->get(CURLOPT_PROXYPORT));
316  }
317 
318  public function testIsInvokable()
319  {
320  $client = $this->getClient();
321  $response = new Response(200);
322  $this->setMockResponse($client, array($response));
323  $command = new MockCommand();
324  $command->setClient($client);
325  // Returns the result of the command
326  $this->assertSame($response, $command());
327  }
328 
329  public function testCreatesDefaultOperation()
330  {
331  $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')->getMockForAbstractClass();
332  $this->assertInstanceOf('Guzzle\Service\Description\Operation', $command->getOperation());
333  }
334 
336  {
337  $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')->getMockForAbstractClass();
338  $v = new SchemaValidator();
339  $command->setValidator($v);
340  $this->assertSame($v, $this->readAttribute($command, 'validator'));
341  }
342 
343  public function testCanDisableValidation()
344  {
345  $command = new MockCommand();
346  $command->setClient(new \Guzzle\Service\Client());
347  $v = $this->getMockBuilder('Guzzle\Service\Description\SchemaValidator')
348  ->setMethods(array('validate'))
349  ->getMock();
350  $v->expects($this->never())->method('validate');
351  $command->setValidator($v);
352  $command->set(AbstractCommand::DISABLE_VALIDATION, true);
353  $command->prepare();
354  }
355 
357  {
358  $command = new MockCommand(array('test' => 123, 'foo' => 'bar'));
359  $command->setClient(new \Guzzle\Service\Client());
360  $command->prepare();
361  $this->assertEquals(123, $command->get('test'));
362  $this->assertEquals('bar', $command->get('foo'));
363  }
364 
366  {
367  $command = new MockCommand();
368  $command->setClient(new \Guzzle\Service\Client());
369  $command->prepare();
370  $this->assertEquals(123, $command->get('test'));
371  $this->assertEquals('abc', $command->get('_internal'));
372  }
373 
379  {
380  $command = new MockCommand();
381  $command->setClient(new \Guzzle\Service\Client());
382  $v = $this->getMockBuilder('Guzzle\Service\Description\SchemaValidator')
383  ->setMethods(array('validate', 'getErrors'))
384  ->getMock();
385  $v->expects($this->any())->method('validate')->will($this->returnValue(false));
386  $v->expects($this->any())->method('getErrors')->will($this->returnValue(array('[Foo] Baz', '[Bar] Boo')));
387  $command->setValidator($v);
388  $command->prepare();
389  }
390 
396  {
397  $description = ServiceDescription::factory(array(
398  'operations' => array(
399  'foo' => array(
400  'parameters' => array(
401  'baz' => array('type' => 'integer')
402  ),
403  'additionalParameters' => array(
404  'type' => 'string'
405  )
406  )
407  )
408  ));
409 
410  $client = new Client();
411  $client->setDescription($description);
412  $command = $client->getCommand('foo', array(
413  'abc' => false,
414  'command.headers' => array('foo' => 'bar')
415  ));
416  $command->prepare();
417  }
418 
420  {
421  $validationErrors = array('[Foo] Baz', '[Bar] Boo');
422  $command = new MockCommand();
423  $command->setClient(new \Guzzle\Service\Client());
424 
425  $this->assertFalse($command->getValidationErrors());
426 
427  $v = $this->getMockBuilder('Guzzle\Service\Description\SchemaValidator')
428  ->setMethods(array('validate', 'getErrors'))
429  ->getMock();
430  $v->expects($this->any())->method('getErrors')->will($this->returnValue($validationErrors));
431  $command->setValidator($v);
432 
433  $this->assertEquals($validationErrors, $command->getValidationErrors());
434  }
435 
436  public function testCanChangeResponseBody()
437  {
438  $body = EntityBody::factory();
439  $command = new MockCommand();
440  $command->setClient(new \Guzzle\Service\Client());
441  $command->set(AbstractCommand::RESPONSE_BODY, $body);
442  $request = $command->prepare();
443  $this->assertSame($body, $this->readAttribute($request, 'responseBody'));
444  }
445 }
Guzzle\Tests\Service\Command\CommandTest\testExecuteThrowsExceptionWhenNoClientIsSet
testExecuteThrowsExceptionWhenNoClientIsSet()
Definition: CommandTest.php:175
Guzzle\Tests\Service\Command\AbstractCommandTest
Definition: AbstractCommandTest.php:8
Guzzle\Tests\Service\Command\CommandTest\testCanSetResultManually
testCanSetResultManually()
Definition: CommandTest.php:276
Guzzle\Service\Description\Operation
Definition: Operation.php:10
Guzzle\Tests\Service\Command\CommandTest\testCommandsUsesOperation
testCommandsUsesOperation()
Definition: CommandTest.php:222
Guzzle\Tests\Service\Command\CommandTest\testCommandsAllowsCustomRequestHeadersAsArray
testCommandsAllowsCustomRequestHeadersAsArray()
Definition: CommandTest.php:201
Guzzle\Tests\Service\Command\CommandTest\testCanAccessValidationErrorsFromCommand
testCanAccessValidationErrorsFromCommand()
Definition: CommandTest.php:419
Guzzle\Tests\Service\Command\CommandTest
Definition: CommandTest.php:20
Guzzle\Tests\Service\Command\CommandTest\testIsInvokable
testIsInvokable()
Definition: CommandTest.php:318
Guzzle\Tests\Service\Command\CommandTest\testGetResponseExecutesCommandsWhenNeeded
testGetResponseExecutesCommandsWhenNeeded()
Definition: CommandTest.php:52
Guzzle\Service\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php:25
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Tests\Service\Command\CommandTest\testDeterminesShortName
testDeterminesShortName()
Definition: CommandTest.php:30
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
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\Command\CommandTest\testCanDisableValidation
testCanDisableValidation()
Definition: CommandTest.php:343
Guzzle\Tests\Service\Command\CommandTest\testPrepareThrowsExceptionWhenNoClientIsSet
testPrepareThrowsExceptionWhenNoClientIsSet()
Definition: CommandTest.php:184
Guzzle\Tests\Service\Mock\Command\MockCommand
Definition: MockCommand.php:7
Guzzle\Tests\Service\Command\CommandTest\testAddsCurlOptionsToRequestsWhenPreparing
testAddsCurlOptionsToRequestsWhenPreparing()
Definition: CommandTest.php:306
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Service\Command\CommandTest\testValidatesAdditionalParameters
testValidatesAdditionalParameters()
Definition: CommandTest.php:395
Guzzle\Service\Description\ServiceDescription
Definition: ServiceDescription.php:11
Guzzle\Tests\Service\Command\CommandTest\testCanInitConfig
testCanInitConfig()
Definition: CommandTest.php:288
Guzzle\Tests\Service\Command\CommandTest\testConstructorAddsDefaultParams
testConstructorAddsDefaultParams()
Definition: CommandTest.php:22
Guzzle\Tests\Service\Command\CommandTest\testValidatesCommandBeforeSending
testValidatesCommandBeforeSending()
Definition: CommandTest.php:378
Guzzle\Tests\Service\Command\CommandTest\testGetRequestThrowsExceptionBeforePreparation
testGetRequestThrowsExceptionBeforePreparation()
Definition: CommandTest.php:46
Guzzle\Service\Command\AbstractCommand\HEADERS_OPTION
const HEADERS_OPTION
Definition: AbstractCommand.php:24
Guzzle\Tests\Service\Command\CommandTest\testCanChangeResponseBody
testCanChangeResponseBody()
Definition: CommandTest.php:436
Guzzle\Service\Command\AbstractCommand
Definition: AbstractCommand.php:21
Guzzle\Tests\Service\Command\CommandTest\testGetResultExecutesCommandsWhenNeeded
testGetResultExecutesCommandsWhenNeeded()
Definition: CommandTest.php:63
Guzzle\Tests\Service\Command\CommandTest\testExecute
testExecute()
Definition: CommandTest.php:93
Http
Guzzle\Tests\Service\Command\CommandTest\testValidatorDoesNotUpdateNonDefaultValues
testValidatorDoesNotUpdateNonDefaultValues()
Definition: CommandTest.php:356
Guzzle\Tests\Service\Command\CommandTest\testValidatorUpdatesDefaultValues
testValidatorUpdatesDefaultValues()
Definition: CommandTest.php:365
Guzzle\Tests\Service\Command\CommandTest\testSetClient
testSetClient()
Definition: CommandTest.php:74
Guzzle\Plugin\Mock\MockPlugin
Definition: MockPlugin.php:17
Guzzle\Service\Command\AbstractCommand\RESPONSE_BODY
const RESPONSE_BODY
Definition: AbstractCommand.php:28
Guzzle\Tests\Service\Command\CommandTest\testOnCompleteMustBeCallable
testOnCompleteMustBeCallable()
Definition: CommandTest.php:269
Guzzle\Tests\Service\Command\CommandTest\testProcessResponseIsNotXml
testProcessResponseIsNotXml()
Definition: CommandTest.php:157
Guzzle\Service\Description\SchemaValidator
Definition: SchemaValidator.php:10
Guzzle\Tests\Service\Command\CommandTest\testAllowsValidatorToBeInjected
testAllowsValidatorToBeInjected()
Definition: CommandTest.php:335
Guzzle\Tests\Service\Command\CommandTest\testConvertsJsonResponsesToArray
testConvertsJsonResponsesToArray()
Definition: CommandTest.php:115
Guzzle\Tests\Service\Command\CommandTest\testCreatesDefaultOperation
testCreatesDefaultOperation()
Definition: CommandTest.php:329
Guzzle\Tests\Service\Command
Definition: AbstractCommandTest.php:3
Guzzle
Guzzle\Tests\Service\Command\CommandTest\testConvertsInvalidJsonResponsesToArray
testConvertsInvalidJsonResponsesToArray()
Definition: CommandTest.php:135
Guzzle\Tests\Service\Command\CommandTest\testCommandsAllowsCustomRequestHeaders
testCommandsAllowsCustomRequestHeaders()
Definition: CommandTest.php:190
Guzzle\Tests\Service\Command\CommandTest\testCloneMakesNewRequest
testCloneMakesNewRequest()
Definition: CommandTest.php:232
Guzzle\Tests\GuzzleTestCase\setMockResponse
setMockResponse(Client $client, $paths)
Definition: GuzzleTestCase.php:181
Guzzle\Tests\Service\Command\CommandTest\testHasOnCompleteMethod
testHasOnCompleteMethod()
Definition: CommandTest.php:245
Guzzle\Service\Command\AbstractCommand\DISABLE_VALIDATION
const DISABLE_VALIDATION
Definition: AbstractCommand.php:35
Guzzle\Tests\Service\Command\AbstractCommandTest\getClient
getClient()
Definition: AbstractCommandTest.php:10
Guzzle\Tests\Service\Mock\Command\Sub\Sub
Definition: Sub.php:7