Example #1
0
 /**
  * @param $xml
  * @return Response
  */
 public function generateEbaySuccessResponse($xml)
 {
     $mockReponse = new Response(200);
     if (file_exists($xml)) {
         $mockReponse->setBody(EntityBody::factory(file_get_contents($xml)));
     } else {
         $mockReponse->setBody(EntityBody::factory($xml));
     }
     return $mockReponse;
 }
Example #2
0
 public function testRetrieving()
 {
     $response = new Response(200);
     $response->setBody(json_encode(array('status' => 1, 'complete' => 1, 'list' => array(123 => array('item_id' => 123, 'resolved_id' => 123, 'given_url' => 'http://acairns.co.uk', 'given_title' => 'Andrew Cairns', 'favorite' => 0, 'status' => 0, 'time_added' => time(), 'time_updated' => time(), 'time_read' => 0, 'time_favorited' => 0, 'sort_id' => 0, 'resolved_title' => 'Andrew Cairns', 'resolved_url' => 'http://acairns.co.uk', 'excerpt' => 'Some excerpt about something', 'is_article' => 0, 'is_index' => 0, 'has_video' => 0, 'has_image' => 0, 'word_count' => 123)))));
     $this->setPocketResponse($response);
     $response = $this->pockpack->retrieve();
     $this->assertEquals(1, $response->status);
     $this->assertEquals(1, $response->complete);
     $this->assertNotEmpty($response->list);
     $this->assertNotEmpty($response->list->{123});
     $item = $response->list->{123};
     $this->assertEquals(123, $item->item_id);
     $this->assertEquals(123, $item->resolved_id);
     $this->assertEquals('Andrew Cairns', $item->given_title);
     $this->assertEquals('Andrew Cairns', $item->resolved_title);
     $this->assertEquals('http://acairns.co.uk', $item->given_url);
     $this->assertEquals('http://acairns.co.uk', $item->resolved_url);
     $this->assertEquals('Some excerpt about something', $item->excerpt);
     $this->assertEquals(0, $item->is_article);
     $this->assertEquals(0, $item->favorite);
     $this->assertEquals(0, $item->status);
     $this->assertEquals(0, $item->time_read);
     $this->assertEquals(0, $item->time_favorited);
     $this->assertEquals(0, $item->sort_id);
     $this->assertEquals(0, $item->is_index);
     $this->assertEquals(0, $item->has_video);
     $this->assertEquals(0, $item->has_image);
     $this->assertEquals(123, $item->word_count);
 }
 public function dispatch($request, $connection)
 {
     $response = new Response(200);
     $path = $request->getPath();
     if ($this->match($path)) {
         echo "Found match for path: {$path}" . PHP_EOL;
         if (is_callable($this->routes[$path])) {
             call_user_func_array($this->routes[$path], array($request, $response, $connection));
         } else {
             throw new \Exception('Invalid route definition.');
         }
         return;
     }
     if (is_null($this->fileHandler)) {
         throw new \Exception('No file handler configured');
     }
     if (false === $this->fileHandler->canHandleRequest($request)) {
         $response->setStatus(404);
         $response->setBody('File not found');
         $connection->send($response);
         $connection->close();
         return;
     }
     $this->fileHandler->handleRequest($request, $response, $connection);
 }
 public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
 {
     $this->log('open', $conn);
     $response = new Response(200, array('Content-type' => 'text/javascript', 'Content-Length' => $this->getFilesize($request->getPath())));
     $response->setBody($this->getContent($request->getPath()));
     $conn->send((string) $response);
     $conn->close();
 }
Example #5
0
 public function testReceivingAccessTokenAndUsername()
 {
     $response = new Response(200);
     $response->setBody(json_encode(array('access_token' => 'fake_access_token', 'username' => 'acairns')));
     $this->setPocketResponse($response);
     $data = $this->pockpack_auth->receiveTokenAndUsername('fake_consumer_key', 'fake_access_token');
     $this->assertEquals('fake_access_token', $data['access_token']);
     $this->assertEquals('acairns', $data['username']);
 }
