Skip to content

Commit

Permalink
[UPDATE] Make empty array allowed in some cases (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neirda24 authored Jul 1, 2024
1 parent 5c6b1bb commit 29d8040
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 49 deletions.
16 changes: 16 additions & 0 deletions src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/Builder/Screenshot/AbstractChromiumScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions src/DependencyInjection/SensiolabsGotenbergExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Builder/Screenshot/HtmlScreenshotBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
],
Expand All @@ -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,
],
Expand All @@ -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,
],
Expand Down
20 changes: 20 additions & 0 deletions tests/DependencyInjection/SensiolabsGotenbergExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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' => [
Expand Down
41 changes: 24 additions & 17 deletions tests/GotenbergPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
43 changes: 24 additions & 19 deletions tests/GotenbergScreenshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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());
}
}

0 comments on commit 29d8040

Please sign in to comment.