public function testNonJsonResponse() { $responseBody = new Stream(fopen('php://memory', 'wb+')); $responseBody->write('image-content'); $response = new Response(200, ['Content-Type' => 'image/jpeg'], $responseBody); $this->mockHandler->append($response); $response = $this->client->execute(['resource' => 'user', 'id' => 1337, 'format' => 'image']); $this->assertEquals('image-content', $response); }
/** * Build a message stream from a string. * * @param string $message * * @return Stream */ protected static function buildMessageStream($message) { $messageHandle = fopen('php://memory', 'r+'); $messageStream = new Stream($messageHandle); $messageStream->write($message); $messageStream->rewind(); return $messageStream; }
/** * @param string $file * @return \Psr\Http\Message\ResponseInterface */ protected function getHttpResponseFromFile($file) { $content = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $file); $data = json_decode($content); $body = new Stream(fopen('php://memory', 'wb+')); $body->write($data->body); $response = new Response($data->statusCode, (array) $data->headers, $body); return $response; }
public function testCanDetachStream() { $r = fopen('php://temp', 'w+'); $stream = new Stream($r); $stream->write('foo'); $this->assertTrue($stream->isReadable()); $this->assertSame($r, $stream->detach()); $stream->detach(); $this->assertFalse($stream->isReadable()); $this->assertFalse($stream->isWritable()); $this->assertFalse($stream->isSeekable()); $self = $this; $throws = function ($fn) use($stream, $self) { try { $fn($stream); $self->fail(); } catch (\Exception $e) { } }; $throws(function ($stream) { $stream->read(10); }); $throws(function ($stream) { $stream->write('bar'); }); $throws(function ($stream) { $stream->seek(10); }); $throws(function ($stream) { $stream->tell(); }); $throws(function ($stream) { $stream->eof(); }); $throws(function ($stream) { $stream->getSize(); }); $throws(function ($stream) { $stream->getContents(); }); $this->assertSame('', (string) $stream); $stream->close(); }
use JimLind\Pie7o\Tweet; use JimLind\Pie7o\Tweeter; /* * Configure all the things */ $settingList = ['accessToken' => 'YOUR ACCESS TOKEN', 'accessTokenSecret' => 'YOUR ACCESS TOKEN SECRET', 'consumerKey' => 'YOUR CONSUMER KEY', 'consumerSecret' => 'YOUR CONSUMER SECRET']; $authorizationBuilder = new AuthorizationBuilder($settingList); $guzzleClient = new Client(); $statusUpdater = new StatusUpdater($authorizationBuilder, $guzzleClient); $mediaUploader = new MediaUploader($authorizationBuilder, $guzzleClient); $tweeter = new Tweeter($statusUpdater, $mediaUploader); /* * Create a Tweet */ $messageHandle = fopen('php://temp', 'r+'); $messageStream = new Stream($messageHandle); $messageStream->write('This is a pictures of cats.'); $messageStream->rewind(); $mediaHandle = fopen('./cat.jpg', 'r'); $mediaStream = new Stream($mediaHandle); $tweet = (new Tweet())->withMessage($messageStream)->withMedia($mediaStream); /* * Tweet and catch exceptions */ try { $tweeter->tweet($tweet); echo 'Tweeting was successful.' . PHP_EOL; } catch (Pie7oException $exception) { echo 'Tweeting failed.' . PHP_EOL; echo 'Exception thrown: `' . $exception->getMessage() . '`' . PHP_EOL; }
/** * Add parameters to request based on HTTP method * * @param RequestInterface $request * @param array $params * @return \Psr\Http\Message\RequestInterface */ private function applyParameters(RequestInterface $request, array $params) { if (empty($params)) { return $request; } ksort($params); $urlEncodedParams = http_build_query($params); if ($request->getMethod() == 'GET') { $uri = $request->getUri()->withQuery($urlEncodedParams); $request = $request->withUri($uri); } else { $stream = fopen('php://memory', 'wb+'); $body = new Stream($stream); $body->write($urlEncodedParams); if (array_key_exists('data', $params) && !empty($params['data'])) { ftruncate($stream, 0); $body->write(json_encode($params['data'])); unset($params['data']); $request = $request->withUri($request->getUri()->withQuery(http_build_query($params)))->withHeader('Content-Type', 'application/json'); } $request = $request->withBody($body); } return $request; }