Example #6
0
 public function testCanCreateApiRateLimitErrorException()
 {
     $command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
     $request = $this->getMock('Guzzle\\Http\\Message\\Request', [], [], '', false);
     $command->expects($this->once())->method('getRequest')->will($this->returnValue($request));
     $response = new Response(400);
     $response->setBody(json_encode(['error' => ['type' => 'invalid_request_error', 'code' => 'rate_limit']]));
     $exception = BadRequestException::fromCommand($command, $response);
     $this->assertInstanceOf('ZfrStripe\\Exception\\ApiRateLimitException', $exception);
 }
 /** @test */
 public function it_can_create_the_exception()
 {
     $command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
     $command->expects($this->once())->method('getRequest')->will($this->returnValue($this->getMock('Guzzle\\Http\\Message\\Request', [], [], '', false)));
     $response = new Response(400);
     $response->setBody('');
     /** @noinspection PhpParamsInspection */
     $exception = BadRequestException::fromCommand($command, $response);
     $this->assertInstanceOf('Apache\\Usergrid\\Api\\Exception\\BadRequestException', $exception);
 }
 /**
  * Setup
  */
 public function setUp()
 {
     $plugin = new MockPlugin();
     $response = new GuzzleResponse(200);
     $response->setBody(file_get_contents(__DIR__ . '/Fixtures/response_projects.json'));
     $plugin->addResponse($response);
     $client = new VersionEyeClient('foo');
     $client->addSubscriber($plugin);
     $this->object = new VersionEyeApi($client, 'foo');
 }
 public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
 {
     $this->emit('open', array($conn, $request));
     try {
         $this->router->dispatch($request, $conn);
     } catch (Exception $e) {
         $response = new Response(500);
         $response->setBody("Server error.");
         $conn->send($response);
         $conn->close();
     }
 }
Example #10
0
 protected function getClient($fixtureFilePath = null)
 {
     if (null === $fixtureFilePath) {
         return $this->getMock('Guzzle\\Http\\Client');
     }
     $response = new Response(200);
     $response->setBody(file_get_contents($fixtureFilePath));
     $mock = new MockPlugin();
     $mock->addResponse($response);
     $client = new Client();
     $client->addSubscriber($mock);
     return $client;
 }
Example #11
0
 public function setUp()
 {
     $this->fixtureFilePath = __DIR__ . '/fixture';
     $httpClient = $this->getMockBuilder('Guzzle\\Http\\Client')->setMethods(array('send'))->getMock();
     // This client mock returns always a response object which body is a json-encoded array
     // containing the url and the method of the request.
     $httpClient->expects($this->any())->method('send')->will($this->returnCallback(function ($request) {
         $response = new Response(200);
         $response->setBody(json_encode(array('url' => $request->getUrl(), 'method' => $request->getMethod(), 'mime-type' => $request->getHeader('Content-Type', true))));
         return $response;
     }));
     $this->client = new Client('test', 'user', 'pswd', $httpClient);
     $this->client->setApiConfig($this->apiConfig);
 }
