Skip to content

Commit

Permalink
Add the metadata requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstantBqt committed Jul 9, 2024
1 parent 82e1835 commit 1f8ed84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Builder/Pdf/WriteMetadataPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function getMultipartFormData(): array
throw new MissingRequiredFieldException('At least one PDF file is required');
}

if ([] === ($this->formFields['metadata'] ?? [])) {
throw new MissingRequiredFieldException('At least one metadata field is required');
}

return parent::getMultipartFormData();
}

Expand Down
18 changes: 17 additions & 1 deletion tests/Builder/Pdf/WriteMetadataPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testEndpointIsCorrect(): void
self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf',
self::PDF_DOCUMENTS_DIR.'/simple_pdf_1.pdf',
)
->metadata(['Author' => 'Sensiolabs'])
->generate()
;
}
Expand Down Expand Up @@ -64,16 +65,31 @@ public function testConfigurationIsCorrectlySet(string $key, mixed $value, array
self::assertEquals($expected, $builder->getMultipartFormData()[0]);
}

public function testRequiredFormData(): void
public function testRequiredFilesFormFields(): void
{
$builder = $this->getWriteMetadataPdfBuilder();
$builder->metadata(['Author' => 'Sensiolabs']);

$this->expectException(MissingRequiredFieldException::class);
$this->expectExceptionMessage('At least one PDF file is required');

$builder->getMultipartFormData();
}

public function testRequiredMetadataFormFields(): void
{
$builder = $this->getWriteMetadataPdfBuilder();
$builder->files(
self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf',
self::PDF_DOCUMENTS_DIR.'/simple_pdf_1.pdf',
);

$this->expectException(MissingRequiredFieldException::class);
$this->expectExceptionMessage('At least one metadata field is required');

$builder->getMultipartFormData();
}

private function getWriteMetadataPdfBuilder(): WriteMetadataPdfBuilder
{
return new WriteMetadataPdfBuilder($this->gotenbergClient, self::$assetBaseDirFormatter);
Expand Down

0 comments on commit 1f8ed84

Please sign in to comment.