/** * Make sure that the request sent and retrieved data is correct. * * @return void */ public function testFetch() { $json = <<<JSON { "capture_id": "1002", "updated": "from json" } JSON; $this->mock->addResponse(new Response(200, ['Content-Type' => 'application/json'], Stream::factory($json))); $capture = new Capture($this->connector, '/path', '1002'); $capture['updated'] = 'not from json'; $capture->fetch(); $this->assertEquals('from json', $capture['updated']); $this->assertEquals('1002', $capture->getId()); $request = $this->history->getLastRequest(); $this->assertEquals('GET', $request->getMethod()); $this->assertEquals('/path/captures/1002', $request->getPath()); $this->assertAuthorization($request); }
/** * Fetches the specified capture. * * @param string $captureId Capture ID * * @see Capture::fetch() For more information on how to fetch a capture * * @throws ConnectorException When the API replies with an error response * @throws RequestException When an error is encountered * @throws \RuntimeException On an unexpected API response * @throws \RuntimeException If the response content type is not JSON * @throws \InvalidArgumentException If the JSON cannot be parsed * @throws \LogicException When Guzzle cannot populate the response * * @return Capture */ public function fetchCapture($captureId) { if ($this->offsetExists('captures')) { foreach ($this['captures'] as $capture) { if ($capture->getId() !== $captureId) { continue; } return $capture->fetch(); } } $capture = new Capture($this->connector, $this->getLocation(), $captureId); $capture->fetch(); $this['captures'][] = $capture; return $capture; }
/** * Make sure a non-JSON response results in an exception. * * @return void */ public function testFetchNotJson() { $this->connector->expects($this->once())->method('createRequest')->with('/orders/1/captures/2', 'GET', [])->will($this->returnValue($this->request)); $this->connector->expects($this->once())->method('send')->with($this->request)->will($this->returnValue($this->response)); $this->response->expects($this->once())->method('getStatusCode')->will($this->returnValue('200')); $this->response->expects($this->once())->method('hasHeader')->with('Content-Type')->will($this->returnValue(true)); $this->response->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('text/plain')); $capture = new Capture($this->connector, '/orders/1', '2'); $capture['data'] = 'is overwritten'; $this->setExpectedException('RuntimeException', 'Unexpected Content-Type header received: text/plain'); $capture->fetch(); }