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); }
public function testAllowsBoundedSeek() { $this->body->seek(100); $this->assertEquals(10, $this->body->tell()); $this->assertEquals(13, $this->decorated->tell()); $this->body->seek(0); $this->assertEquals(0, $this->body->tell()); $this->assertEquals(3, $this->decorated->tell()); try { $this->body->seek(-10); $this->fail(); } catch (\RuntimeException $e) { } $this->assertEquals(0, $this->body->tell()); $this->assertEquals(3, $this->decorated->tell()); $this->body->seek(5); $this->assertEquals(5, $this->body->tell()); $this->assertEquals(8, $this->decorated->tell()); // Fail try { $this->body->seek(1000, SEEK_END); $this->fail(); } catch (\RuntimeException $e) { } }
/** * @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; }
/** * Build a media stream from a filepath. * * @param string $mediaPath * * @return Stream * * @throws Pie7oException */ protected static function buildMediaStream($mediaPath) { if (false === file_exists($mediaPath)) { throw new Pie7oException('File Does Not Exist: `' . $mediaPath . '`'); } $mediaHandle = fopen($mediaPath, 'r'); $mediaStream = new Stream($mediaHandle); $mediaStream->rewind(); return $mediaStream; }
protected static function extractOriginalExceptionMessage(ClientException $exception) { self::$response = $exception->getResponse(); self::$stream = self::$response->getBody(); self::$content = self::$stream->getContents(); if (!self::$content) { throw new InstagramServerException($exception->getMessage(), $exception->getCode(), $exception); } return json_decode(self::$content); }
/** * Constructs a StreamStub object. */ public function __construct($file = NULL, $raw = FALSE) { if ($file && $raw) { $handle = fopen('php://temp', 'w+'); fwrite($handle, $file); return parent::__construct($handle, strlen($file)); } if (!$file) { $file = 'php://temp'; } parent::__construct(fopen($file, 'r+'), NULL); }
public function testCanSeekToReadBytesWithPartialBodyReturned() { $stream = fopen('php://temp', 'r+'); fwrite($stream, 'testing'); fseek($stream, 0); $this->decorated = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Stream')->setConstructorArgs([$stream])->setMethods(['read'])->getMock(); $this->decorated->expects($this->exactly(2))->method('read')->willReturnCallback(function ($length) use($stream) { return fread($stream, 2); }); $this->body = new CachingStream($this->decorated); $this->assertEquals(0, $this->body->tell()); $this->body->seek(4, SEEK_SET); $this->assertEquals(4, $this->body->tell()); $this->body->seek(0); $this->assertEquals('test', $this->body->read(4)); }
/** * Write the given image * @param string $filename * @param Stream $image */ private function writeImage($filename, Stream $image) { $filename = $this->getDestinationPath($filename); $resource = $image->detach(); $config = ['visibility' => 'public', 'mimetype' => Mimetypes::getInstance()->fromFilename($filename)]; if ($this->fileExists($filename)) { return $this->filesystem->disk($this->getConfiguredFilesystem())->updateStream($filename, $resource, $config); } $this->filesystem->disk($this->getConfiguredFilesystem())->writeStream($filename, $resource, $config); }
public function testCloseClearProperties() { $handle = fopen('php://temp', 'r+'); $stream = new Stream($handle); $stream->close(); $this->assertFalse($stream->isSeekable()); $this->assertFalse($stream->isReadable()); $this->assertFalse($stream->isWritable()); $this->assertNull($stream->getSize()); $this->assertEmpty($stream->getMetadata()); }
public function __construct($cmd, $mode = 'r') { parent::__construct(popen($cmd, $mode)); }
/** * Get a File's Content. * * @param mixed $file File Location or Resource * * @return string File Contents */ protected function getFileContents($file) { if (!is_resource($file)) { $file = fopen($file, 'r'); } $stream = new Stream($file); $output = $stream->getContents(); $stream->close(); return $output; }
/** * Scopes: role_owner, role_admin * * @param string|resource $file * @return \Aikidesk\SDK\Instance\Contracts\ResponseInterface * @throws \Aikidesk\SDK\Instance\Exceptions\SDKValidationException */ public function logo($file) { $input = []; if (is_string($file)) { $resource = fopen($file, 'r'); $psr7Stream = new Stream($resource); } elseif (is_resource($file) and (get_resource_type($file) == 'file' or get_resource_type($file) == 'stream')) { $resource = $file; $psr7Stream = new Stream($resource); } else { throw new SDKValidationException('Logo is not file path nor file resource'); } $fileContent = $psr7Stream->getContents(); $fileType = pathinfo($psr7Stream->getMetadata('uri'), PATHINFO_EXTENSION); $fileName = pathinfo($psr7Stream->getMetadata('uri'), PATHINFO_FILENAME); $input['logo_img_s3key'] = 'data:image/' . $fileType . ';base64,' . base64_encode($fileContent) . ',' . $fileName; return $this->request->post('setting/theme', $input); }
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; }
public function __toString() { $this->toStringCalled = true; return parent::__toString(); }
/** * Get the size of the file * * @return int */ public function getSize() { return $this->stream->getSize(); }