Example #12
0
 /**
  * @covers \Wordsmith\Project::all
  */
 public function testGetAllProjects()
 {
     $mockResponse = new Response(200);
     $mockResponseBody = EntityBody::factory(fopen(__DIR__ . '/mock/bodies/projects.txt', 'r+'));
     $mockResponse->setBody($mockResponseBody);
     $mockResponse->setHeaders(array("Host" => "httpbin.org", "User-Agent" => "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3", "Accept" => "application/json", "Content-Type" => "application/json"));
     $plugin = new MockPlugin();
     $plugin->addResponse($mockResponse);
     $client = new HttpClient();
     $client->addSubscriber($plugin);
     $request = $client->get('https://api.automatedinsights.com/v1/projects');
     $response = $request->send();
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertTrue(in_array('Host', array_keys($response->getHeaders()->toArray())));
     $this->assertTrue($response->hasHeader("User-Agent"));
     $this->assertCount(4, $response->getHeaders());
     $this->assertSame($mockResponseBody->getSize(), $response->getBody()->getSize());
     $this->assertSame(1, count(json_decode($response->getBody(true))->data));
 }
 public function testDeserializeContextConfiguration()
 {
     $expectedContext = DeserializationContext::create();
     $expectedContext->setGroups('group');
     $expectedContext->setVersion(1);
     $expectedContext->enableMaxDepthChecks();
     $operation = $this->getMock('Guzzle\\Service\\Description\\OperationInterface');
     $operation->expects($this->any())->method('getResponseType')->will($this->returnValue(OperationInterface::TYPE_CLASS));
     $operation->expects($this->any())->method('getResponseClass')->will($this->returnValue('ResponseClass'));
     $dataMap = array(array('jms_serializer.groups', 'group'), array('jms_serializer.version', 1), array('jms_serializer.max_depth_checks', true));
     $operation->expects($this->any())->method('getData')->will($this->returnValueMap($dataMap));
     $command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
     $command->expects($this->any())->method('getOperation')->will($this->returnValue($operation));
     $response = new Response(200);
     $response->setBody('body');
     $serializer = $this->getMock('JMS\\Serializer\\SerializerInterface');
     $serializer->expects($this->once())->method('deserialize')->with('body', 'ResponseClass', 'json', $this->equalTo($expectedContext));
     $parser = new JMSSerializerResponseParser($serializer, $this->getMock('Guzzle\\Service\\Command\\ResponseParserInterface'));
     $ref = new \ReflectionMethod($parser, 'deserialize');
     $ref->setAccessible(true);
     return $ref->invoke($parser, $command, $response, 'json');
 }
 public function fetch(RequestInterface $request)
 {
     $key = $this->getCacheKey($request);
     if (!($entries = $this->cache->fetch($key))) {
         return null;
     }
     $match = null;
     $headers = $this->persistHeaders($request);
     $entries = unserialize($entries);
     foreach ($entries as $index => $entry) {
         if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
             $match = $entry;
             break;
         }
     }
     if (!$match) {
         return null;
     }
     // Ensure that the response is not expired
     $response = null;
     if ($match[4] < time()) {
         $response = -1;
     } else {
         $response = new Response($match[2], $match[1]);
         if ($match[3]) {
             if ($body = $this->cache->fetch($match[3])) {
                 $response->setBody($body);
             } else {
                 // The response is not valid because the body was somehow deleted
                 $response = -1;
             }
         }
     }
     if ($response === -1) {
         // Remove the entry from the metadata and update the cache
         unset($entries[$index]);
         if ($entries) {
             $this->cache->save($key, serialize($entries));
         } else {
             $this->cache->delete($key);
         }
         return null;
     }
     return $response;
 }
 /**
  * @return MockPlugin
  */
 private function createGuzzleMockPlugin()
 {
     $terytPageFixturesPath = $this->fixturesPath . '/TerytPage';
     $mock = new MockPlugin();
     $downloadPageResponse = new Response(200);
     $downloadPageResponse->setBody(file_get_contents($terytPageFixturesPath . DIRECTORY_SEPARATOR . 'listTerytFiles.html'));
     $mock->addResponse($downloadPageResponse);
     return $mock;
 }
 /**
  * @param string $filename
  * @param int $httpStatusCode
  * @return \Guzzle\Http\Message\Response
  */
 protected function getFixtureResponse($filename, $httpStatusCode = 200)
 {
     $response = new GuzzleResponse($httpStatusCode);
     $response->setBody(file_get_contents(__DIR__ . '/Fixtures/' . $filename));
     return $response;
 }
Example #17
0
 /**
  * We need to set the body again after deserialization because it was a stream that didn't get serialized
  */
 public function __wakeup()
 {
     $this->response->setBody($this->body);
 }
