Testing Guzzle request and PDF converter

Issue

This Content is from Stack Overflow. Question asked by Zeyukan Ich’

I’m pretty new into testing things, and I was wondering how I could test such code or even if it’s valuable to test this.

this is what my code looks like :

    public function convert(string $urlToConvert, string $path, array $options = []): void
    {
        try {
            $result = $this->getClient()->post(
                sprintf('%s/%s', $this->config->getEndpoint(), self::GOTENBERG_CHROMIUM_ENDPOINT),
                [
                    'multipart' => [
                        ['name' => 'url', 'contents' => $urlToConvert],
                        ['name' => 'emulatedMediaType', 'contents' => 'screen'],
                        ...$options
                    ]
                ]
            );

            $directory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
            $directory->writeFile($path, $result->getBody());
        } catch (Exception | GuzzleException $e) {
            $this->logger->error($e, ['context' => 'm2-gotenberg']);

            throw new GotenbergConvertException(__('Failed converting PDF'), $e);
        }
    }

The getClient() returns an instance of a GuzzleHttp.

The process is the following :

  • Do a request on an endpoint with the URL
  • Get the response body which is the converted PDF
  • Create a file with the content given by the response

I don’t feel like I could test anything, or a really small amount of the code.

About the lines creating the files, this is the done by the framework I’m using

The only thing I see is to do an integration test, and fetching the endpoint to create a real pdf, then delete it.

Can someone clarify this for me or give me some advices? When using framework I have troubles on what to test to avoid testing the implementation of my code



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?