/** * @test */ public function shouldValidateRequestTokenWithLogin() { $repository = $this->getRepositoryWithMockedHttpAdapter(); $response = new Response(200); $response->setBody(json_encode(['success' => true, 'request_token' => 'abcdefghijklmnopqrstuvwxyz'])); $this->getAdapter()->expects($this->any())->method('get')->with($this->getRequest('authentication/token/validate_with_login', ['request_token' => 'request_token', 'username' => 'piet', 'password' => 'henk']))->will($this->returnValue($response)); $repository->validateRequestTokenWithLogin(new RequestToken('request_token'), 'piet', 'henk'); }
/** * @test * @expectedException \InvalidArgumentException */ public function shouldThrowExceptionWhenNotValidated() { $api = $this->getApiWithMockedHttpAdapter(); $response = new Response(200); $response->setBody(json_encode(['success' => false])); $this->getAdapter()->expects($this->any())->method('get')->with($this->getRequest('authentication/token/validate_with_login', ['request_token' => 'request_token', 'username' => 'piet', 'password' => 'henk']))->will($this->returnValue($response)); $api->getSessionTokenWithLogin(new RequestToken('request_token'), 'piet', 'henk'); }
/** * Create the response object * * @param ResponseInterface $adapterResponse * @return \Tmdb\HttpClient\Response */ private function createResponse(ResponseInterface $adapterResponse = null) { $response = new Response(); if ($adapterResponse !== null) { $response->setCode($adapterResponse->getStatusCode()); $response->setHeaders(new ParameterBag($adapterResponse->getHeaders())); $response->setBody((string) $adapterResponse->getBody()); } return $response; }
/** * Create the request object and send it out to listening events. * * @param $path * @param $method * @param array $parameters * @param array $headers * @param null $body * @return string */ private function send($path, $method, array $parameters = [], array $headers = [], $body = null) { $request = $this->createRequest($path, $method, $parameters, $headers, $body); $event = new RequestEvent($request); $this->eventDispatcher->dispatch(TmdbEvents::REQUEST, $event); $this->lastResponse = $event->getResponse(); if ($this->lastResponse instanceof Response) { return (string) $this->lastResponse->getBody(); } return []; }
/** * @test */ public function doesSetGetHeaders() { $response = new Response(); $response->setHeaders(new ParameterBag(['X-Test' => '123'])); $this->assertEquals('123', $response->getHeaders()->get('X-Test')); }
/** * Create the unified exception to throw * * @param Request $request * @param Response $response * @return TmdbApiException */ protected function createApiException(Request $request, Response $response) { $errors = json_decode((string) $response->getBody()); return new TmdbApiException($errors->status_code, $errors->status_message, $request, $response); }