diff --git a/tests/Documents/CreateDocumentTest.php b/tests/Documents/CreateDocumentTest.php index daa462a..533c806 100644 --- a/tests/Documents/CreateDocumentTest.php +++ b/tests/Documents/CreateDocumentTest.php @@ -166,4 +166,54 @@ public function test_document_created_with_new_format(): void $body['data']['type'] === 'doc'; }); } + + public function test_adding_empty_document_raise_error(): void + { + $mockClient = MockClient::global([ + CreateDocumentRequest::class => MockResponse::fixture('documents-create-empty'), + ]); + + $connector = $this->connector($mockClient); + + $documentContent = [ + 'type' => 'doc', + 'content' => [ + [ + 'category' => 'page', + 'attributes' => [ + 'page' => 1, + ], + 'content' => [ + [ + 'role' => 'body', + 'text' => '', + 'marks' => [], + 'attributes' => [ + 'bounding_box' => [], + ], + ], + ], + ], + ], + ]; + + $data = new Document('test-empty-id', 'en', $documentContent); + + $this->expectException(UnprocessableEntityException::class); + + $connector->documents('localhost')->create($data); + + $mockClient->assertSent(CreateDocumentRequest::class); + + $mockClient->assertSentCount(1); + + $mockClient->assertSent(function (Request $request) { + $body = $request->body()->all(); + + return $request->resolveEndpoint() === '/library/localhost/documents' && + $body['id'] === 'test-empty-id' && + $body['lang'] === 'en' && + $body['data']['type'] === 'doc'; + }); + } } diff --git a/tests/Fixtures/Saloon/documents-create-empty.json b/tests/Fixtures/Saloon/documents-create-empty.json new file mode 100644 index 0000000..edd5248 --- /dev/null +++ b/tests/Fixtures/Saloon/documents-create-empty.json @@ -0,0 +1,7 @@ +{ + "statusCode": 422, + "headers": { + "content-type": "application\/json" + }, + "data": "{\"detail\":\"The given document is empty\"}" +} \ No newline at end of file