From 29d8040a36de71f30dd5a9b51b0665604017e358 Mon Sep 17 00:00:00 2001 From: Adrien Roches Date: Mon, 1 Jul 2024 22:10:13 +0200 Subject: [PATCH] [UPDATE] Make empty array allowed in some cases (#88) --- .../Pdf/AbstractChromiumPdfBuilder.php | 16 +++++++ .../AbstractChromiumScreenshotBuilder.php | 16 +++++++ src/DependencyInjection/Configuration.php | 4 +- .../SensiolabsGotenbergExtension.php | 4 -- .../Screenshot/HtmlScreenshotBuilderTest.php | 2 + .../DependencyInjection/ConfigurationTest.php | 17 +++++--- .../SensiolabsGotenbergExtensionTest.php | 20 +++++++++ tests/GotenbergPdfTest.php | 41 ++++++++++-------- tests/GotenbergScreenshotTest.php | 43 +++++++++++-------- 9 files changed, 114 insertions(+), 49 deletions(-) diff --git a/src/Builder/Pdf/AbstractChromiumPdfBuilder.php b/src/Builder/Pdf/AbstractChromiumPdfBuilder.php index 83fcaee7..46a114d6 100644 --- a/src/Builder/Pdf/AbstractChromiumPdfBuilder.php +++ b/src/Builder/Pdf/AbstractChromiumPdfBuilder.php @@ -331,6 +331,12 @@ public function emulatedMediaType(EmulatedMediaType $mediaType): static */ public function cookies(array $cookies): static { + if ([] === $cookies) { + unset($this->formFields['cookies']); + + return $this; + } + $this->formFields['cookies'] = []; foreach ($cookies as $cookie) { @@ -377,6 +383,12 @@ public function addCookies(array $cookies): static */ public function extraHttpHeaders(array $headers): static { + if ([] === $headers) { + unset($this->formFields['extraHttpHeaders']); + + return $this; + } + $this->formFields['extraHttpHeaders'] = $headers; return $this; @@ -392,6 +404,10 @@ public function extraHttpHeaders(array $headers): static */ public function addExtraHttpHeaders(array $headers): static { + if ([] === $headers) { + return $this; + } + $this->formFields['extraHttpHeaders'] = array_merge($this->formFields['extraHttpHeaders'] ?? [], $headers); return $this; diff --git a/src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php b/src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php index ef464bc6..f5af4867 100644 --- a/src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php +++ b/src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php @@ -173,6 +173,12 @@ public function emulatedMediaType(EmulatedMediaType $mediaType): static */ public function cookies(array $cookies): static { + if ([] === $cookies) { + unset($this->formFields['cookies']); + + return $this; + } + $this->formFields['cookies'] = []; foreach ($cookies as $cookie) { @@ -219,6 +225,12 @@ public function addCookies(array $cookies): static */ public function extraHttpHeaders(array $headers): static { + if ([] === $headers) { + unset($this->formFields['extraHttpHeaders']); + + return $this; + } + $this->formFields['extraHttpHeaders'] = $headers; return $this; @@ -234,6 +246,10 @@ public function extraHttpHeaders(array $headers): static */ public function addExtraHttpHeaders(array $headers): static { + if ([] === $headers) { + return $this; + } + $this->formFields['extraHttpHeaders'] = array_merge($this->formFields['extraHttpHeaders'] ?? [], $headers); return $this; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 36cbe57d..7cbba9bc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -311,7 +311,7 @@ private function addChromiumPdfOptionsNode(ArrayNodeDefinition $parent): void ->end() ->arrayNode('fail_on_http_status_codes') ->info('Return a 409 Conflict response if the HTTP status code from the main page is not acceptable. - default [499,599]. https://gotenberg.dev/docs/routes#invalid-http-status-codes-chromium') - ->defaultValue([]) + ->defaultValue([499, 599]) ->integerPrototype() ->end() ->end() @@ -442,7 +442,7 @@ private function addChromiumScreenshotOptionsNode(ArrayNodeDefinition $parent): ->end() ->arrayNode('fail_on_http_status_codes') ->info('Return a 409 Conflict response if the HTTP status code from the main page is not acceptable. - default [499,599]. https://gotenberg.dev/docs/routes#invalid-http-status-codes-chromium') - ->defaultValue([]) + ->defaultValue([499, 599]) ->integerPrototype() ->end() ->end() diff --git a/src/DependencyInjection/SensiolabsGotenbergExtension.php b/src/DependencyInjection/SensiolabsGotenbergExtension.php index 7ab04f57..1a2e217d 100644 --- a/src/DependencyInjection/SensiolabsGotenbergExtension.php +++ b/src/DependencyInjection/SensiolabsGotenbergExtension.php @@ -101,10 +101,6 @@ public function load(array $configs, ContainerBuilder $container): void private function cleanUserOptions(array $userConfigurations): array { return array_filter($userConfigurations, static function ($config): bool { - if (\is_array($config)) { - return 0 !== \count($config); - } - return null !== $config; }); } diff --git a/tests/Builder/Screenshot/HtmlScreenshotBuilderTest.php b/tests/Builder/Screenshot/HtmlScreenshotBuilderTest.php index 22ba1520..b79ed3a6 100644 --- a/tests/Builder/Screenshot/HtmlScreenshotBuilderTest.php +++ b/tests/Builder/Screenshot/HtmlScreenshotBuilderTest.php @@ -11,11 +11,13 @@ use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException; use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase; +use Sensiolabs\GotenbergBundle\Twig\GotenbergAssetExtension; #[CoversClass(HtmlScreenshotBuilder::class)] #[UsesClass(AbstractChromiumScreenshotBuilder::class)] #[UsesClass(AbstractScreenshotBuilder::class)] #[UsesClass(AssetBaseDirFormatter::class)] +#[UsesClass(GotenbergAssetExtension::class)] final class HtmlScreenshotBuilderTest extends AbstractBuilderTestCase { public function testEndpointIsCorrect(): void diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 2fb22055..fc1f8124 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -76,7 +76,10 @@ public function testWithExtraHeadersConfiguration(): void ]); $config = $this->cleanOptions($config['default_options']['pdf']['html']); - self::assertEquals(['extra_http_headers' => ['MyHeader' => 'MyValue', 'User-Agent' => 'MyValue']], $config); + self::assertEquals([ + 'extra_http_headers' => ['MyHeader' => 'MyValue', 'User-Agent' => 'MyValue'], + 'fail_on_http_status_codes' => ['499', '599'], + ], $config); } /** @@ -119,7 +122,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, 'pdf_format' => null, @@ -144,7 +147,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, 'pdf_format' => null, @@ -169,7 +172,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, 'pdf_format' => null, @@ -207,7 +210,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, ], @@ -224,7 +227,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, ], @@ -241,7 +244,7 @@ private static function getBundleDefaultConfig(): array 'emulated_media_type' => null, 'cookies' => [], 'extra_http_headers' => [], - 'fail_on_http_status_codes' => [], + 'fail_on_http_status_codes' => [499, 599], 'fail_on_console_exceptions' => null, 'skip_network_idle_event' => null, ], diff --git a/tests/DependencyInjection/SensiolabsGotenbergExtensionTest.php b/tests/DependencyInjection/SensiolabsGotenbergExtensionTest.php index a0e31cad..81654af0 100644 --- a/tests/DependencyInjection/SensiolabsGotenbergExtensionTest.php +++ b/tests/DependencyInjection/SensiolabsGotenbergExtensionTest.php @@ -87,6 +87,7 @@ public function testGotenbergConfiguredWithValidConfig(): void 'skip_network_idle_event' => false, 'pdf_format' => PdfFormat::Pdf2b->value, 'pdf_universal_access' => false, + 'cookies' => [], ], 'markdown' => [ 'paper_width' => 30, @@ -110,6 +111,7 @@ public function testGotenbergConfiguredWithValidConfig(): void 'skip_network_idle_event' => true, 'pdf_format' => PdfFormat::Pdf3b->value, 'pdf_universal_access' => true, + 'cookies' => [], ], 'office' => [ 'landscape' => false, @@ -303,16 +305,25 @@ public function testDataCollectorIsProperlyConfiguredIfEnabled(): void 'metadata' => [ 'Author' => 'SensioLabs HTML', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'url' => [ 'metadata' => [ 'Author' => 'SensioLabs URL', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'markdown' => [ 'metadata' => [ 'Author' => 'SensioLabs MARKDOWN', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'office' => [ 'metadata' => [ @@ -340,16 +351,25 @@ public function testDataCollectorIsProperlyConfiguredIfEnabled(): void 'metadata' => [ 'Author' => 'SensioLabs HTML', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'url' => [ 'metadata' => [ 'Author' => 'SensioLabs URL', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'markdown' => [ 'metadata' => [ 'Author' => 'SensioLabs MARKDOWN', ], + 'cookies' => [], + 'extra_http_headers' => [], + 'fail_on_http_status_codes' => [], ], 'office' => [ 'metadata' => [ diff --git a/tests/GotenbergPdfTest.php b/tests/GotenbergPdfTest.php index 3bebaabe..e8b099b5 100644 --- a/tests/GotenbergPdfTest.php +++ b/tests/GotenbergPdfTest.php @@ -60,7 +60,11 @@ public function testUrlBuilderFactory(): void ->url('https://google.com') ; - self::assertSame([['nativePageRanges' => '1-5'], ['url' => 'https://google.com']], $builder->getMultipartFormData()); + self::assertSame([ + ['failOnHttpStatusCodes' => '[499,599]'], + ['nativePageRanges' => '1-5'], + ['url' => 'https://google.com'], + ], $builder->getMultipartFormData()); } public function testHtmlBuilderFactory(): void @@ -80,20 +84,23 @@ public function testHtmlBuilderFactory(): void $builder->contentFile(__DIR__.'/../Fixtures/files/content.html'); $multipartFormData = $builder->getMultipartFormData(); - self::assertCount(3, $multipartFormData); + self::assertCount(4, $multipartFormData); self::assertArrayHasKey(0, $multipartFormData); - self::assertSame(['marginTop' => '3in'], $multipartFormData[0]); + self::assertSame(['failOnHttpStatusCodes' => '[499,599]'], $multipartFormData[0]); self::assertArrayHasKey(1, $multipartFormData); - self::assertSame(['marginBottom' => '1in'], $multipartFormData[1]); + self::assertSame(['marginTop' => '3in'], $multipartFormData[1]); self::assertArrayHasKey(2, $multipartFormData); - self::assertIsArray($multipartFormData[2]); - self::assertCount(1, $multipartFormData[2]); - self::assertArrayHasKey('files', $multipartFormData[2]); - self::assertInstanceOf(DataPart::class, $multipartFormData[2]['files']); - self::assertSame('index.html', $multipartFormData[2]['files']->getFilename()); + self::assertSame(['marginBottom' => '1in'], $multipartFormData[2]); + + self::assertArrayHasKey(3, $multipartFormData); + self::assertIsArray($multipartFormData[3]); + self::assertCount(1, $multipartFormData[3]); + self::assertArrayHasKey('files', $multipartFormData[3]); + self::assertInstanceOf(DataPart::class, $multipartFormData[3]['files']); + self::assertSame('index.html', $multipartFormData[3]['files']->getFilename()); } public function testMarkdownBuilderFactory(): void @@ -110,19 +117,19 @@ public function testMarkdownBuilderFactory(): void $builder->wrapperFile(__DIR__.'/Fixtures/files/wrapper.html'); $multipartFormData = $builder->getMultipartFormData(); - self::assertCount(2, $multipartFormData); - - self::assertArrayHasKey(0, $multipartFormData); - self::assertIsArray($multipartFormData[0]); - self::assertArrayHasKey('files', $multipartFormData[0]); - self::assertInstanceOf(DataPart::class, $multipartFormData[0]['files']); - self::assertSame('file.md', $multipartFormData[0]['files']->getFilename()); + self::assertCount(3, $multipartFormData); self::assertArrayHasKey(1, $multipartFormData); self::assertIsArray($multipartFormData[1]); self::assertArrayHasKey('files', $multipartFormData[1]); self::assertInstanceOf(DataPart::class, $multipartFormData[1]['files']); - self::assertSame('index.html', $multipartFormData[1]['files']->getFilename()); + self::assertSame('file.md', $multipartFormData[1]['files']->getFilename()); + + self::assertArrayHasKey(2, $multipartFormData); + self::assertIsArray($multipartFormData[2]); + self::assertArrayHasKey('files', $multipartFormData[2]); + self::assertInstanceOf(DataPart::class, $multipartFormData[2]['files']); + self::assertSame('index.html', $multipartFormData[2]['files']->getFilename()); } public function testOfficeBuilderFactory(): void diff --git a/tests/GotenbergScreenshotTest.php b/tests/GotenbergScreenshotTest.php index 450f8c45..e117ae0a 100644 --- a/tests/GotenbergScreenshotTest.php +++ b/tests/GotenbergScreenshotTest.php @@ -45,7 +45,12 @@ public function testUrlBuilderFactory(): void ->url('https://google.com') ; - self::assertSame([['width' => '500'], ['height' => '500'], ['url' => 'https://google.com']], $builder->getMultipartFormData()); + self::assertSame([ + ['failOnHttpStatusCodes' => '[499,599]'], + ['width' => '500'], + ['height' => '500'], + ['url' => 'https://google.com'], + ], $builder->getMultipartFormData()); } public function testHtmlBuilderFactory(): void @@ -65,20 +70,20 @@ public function testHtmlBuilderFactory(): void $builder->contentFile(__DIR__.'/../Fixtures/files/content.html'); $multipartFormData = $builder->getMultipartFormData(); - self::assertCount(3, $multipartFormData); - - self::assertArrayHasKey(0, $multipartFormData); - self::assertSame(['format' => 'jpeg'], $multipartFormData[0]); + self::assertCount(4, $multipartFormData); self::assertArrayHasKey(1, $multipartFormData); - self::assertSame(['quality' => '50'], $multipartFormData[1]); + self::assertSame(['format' => 'jpeg'], $multipartFormData[1]); self::assertArrayHasKey(2, $multipartFormData); - self::assertIsArray($multipartFormData[2]); - self::assertCount(1, $multipartFormData[2]); - self::assertArrayHasKey('files', $multipartFormData[2]); - self::assertInstanceOf(DataPart::class, $multipartFormData[2]['files']); - self::assertSame('index.html', $multipartFormData[2]['files']->getFilename()); + self::assertSame(['quality' => '50'], $multipartFormData[2]); + + self::assertArrayHasKey(3, $multipartFormData); + self::assertIsArray($multipartFormData[3]); + self::assertCount(1, $multipartFormData[3]); + self::assertArrayHasKey('files', $multipartFormData[3]); + self::assertInstanceOf(DataPart::class, $multipartFormData[3]['files']); + self::assertSame('index.html', $multipartFormData[3]['files']->getFilename()); } public function testMarkdownBuilderFactory(): void @@ -95,18 +100,18 @@ public function testMarkdownBuilderFactory(): void $builder->wrapperFile(__DIR__.'/Fixtures/files/wrapper.html'); $multipartFormData = $builder->getMultipartFormData(); - self::assertCount(2, $multipartFormData); - - self::assertArrayHasKey(0, $multipartFormData); - self::assertIsArray($multipartFormData[0]); - self::assertArrayHasKey('files', $multipartFormData[0]); - self::assertInstanceOf(DataPart::class, $multipartFormData[0]['files']); - self::assertSame('file.md', $multipartFormData[0]['files']->getFilename()); + self::assertCount(3, $multipartFormData); self::assertArrayHasKey(1, $multipartFormData); self::assertIsArray($multipartFormData[1]); self::assertArrayHasKey('files', $multipartFormData[1]); self::assertInstanceOf(DataPart::class, $multipartFormData[1]['files']); - self::assertSame('index.html', $multipartFormData[1]['files']->getFilename()); + self::assertSame('file.md', $multipartFormData[1]['files']->getFilename()); + + self::assertArrayHasKey(2, $multipartFormData); + self::assertIsArray($multipartFormData[2]); + self::assertArrayHasKey('files', $multipartFormData[2]); + self::assertInstanceOf(DataPart::class, $multipartFormData[2]['files']); + self::assertSame('index.html', $multipartFormData[2]['files']->getFilename()); } }