Open Journal Systems  3.3.0
OperationResponseParserTest.php
1 <?php
2 
4 
16 
22 {
23  public function testHasVisitors()
24  {
25  $p = new OperationResponseParser(new VisitorFlyweight(array()));
26  $visitor = new BodyVisitor();
27  $p->addVisitor('foo', $visitor);
28  $this->assertSame($visitor, $this->readAttribute($p, 'factory')->getResponseVisitor('foo'));
29  }
30 
31  public function testUsesParentParser()
32  {
34  $operation = new Operation();
35  $operation->setServiceDescription(new ServiceDescription());
36  $op = new OperationCommand(array(), $operation);
37  $op->setResponseParser($p)->setClient(new Client());
38  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/xml'), '<F><B>C</B></F>'), true);
39  $this->assertInstanceOf('SimpleXMLElement', $op->execute());
40  }
41 
42  public function testVisitsLocations()
43  {
44  $parser = new OperationResponseParser(new VisitorFlyweight(array()));
45  $parser->addVisitor('statusCode', new StatusCodeVisitor());
46  $parser->addVisitor('reasonPhrase', new ReasonPhraseVisitor());
47  $parser->addVisitor('json', new JsonVisitor());
48  $op = new OperationCommand(array(), $this->getDescription()->getOperation('test'));
49  $op->setResponseParser($parser)->setClient(new Client());
50  $op->prepare()->setResponse(new Response(201), true);
51  $result = $op->execute();
52  $this->assertEquals(201, $result['code']);
53  $this->assertEquals('Created', $result['phrase']);
54  }
55 
57  {
59  $operation = $this->getDescription()->getOperation('test');
60  $op = new OperationCommand(array(), $operation);
61  $op->setResponseParser($parser)->setClient(new Client());
62  $op->prepare()->setResponse(new Response(200, array(
63  'Content-Type' => 'application/json'
64  ), '{"baz":"bar","enigma":"123"}'), true);
65  $result = $op->execute();
66  $this->assertEquals(array(
67  'baz' => 'bar',
68  'enigma' => '123',
69  'code' => 200,
70  'phrase' => 'OK'
71  ), $result->toArray());
72  }
73 
74  public function testSkipsUnkownModels()
75  {
77  $operation = $this->getDescription()->getOperation('test');
78  $operation->setResponseClass('Baz')->setResponseType('model');
79  $op = new OperationCommand(array(), $operation);
80  $op->setResponseParser($parser)->setClient(new Client());
81  $op->prepare()->setResponse(new Response(201), true);
82  $this->assertInstanceOf('Guzzle\Http\Message\Response', $op->execute());
83  }
84 
86  {
88  $operation = $this->getDescription()->getOperation('test');
89  $op = new OperationCommand(array('command.response_processing' => 'native'), $operation);
90  $op->setResponseParser($parser)->setClient(new Client());
91  $op->prepare()->setResponse(new Response(200, array(
92  'Content-Type' => 'application/json'
93  ), '{"baz":"bar","enigma":"123"}'), true);
94  $result = $op->execute();
95  $this->assertInstanceOf('Guzzle\Service\Resource\Model', $result);
96  $this->assertEquals(array(
97  'baz' => 'bar',
98  'enigma' => '123'
99  ), $result->toArray());
100  }
101 
103  {
105  $desc = $this->getDescription();
106  $operation = $desc->getOperation('test');
107  $op = new OperationCommand(array(), $operation);
108  $op->setResponseParser($parser)->setClient(new Client());
109  $op->prepare()->setResponse(new Response(200, array(
110  'Content-Type' => 'application/json'
111  ), '{"baz":"bar","enigma":"123"}'), true);
112  $result = $op->execute();
113  $this->assertSame($result->getStructure(), $desc->getModel('Foo'));
114  }
115 
117  {
119  $description = ServiceDescription::factory(array(
120  'operations' => array('test' => array('responseClass' => 'Foo')),
121  'models' => array(
122  'Foo' => array(
123  'type' => 'object',
124  'properties' => array('baz' => array('location' => 'body'))
125  )
126  )
127  ));
128  $operation = $description->getOperation('test');
129  $op = new OperationCommand(array(), $operation);
130  $op->setResponseParser($parser)->setClient(new Client());
131  $brokenXml = '<broken><><><<xml>>>>>';
132  $op->prepare()->setResponse(new Response(200, array(
133  'Content-Type' => 'application/xml'
134  ), $brokenXml), true);
135  $result = $op->execute();
136  $this->assertEquals(array('baz'), $result->getKeys());
137  $this->assertEquals($brokenXml, (string) $result['baz']);
138  }
139 
141  {
143  $description = ServiceDescription::factory(array(
144  'operations' => array('test' => array('responseClass' => 'Foo')),
145  'models' => array(
146  'Foo' => array(
147  'type' => 'object',
148  'properties' => array(
149  'code' => array('location' => 'statusCode')
150  ),
151  'additionalProperties' => array(
152  'location' => 'json',
153  'type' => 'object',
154  'properties' => array(
155  'a' => array(
156  'type' => 'string',
157  'filters' => 'strtoupper'
158  )
159  )
160  )
161  )
162  )
163  ));
164 
165  $operation = $description->getOperation('test');
166  $op = new OperationCommand(array(), $operation);
167  $op->setResponseParser($parser)->setClient(new Client());
168  $json = '[{"a":"test"},{"a":"baz"}]';
169  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
170  $result = $op->execute()->toArray();
171  $this->assertEquals(array(
172  'code' => 200,
173  array('a' => 'TEST'),
174  array('a' => 'BAZ')
175  ), $result);
176  }
177 
183  {
185  $description = ServiceDescription::factory(array(
186  'operations' => array('test' => array('responseClass' => 'Foo')),
187  'models' => array(
188  'Foo' => array(
189  'type' => 'object',
190  'additionalProperties' => false,
191  'properties' => array(
192  'name' => array(
193  'location' => 'json',
194  'type' => 'string',
195  ),
196  'nested' => array(
197  'location' => 'json',
198  'type' => 'object',
199  'additionalProperties' => false,
200  'properties' => array(
201  'width' => array(
202  'type' => 'integer'
203  )
204  ),
205  ),
206  'code' => array('location' => 'statusCode')
207  ),
208 
209  )
210  )
211  ));
212 
213  $operation = $description->getOperation('test');
214  $op = new OperationCommand(array(), $operation);
215  $op->setResponseParser($parser)->setClient(new Client());
216  $json = '{"name":"test", "volume":2.0, "nested":{"width":10,"bogus":1}}';
217  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
218  $result = $op->execute()->toArray();
219  $this->assertEquals(array(
220  'name' => 'test',
221  'nested' => array(
222  'width' => 10,
223  ),
224  'code' => 200
225  ), $result);
226  }
227 
229  {
231  $description = ServiceDescription::factory(array(
232  'operations' => array('test' => array('responseClass' => 'Guzzle\Tests\Mock\CustomResponseModel'))
233  ));
234  $operation = $description->getOperation('test');
235  $op = new OperationCommand(array(), $operation);
236  $op->setResponseParser($parser)->setClient(new Client());
237  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
238  $result = $op->execute();
239  $this->assertInstanceOf('Guzzle\Tests\Mock\CustomResponseModel', $result);
240  $this->assertSame($op, $result->command);
241  }
242 
248  {
250  $description = ServiceDescription::factory(array(
251  'operations' => array('test' => array('responseClass' => 'Foo\Baz\Bar'))
252  ));
253  $operation = $description->getOperation('test');
254  $op = new OperationCommand(array(), $operation);
255  $op->setResponseParser($parser)->setClient(new Client());
256  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
257  $op->execute();
258  }
259 
265  {
267  $description = ServiceDescription::factory(array(
268  'operations' => array('test' => array('responseClass' => __CLASS__))
269  ));
270  $operation = $description->getOperation('test');
271  $op = new OperationCommand(array(), $operation);
272  $op->setResponseParser($parser)->setClient(new Client());
273  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
274  $op->execute();
275  }
276 
277  protected function getDescription()
278  {
279  return ServiceDescription::factory(array(
280  'operations' => array('test' => array('responseClass' => 'Foo')),
281  'models' => array(
282  'Foo' => array(
283  'type' => 'object',
284  'properties' => array(
285  'baz' => array('type' => 'string', 'location' => 'json'),
286  'code' => array('location' => 'statusCode'),
287  'phrase' => array('location' => 'reasonPhrase'),
288  )
289  )
290  )
291  ));
292  }
293 
295  {
296  $client = new Client();
297  $client->setDescription(ServiceDescription::factory(array(
298  'operations' => array('test' => array('responseClass' => 'FooBazBar'))
299  )));
300  $foo = new \stdClass();
301  $client->getEventDispatcher()->addListener('command.parse_response', function ($e) use ($foo) {
302  $e['result'] = $foo;
303  });
304  $command = $client->getCommand('test');
305  $command->prepare()->setResponse(new Response(200), true);
306  $result = $command->execute();
307  $this->assertSame($result, $foo);
308  }
309 
315  {
317  $description = ServiceDescription::factory(array(
318  'operations' => array('test' => array('responseClass' => 'Foo')),
319  'models' => array(
320  'Baz' => array('type' => 'string'),
321  'Foo' => array(
322  'type' => 'object',
323  'additionalProperties' => array('$ref' => 'Baz', 'location' => 'json')
324  )
325  )
326  ));
327  $operation = $description->getOperation('test');
328  $op = new OperationCommand(array(), $operation);
329  $op->setResponseParser($parser)->setClient(new Client());
330  $json = '{"a":"a","b":"b","c":"c"}';
331  $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
332  $result = $op->execute()->toArray();
333  $this->assertEquals(array('a' => 'a', 'b' => 'b', 'c' => 'c'), $result);
334  }
335 }
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight
Definition: VisitorFlyweight.php:12
Guzzle\Tests\Service\Command\OperationResponseParserTest\testAdditionalPropertiesWithRefAreResolved
testAdditionalPropertiesWithRefAreResolved()
Definition: OperationResponseParserTest.php:314
$op
$op
Definition: lib/pkp/pages/help/index.php:18
Guzzle\Service\Command\LocationVisitor\Response\JsonVisitor
Definition: Response/JsonVisitor.php:17
Guzzle\Tests\Service\Command\OperationResponseParserTest\getDescription
getDescription()
Definition: OperationResponseParserTest.php:277
Guzzle\Service\Description\Operation
Definition: Operation.php:10
Guzzle\Tests\Service\Command\OperationResponseParserTest\testCanAddListenerToParseDomainObjects
testCanAddListenerToParseDomainObjects()
Definition: OperationResponseParserTest.php:294
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Service\Command\OperationResponseParserTest\testSkipsUnkownModels
testSkipsUnkownModels()
Definition: OperationResponseParserTest.php:74
Guzzle\Service\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php:25
Guzzle\Service\Command\LocationVisitor\Response\StatusCodeVisitor
Definition: StatusCodeVisitor.php:12
Guzzle\Tests\Service\Command\OperationResponseParserTest\testCanInjectModelSchemaIntoModels
testCanInjectModelSchemaIntoModels()
Definition: OperationResponseParserTest.php:102
Guzzle\Service\Description\ServiceDescription\factory
static factory($config, array $options=array())
Definition: ServiceDescription.php:65
Guzzle\Service\Command\OperationCommand
Definition: OperationCommand.php:10
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Service\Command\LocationVisitor\VisitorFlyweight\getInstance
static getInstance()
Definition: VisitorFlyweight.php:52
Guzzle\Tests\Service\Command\OperationResponseParserTest\testDoesNotParseXmlWhenNotUsingXmlVisitor
testDoesNotParseXmlWhenNotUsingXmlVisitor()
Definition: OperationResponseParserTest.php:116
Guzzle\Tests\Service\Command\OperationResponseParserTest\testUsesParentParser
testUsesParentParser()
Definition: OperationResponseParserTest.php:31
Guzzle\Service\Description\ServiceDescription
Definition: ServiceDescription.php:11
Guzzle\Service\Command\OperationResponseParser\getInstance
static getInstance()
Definition: OperationResponseParser.php:38
Guzzle\Tests\Service\Command\OperationResponseParserTest\testHasVisitors
testHasVisitors()
Definition: OperationResponseParserTest.php:23
Guzzle\Tests\Service\Command\OperationResponseParserTest\testVisitsLocationsForJsonResponse
testVisitsLocationsForJsonResponse()
Definition: OperationResponseParserTest.php:56
Guzzle\Tests\Service\Command\OperationResponseParserTest\testCreatesCustomResponseClassInterface
testCreatesCustomResponseClassInterface()
Definition: OperationResponseParserTest.php:228
Guzzle\Tests\Service\Command\OperationResponseParserTest
Definition: OperationResponseParserTest.php:21
Guzzle\Tests\Service\Command\OperationResponseParserTest\testAllowsModelProcessingToBeDisabled
testAllowsModelProcessingToBeDisabled()
Definition: OperationResponseParserTest.php:85
Guzzle\Service\Command\OperationResponseParser
Definition: OperationResponseParser.php:17
Guzzle\Tests\Service\Command\OperationResponseParserTest\testEnsuresResponseClassExists
testEnsuresResponseClassExists()
Definition: OperationResponseParserTest.php:247
Guzzle\Service\Command\LocationVisitor\Response\ReasonPhraseVisitor
Definition: ReasonPhraseVisitor.php:12
Guzzle\Tests\Service\Command\OperationResponseParserTest\testAdditionalPropertiesDisabledDiscardsData
testAdditionalPropertiesDisabledDiscardsData()
Definition: OperationResponseParserTest.php:182
Guzzle\Tests\Service\Command\OperationResponseParserTest\testVisitsLocations
testVisitsLocations()
Definition: OperationResponseParserTest.php:42
Guzzle\Tests\Service\Command
Definition: AbstractCommandTest.php:3
Guzzle\Tests\Service\Command\OperationResponseParserTest\testEnsuresResponseClassImplementsResponseClassInterface
testEnsuresResponseClassImplementsResponseClassInterface()
Definition: OperationResponseParserTest.php:264
Guzzle\Tests\Service\Command\OperationResponseParserTest\testVisitsAdditionalProperties
testVisitsAdditionalProperties()
Definition: OperationResponseParserTest.php:140
Guzzle\Service\Command\LocationVisitor\Response\BodyVisitor
Definition: Response/BodyVisitor.php:12