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

[UPDATE] Better tests #52

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ parameters:
- 'src'
- 'tests'
ignoreErrors:
-
message: "#^Cannot use array destructuring on array\\<int, string\\|null\\>\\|null\\.$#"
count: 1
path: src/DataCollector/GotenbergDataCollector.php
-
message: "#^Method Sensiolabs\\\\GotenbergBundle\\\\Tests\\\\Kernel\\:\\:configureContainer\\(\\) is unused\\.$#"
count: 1
Expand Down
14 changes: 7 additions & 7 deletions src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sensiolabs\GotenbergBundle\Builder\Pdf;

use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Enum\PaperSize;
use Sensiolabs\GotenbergBundle\Enum\EmulatedMediaType;
use Sensiolabs\GotenbergBundle\Enum\PaperSizeInterface;
use Sensiolabs\GotenbergBundle\Enum\Part;
use Sensiolabs\GotenbergBundle\Enum\PdfFormat;
Expand Down Expand Up @@ -315,9 +315,9 @@ public function waitForExpression(string $expression): static
*
* @see https://gotenberg.dev/docs/routes#console-exceptions
*/
public function emulatedMediaType(string $mediaType): static
public function emulatedMediaType(EmulatedMediaType $mediaType): static
{
$this->formFields['emulatedMediaType'] = $mediaType;
$this->formFields['emulatedMediaType'] = $mediaType->value;

return $this;
}
Expand Down Expand Up @@ -526,7 +526,7 @@ protected function withRenderedPart(Part $pdfPart, string $template, array $cont
private function addConfiguration(string $configurationName, mixed $value): void
{
$splitAndParseStringWithUnit = static function (mixed $raw, callable $callback): void {
[$value, $unit] = sscanf((string) $raw, '%d%s') ?? throw new \InvalidArgumentException(sprintf('Unexpected value "%s", expected format is "%%d%%s"', $raw));
[$value, $unit] = sscanf((string) $raw, '%f%s') ?? throw new \InvalidArgumentException(sprintf('Unexpected value "%s", expected format is "%%d%%s"', $raw));
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved

$callback((float) $value, Unit::tryFrom((string) $unit) ?? Unit::Inches);
};
Expand All @@ -535,8 +535,8 @@ private function addConfiguration(string $configurationName, mixed $value): void
'single_page' => $this->singlePage($value),
'pdf_format' => $this->pdfFormat(PdfFormat::from($value)),
'pdf_universal_access' => $this->pdfUniversalAccess($value),
'paper_width' => $this->paperWidth($value),
'paper_height' => $this->paperHeight($value),
'paper_width' => $splitAndParseStringWithUnit($value, $this->paperWidth(...)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a Util/UnitParser with static methods. We could also test it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems a bit much for just one usecase ? It is indirectly tested already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are units used in with LibreOffice? If so, this util could be useful.

If not, let's keep it that way!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with tests

'paper_height' => $splitAndParseStringWithUnit($value, $this->paperHeight(...)),
'margin_top' => $splitAndParseStringWithUnit($value, $this->marginTop(...)),
'margin_bottom' => $splitAndParseStringWithUnit($value, $this->marginBottom(...)),
'margin_left' => $splitAndParseStringWithUnit($value, $this->marginLeft(...)),
Expand All @@ -549,7 +549,7 @@ private function addConfiguration(string $configurationName, mixed $value): void
'native_page_ranges' => $this->nativePageRanges($value),
'wait_delay' => $this->waitDelay($value),
'wait_for_expression' => $this->waitForExpression($value),
'emulated_media_type' => $this->emulatedMediaType($value),
'emulated_media_type' => $this->emulatedMediaType(EmulatedMediaType::from($value)),
'cookies' => $this->cookies($value),
'extra_http_headers' => $this->extraHttpHeaders($value),
'fail_on_http_status_codes' => $this->failOnHttpStatusCodes($value),
Expand Down
14 changes: 12 additions & 2 deletions src/Builder/Pdf/AbstractPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
return $this->encodeData('failOnHttpStatusCodes', $value);
},
'cookies' => function (mixed $value): array {
return $this->encodeData('cookies', \array_values($value));
return $this->encodeData('cookies', array_values($value));
},
'metadata' => function (mixed $value): array {
return $this->encodeData('metadata', $value);
Expand Down Expand Up @@ -164,12 +164,22 @@ private function addToMultipart(string $key, array|string|int|float|bool|DataPar
]];
}

if (\is_int($value) || \is_float($value)) {
if (\is_int($value)) {
return [[
$key => (string) $value,
]];
}

if (\is_float($value)) {
[$left, $right] = sscanf((string) $value, '%d.%s');
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved

$right ??= '0';

return [[
$key => "{$left}.{$right}",
]];
}

if (\is_array($value)) {
$result = [];
foreach ($value as $nestedValue) {
Expand Down
11 changes: 5 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use function array_map;

class Configuration implements ConfigurationInterface
{
Expand Down Expand Up @@ -205,7 +204,7 @@ private function addChromiumPdfOptionsNode(NodeBuilder $treeBuilder): void
->end()
->enumNode('emulated_media_type')
->info('The media type to emulate, either "screen" or "print" - default "print". https://gotenberg.dev/docs/routes#emulated-media-type')
->values(array_map(static fn(EmulatedMediaType $case): string => $case->value, EmulatedMediaType::cases()))
->values(\array_map(static fn (EmulatedMediaType $case): string => $case->value, EmulatedMediaType::cases()))
->defaultNull()
->end()
->arrayNode('cookies')
Expand Down Expand Up @@ -274,7 +273,7 @@ private function addChromiumPdfOptionsNode(NodeBuilder $treeBuilder): void
->end()
->enumNode('pdf_format')
->info('Convert the resulting PDF into the given PDF/A format - default None. https://gotenberg.dev/docs/routes#pdfa-chromium')
->values(array_map(static fn(PdfFormat $case): string => $case->value, PdfFormat::cases()))
->values(\array_map(static fn (PdfFormat $case): string => $case->value, PdfFormat::cases()))
->defaultNull()
->end()
->booleanNode('pdf_universal_access')
Expand Down Expand Up @@ -302,7 +301,7 @@ private function addChromiumScreenshotOptionsNode(NodeBuilder $treeBuilder): voi
->end()
->enumNode('format')
->info('The image compression format, either "png", "jpeg" or "webp" - default png. https://gotenberg.dev/docs/routes#screenshots-route')
->values(array_map(static fn(ScreenshotFormat $case): string => $case->value, ScreenshotFormat::cases()))
->values(\array_map(static fn (ScreenshotFormat $case): string => $case->value, ScreenshotFormat::cases()))
->defaultNull()
->end()
->integerNode('quality')
Expand Down Expand Up @@ -341,7 +340,7 @@ private function addChromiumScreenshotOptionsNode(NodeBuilder $treeBuilder): voi
->end()
->enumNode('emulated_media_type')
->info('The media type to emulate, either "screen" or "print" - default "print". https://gotenberg.dev/docs/routes#emulated-media-type')
->values(array_map(static fn(EmulatedMediaType $case): string => $case->value, EmulatedMediaType::cases()))
->values(\array_map(static fn (EmulatedMediaType $case): string => $case->value, EmulatedMediaType::cases()))
->defaultNull()
->end()
->arrayNode('cookies')
Expand Down Expand Up @@ -438,7 +437,7 @@ private function addPdfOfficeNode(): NodeDefinition
->end()
->enumNode('pdf_format')
->info('Convert the resulting PDF into the given PDF/A format - default None. https://gotenberg.dev/docs/routes#pdfa-chromium')
->values(array_map(static fn(PdfFormat $case): string => $case->value, PdfFormat::cases()))
->values(\array_map(static fn (PdfFormat $case): string => $case->value, PdfFormat::cases()))
->defaultNull()
->end()
->booleanNode('pdf_universal_access')
Expand Down
34 changes: 33 additions & 1 deletion tests/Builder/AbstractBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Sensiolabs\GotenbergBundle\Tests\Builder;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mime\Part\DataPart;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

Expand All @@ -12,8 +17,35 @@ abstract class AbstractBuilderTestCase extends TestCase

protected static Environment $twig;

/**
* @var MockObject&GotenbergClientInterface
*/
protected static GotenbergClientInterface $gotenbergClient;
protected static AssetBaseDirFormatter $assetBaseDirFormatter;

public static function setUpBeforeClass(): void
{
self::$twig = new Environment(new FilesystemLoader(self::FIXTURE_DIR.'/templates'));
self::$twig = new Environment(new FilesystemLoader(self::FIXTURE_DIR));
self::$assetBaseDirFormatter = new AssetBaseDirFormatter(new Filesystem(), self::FIXTURE_DIR, self::FIXTURE_DIR);
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved
}

protected function setUp(): void
{
self::$gotenbergClient = $this->createMock(GotenbergClientInterface::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this variable static ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reasons... habits I guess. Will change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

}

/**
* @param array<mixed> $data
*/
protected function assertFile(array $data, string $filename, string $expectedContent): void
{
self::assertArrayHasKey('files', $data);

$file = $data['files'];

self::assertInstanceOf(DataPart::class, $file);

self::assertSame($expectedContent, $file->getBody());
self::assertSame($filename, $file->getFilename());
}
}
Loading