Example #18
0
 public function fetch(RequestInterface $request)
 {
     $key = $this->getCacheKey($request);
     if (!($entries = $this->cache->fetch($key))) {
         return null;
     }
     $match = null;
     $headers = $this->persistHeaders($request);
     $entries = unserialize($entries);
     foreach ($entries as $index => $entry) {
         if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
             $match = $entry;
             break;
         }
     }
     if (!$match) {
         return null;
     }
     $response = null;
     if ($match[4] < time()) {
         $response = -1;
     } else {
         $response = new Response($match[2], $match[1]);
         if ($match[3]) {
             if ($body = $this->cache->fetch($match[3])) {
                 $response->setBody($body);
             } else {
                 $response = -1;
             }
         }
     }
     if ($response === -1) {
         unset($entries[$index]);
         if ($entries) {
             $this->cache->save($key, serialize($entries));
         } else {
             $this->cache->delete($key);
         }
         return null;
     }
     return $response;
 }
Example #19
0
 /**
  * @covers Guzzle\Http\Message\Response::getBody
  * @covers Guzzle\Http\Message\Response::setBody
  */
 public function testGetBody()
 {
     $body = EntityBody::factory('');
     $response = new Response(403, new Collection(), $body);
     $this->assertEquals($body, $response->getBody());
     $response->setBody('foo');
     $this->assertEquals('foo', $response->getBody(true));
 }
 function testGetOrderResponse()
 {
     $json = file_get_contents(__DIR__ . '/' . $this->fixturesFolder . '/get-campaigns-id-orders-id.json');
     $jsonObj = json_decode($json);
     $response = new Response(200);
     $response->setBody($json);
     $marketPartnerMock = $this->getMock('Yandex\\Market\\Partner\\PartnerClient', array('sendRequest'));
     $marketPartnerMock->expects($this->any())->method('sendRequest')->will($this->returnValue($response));
     /** @var Order $order */
     $order = $marketPartnerMock->getOrder('124');
     $this->assertEquals($jsonObj->order->id, $order->getId());
     $this->assertEquals($jsonObj->order->status, $order->getStatus());
     $this->assertEquals($jsonObj->order->creationDate, $order->getCreationDate());
     $this->assertEquals($jsonObj->order->currency, $order->getCurrency());
     $this->assertEquals($jsonObj->order->itemsTotal, $order->getItemsTotal());
     $this->assertEquals($jsonObj->order->total, $order->getTotal());
     $this->assertEquals($jsonObj->order->paymentType, $order->getPaymentType());
     $this->assertEquals($jsonObj->order->paymentMethod, $order->getPaymentMethod());
     $this->assertEquals($jsonObj->order->fake, $order->getFake());
     //items
     $items = $order->getItems();
     /** @var Item $item0 */
     $item0 = $items->current();
     $this->assertEquals($jsonObj->order->items[0]->feedId, $item0->getFeedId());
     $this->assertEquals($jsonObj->order->items[0]->offerId, $item0->getOfferId());
     $this->assertEquals($jsonObj->order->items[0]->feedCategoryId, $item0->getFeedCategoryId());
     $this->assertEquals($jsonObj->order->items[0]->offerName, $item0->getOfferName());
     $this->assertEquals($jsonObj->order->items[0]->price, $item0->getPrice());
     $this->assertEquals($jsonObj->order->items[0]->count, $item0->getCount());
     /** @var Item $item1 */
     $item1 = $items->next();
     $this->assertEquals($jsonObj->order->items[1]->feedId, $item1->getFeedId());
     $this->assertEquals($jsonObj->order->items[1]->offerId, $item1->getOfferId());
     $this->assertEquals($jsonObj->order->items[1]->feedCategoryId, $item1->getFeedCategoryId());
     $this->assertEquals($jsonObj->order->items[1]->offerName, $item1->getOfferName());
     $this->assertEquals($jsonObj->order->items[1]->price, $item1->getPrice());
     $this->assertEquals($jsonObj->order->items[1]->count, $item1->getCount());
     //delivery
     $this->assertEquals($jsonObj->order->delivery->type, $order->getDelivery()->getType());
     $this->assertEquals($jsonObj->order->delivery->serviceName, $order->getDelivery()->getServiceName());
     $this->assertEquals($jsonObj->order->delivery->price, $order->getDelivery()->getPrice());
     //delivery->region
     $this->assertEquals($jsonObj->order->delivery->region->id, $order->getDelivery()->getRegion()->getId());
     $this->assertEquals($jsonObj->order->delivery->region->name, $order->getDelivery()->getRegion()->getName());
     $this->assertEquals($jsonObj->order->delivery->region->type, $order->getDelivery()->getRegion()->getType());
     $this->assertEquals($jsonObj->order->delivery->region->parent->id, $order->getDelivery()->getRegion()->getParent()->getId());
     $this->assertEquals($jsonObj->order->delivery->region->parent->name, $order->getDelivery()->getRegion()->getParent()->getName());
     $this->assertEquals($jsonObj->order->delivery->region->parent->type, $order->getDelivery()->getRegion()->getParent()->getType());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->id, $order->getDelivery()->getRegion()->getParent()->getParent()->getId());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->name, $order->getDelivery()->getRegion()->getParent()->getParent()->getName());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->type, $order->getDelivery()->getRegion()->getParent()->getParent()->getType());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->parent->id, $order->getDelivery()->getRegion()->getParent()->getParent()->getParent()->getId());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->parent->name, $order->getDelivery()->getRegion()->getParent()->getParent()->getParent()->getName());
     $this->assertEquals($jsonObj->order->delivery->region->parent->parent->parent->type, $order->getDelivery()->getRegion()->getParent()->getParent()->getParent()->getType());
     //delivery->address
     $this->assertEquals($jsonObj->order->delivery->address->country, $order->getDelivery()->getAddress()->getCountry());
     $this->assertEquals($jsonObj->order->delivery->address->postcode, $order->getDelivery()->getAddress()->getPostcode());
     $this->assertEquals($jsonObj->order->delivery->address->city, $order->getDelivery()->getAddress()->getCity());
     $this->assertEquals($jsonObj->order->delivery->address->subway, $order->getDelivery()->getAddress()->getSubway());
     $this->assertEquals($jsonObj->order->delivery->address->street, $order->getDelivery()->getAddress()->getStreet());
     $this->assertEquals($jsonObj->order->delivery->address->house, $order->getDelivery()->getAddress()->getHouse());
     $this->assertEquals($jsonObj->order->delivery->address->entrance, $order->getDelivery()->getAddress()->getEntrance());
     $this->assertEquals($jsonObj->order->delivery->address->entryphone, $order->getDelivery()->getAddress()->getEntryphone());
     $this->assertEquals($jsonObj->order->delivery->address->floor, $order->getDelivery()->getAddress()->getFloor());
     $this->assertEquals($jsonObj->order->delivery->address->apartment, $order->getDelivery()->getAddress()->getApartment());
     $this->assertEquals($jsonObj->order->delivery->address->recipient, $order->getDelivery()->getAddress()->getRecipient());
     $this->assertEquals($jsonObj->order->delivery->address->phone, $order->getDelivery()->getAddress()->getPhone());
     //buyer
     $this->assertEquals($jsonObj->order->buyer->id, $order->getBuyer()->getId());
     $this->assertEquals($jsonObj->order->buyer->lastName, $order->getBuyer()->getLastName());
     $this->assertEquals($jsonObj->order->buyer->firstName, $order->getBuyer()->getFirstName());
     $this->assertEquals($jsonObj->order->buyer->middleName, $order->getBuyer()->getMiddleName());
     $this->assertEquals($jsonObj->order->buyer->phone, $order->getBuyer()->getPhone());
     $this->assertEquals($jsonObj->order->buyer->email, $order->getBuyer()->getEmail());
 }