Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream PDF file #243

Closed
cierzniak opened this issue Feb 20, 2018 · 0 comments
Closed

Stream PDF file #243

cierzniak opened this issue Feb 20, 2018 · 0 comments

Comments

@cierzniak
Copy link

cierzniak commented Feb 20, 2018

Hi,
I can't handle this for two days. I need help! If it's related to #230, does it could be merged?

I have Guzzle request with stream => true option to fetch PDF file from external API. Request via browser or Postman works with success, tests in PHPUnit works, but tests in PHPUnit with PHP-VCR fails.

I still have array_shift() expects parameter 1 to be array, null given at https://github.com/guzzle/guzzle/blob/master/src/Handler/StreamHandler.php#L100

Can you point me where is the trouble?

tests/WebTestCase.php

    protected function setUp(): void
    {
        parent::setUp();
        $this->runCommand('doctrine:migrations:migrate --no-interaction');
        VCR::configure()->enableLibraryHooks(['curl'])->setStorage('yaml');
        VCR::getEventDispatcher()->addListener(VCREvents::VCR_BEFORE_HTTP_REQUEST, [$this, 'setCustomHeaders']);
        VCR::getEventDispatcher()->addListener(VCREvents::VCR_BEFORE_PLAYBACK, [$this, 'setCustomHeaders']);
        $this->client = $this->createClient();
    }

    public function setCustomHeaders(Event $event, $eventName): void
    {
         // It's needed to be agnostic of PHP, Guzzle and PHPUnit version
        /** @var Request $request */
        $request = $event->getRequest();
        $request->setHeader('User-Agent', 'VCR_user_agent');
    }

    protected function setXAuthorizationToken(string $token): void
    {
        $this->client->getCookieJar()->set(new Cookie('X-Authorization', $token));
    }

    protected function doRequest(string $method, string $url, array $payload = []): void
    {
        $this->client->request($method, $url, [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($payload));
    }

    protected function getResponse(): \stdClass
    {
        return json_decode($this->client->getResponse()->getContent());
    }

    protected function getStatusCode(): int
    {
        return $this->client->getResponse()->getStatusCode();
    }

tests/DownloadPdfTest.php

    /**
     * @test
     * @vcr DownloadPdf.yaml
     */
    public function returnsStreamWithEbookToDownload(): void
    {
        $this->setXAuthorizationToken('2a37d25d5b54aed80cf89029d7ea03710b526f9c');
        $this->doRequest('GET', '/api/v1/my_accesses/ebook/download/5');

        dump($this->getResponse()->error->exception[0]->message);

        $this->assertSame(Response::HTTP_OK, $this->getStatusCode());
        $this->assertSame('', $this->getResponse());
    }

guzzle action

    private function fetchData(string $url, array $queryParams = [], array $headers = []): array
    {
        $response = $this->guzzle->request('GET', $url, ['stream' => true, 'query' => $queryParams]);
        if ($response->getStatusCode() === Response::HTTP_OK) {
            return [
                'body' => $response->getBody(),
                'headers' => array_reduce($headers, function ($result, $header) use ($response) {
                    $result[$header] = $response->getHeader($header);

                    return $result;
                }, ['Content-Transfer-Encoding' => 'binary']),
            ];
        }
        $content = json_decode($response->getBody()->getContents(), true);

        throw new ApiException(
            $content['errors']['message'],
            $content['errors']['code'] ?? $response->getStatusCode()
        );
    }
@cierzniak cierzniak closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant