/** * Build a mock response object to inject into the response * * @param array $data Data to set in the response * @return \DuoAuth\Response object */ protected function buildMockResponse($data) { $response = new \DuoAuth\Response(); $r = new MockResponse(); $r->setBody(json_encode($data)); $response->setData($r); return $response; }
/** * Test that, when the object is given on construct, data is * still set correctly. */ public function testSetDataOnConstruct() { $data = json_encode(array('response' => 'test')); $response = new MockResponse(); $response->setBody($data); $r = new \DuoAuth\Response($response); $this->assertEquals('test', $r->getBody()); }
private function buildMockRequest($data) { $mockClient = new MockClient(); $response = new \DuoAuth\Response(); $r = new MockResponse(); $r->setBody(json_encode($data)); $response->setData($r); $request = $this->getMock('\\DuoAuth\\Request', array('send'), array($mockClient)); $request->expects($this->once())->method('send')->will($this->returnValue($response)); return $request; }
/** * Send the request to the API * * @return string|boolean Parsed json if successful, false if not */ public function send() { $path = 'https://' . $this->getHostname() . $this->getPath(); $method = strtolower($this->getMethod()); $client = $this->getClient(); $hash = $this->buildHashHeader(); $params = $this->getParams(); ksort($params); if ($method == 'get') { $path .= '?' . http_build_query($params); } // Guzzle doesn't add the date header, so we put it in manually $request = $client->{$method}($path, array('Date' => date('r')), $params)->setAuth($this->getIntKey(), $hash); $response = new \DuoAuth\Response(); try { $response->setData($request->send()); return $response; } catch (\Exception $e) { \DuoAuth\Error::add($e->getMessage()); $this->setError($e->getMessage()); return $response; } }