From ee611cce5b022b993e228be73eb835b4d7194278 Mon Sep 17 00:00:00 2001 From: mbruckmoser Date: Tue, 4 Feb 2025 10:40:26 +0100 Subject: [PATCH] Move API key in request header and change body to json format (#89) - also add examples for live testing and development scenarios --- example/retrieve_usage.php | 14 + example/translate_file.php | 73 ++++ src/DeeplClient.php | 65 ++-- src/DeeplClientFactory.php | 2 +- src/Handler/AbstractDeeplHandler.php | 6 +- .../DeeplBatchTranslationRequestHandler.php | 51 ++- src/Handler/DeeplFileRequestHandler.php | 21 +- .../DeeplFileSubmissionRequestHandler.php | 7 +- ...eplFileTranslationStatusRequestHandler.php | 21 +- ...lGlossariesListRetrievalRequestHandler.php | 16 +- ...dLanguagesPairsRetrievalRequestHandler.php | 16 +- .../DeeplGlossaryCreateRequestHandler.php | 23 +- .../DeeplGlossaryDeleteRequestHandler.php | 16 +- ...lGlossaryEntriesRetrieveRequestHandler.php | 16 +- .../DeeplGlossaryRetrieveRequestHandler.php | 16 +- src/Handler/DeeplRequestFactory.php | 67 ++-- src/Handler/DeeplRequestFactoryInterface.php | 2 - src/Handler/DeeplRequestHandlerInterface.php | 2 - ...pportedLanguageRetrievalRequestHandler.php | 21 +- .../DeeplTranslationRequestHandler.php | 25 +- src/Handler/DeeplUsageRequestHandler.php | 21 +- src/Model/SupportedLanguages.php | 4 +- tests/DeeplClientTest.php | 321 ++++++++++-------- ...eeplBatchTranslationRequestHandlerTest.php | 88 +++-- tests/Handler/DeeplFileRequestHandlerTest.php | 44 +-- .../DeeplFileSubmissionRequestHandlerTest.php | 69 ++-- ...ileTranslationStatusRequestHandlerTest.php | 42 +-- ...ssariesListRetrievalRequestHandlerTest.php | 51 ++- ...guagesPairsRetrievalRequestHandlerTest.php | 49 ++- .../DeeplGlossaryCreateRequestHandlerTest.php | 55 ++- .../DeeplGlossaryDeleteRequestHandlerTest.php | 51 ++- ...ssaryEntriesRetrieveRequestHandlerTest.php | 57 ++-- ...eeplGlossaryRetrieveRequestHandlerTest.php | 51 ++- tests/Handler/DeeplRequestFactoryTest.php | 149 +++++--- ...tedLanguageRetrievalRequestHandlerTest.php | 48 ++- .../DeeplTranslationRequestHandlerTest.php | 84 +++-- .../Handler/DeeplUsageRequestHandlerTest.php | 31 +- 37 files changed, 820 insertions(+), 875 deletions(-) create mode 100644 example/retrieve_usage.php create mode 100644 example/translate_file.php diff --git a/example/retrieve_usage.php b/example/retrieve_usage.php new file mode 100644 index 0000000..eb54460 --- /dev/null +++ b/example/retrieve_usage.php @@ -0,0 +1,14 @@ +getUsage(); + +var_dump($usage); diff --git a/example/translate_file.php b/example/translate_file.php new file mode 100644 index 0000000..a472d39 --- /dev/null +++ b/example/translate_file.php @@ -0,0 +1,73 @@ +translateFile($fileTranslationConfig); + +/** + * Result look like a FileSubmission instance + * + * class Scn\DeeplApiConnector\Model\FileSubmission#22 (2) { + * private string $documentId => "" + * private string $documentKey => "" + * } + */ +var_dump($fileSubmission); + +/** + * We can simulate this by using our own instance with valid values: + * $fileSubmission = (new FileSubmission()) + * ->setDocumentId('') + * ->setDocumentKey(''); + */ + + +/** 2. We request in a queue logic the translation status with the Submission instance **/ +sleep(15); +$translationStatus = $deepl->getFileTranslationStatus($fileSubmission); + +/** + * Result look like a FileTranslationStatus instance + * + * if the 'status' property value is 'done' we can get the fileTranslation + * + * class Scn\DeeplApiConnector\Model\FileTranslationStatus#43 (4) { + * private string $documentId => "" + * private string $status => "translating" + * ..... + * } + */ +var_dump($translationStatus); + + +/** 3. We request in a queue logic the translation status with the Submission instance **/ +$response = $deepl->getFileTranslation($fileSubmission); + +/** + * Result look like a FileTranslation instance + * + * class Scn\DeeplApiConnector\Model\FileTranslation#26 (1) { + * private string $content => "This is a test" + * } + */ +var_dump($response); diff --git a/src/DeeplClient.php b/src/DeeplClient.php index 51f1d01..b0c64d3 100644 --- a/src/DeeplClient.php +++ b/src/DeeplClient.php @@ -33,6 +33,10 @@ class DeeplClient implements DeeplClientInterface { + private const DEEPL_PAID_BASE_URI = 'https://api.deepl.com'; + private const DEEPL_FREE_BASE_URI = 'https://api-free.deepl.com'; + + private string $apiKey; private DeeplRequestFactoryInterface $deeplRequestFactory; private ClientInterface $httpClient; @@ -40,17 +44,19 @@ class DeeplClient implements DeeplClientInterface private RequestFactoryInterface $requestFactory; public function __construct( + string $apiKey, DeeplRequestFactoryInterface $deeplRequestFactory, ClientInterface $httpClient, - RequestFactoryInterface $requestFactory + RequestFactoryInterface $requestFactory, ) { + $this->apiKey = $apiKey; $this->deeplRequestFactory = $deeplRequestFactory; $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; } /** - * Return Usage of API- Key + * Return Usage of API-Key * Possible Return:. * * Usage @@ -62,7 +68,7 @@ public function __construct( public function getUsage(): ResponseModelInterface { return (new Usage())->hydrate( - $this->executeRequest($this->deeplRequestFactory->createDeeplUsageRequestHandler()) + $this->executeRequest($this->deeplRequestFactory->createDeeplUsageRequestHandler()), ); } @@ -79,7 +85,7 @@ public function getUsage(): ResponseModelInterface public function getTranslation(TranslationConfigInterface $translation): ResponseModelInterface { return (new Translation())->hydrate($this->executeRequest( - $this->deeplRequestFactory->createDeeplTranslationRequestHandler($translation) + $this->deeplRequestFactory->createDeeplTranslationRequestHandler($translation), )); } @@ -98,7 +104,7 @@ public function translate(string $text, string $target_language): ResponseModelI public function translateFile(FileTranslationConfigInterface $fileTranslation): ResponseModelInterface { return (new FileSubmission())->hydrate($this->executeRequest( - $this->deeplRequestFactory->createDeeplFileSubmissionRequestHandler($fileTranslation) + $this->deeplRequestFactory->createDeeplFileSubmissionRequestHandler($fileTranslation), )); } @@ -106,7 +112,7 @@ public function translateBatch(array $text, string $targetLanguage): ResponseMod { return (new BatchTranslation())->hydrate($this->executeRequest( $this->deeplRequestFactory->createDeeplBatchTranslationRequestHandler( - new BatchTranslationConfig($text, $targetLanguage) + new BatchTranslationConfig($text, $targetLanguage), ) )); } @@ -114,14 +120,14 @@ public function translateBatch(array $text, string $targetLanguage): ResponseMod public function getFileTranslationStatus(FileSubmissionInterface $fileSubmission): ResponseModelInterface { return (new FileTranslationStatus())->hydrate($this->executeRequest( - $this->deeplRequestFactory->createDeeplFileTranslationStatusRequestHandler($fileSubmission) + $this->deeplRequestFactory->createDeeplFileTranslationStatusRequestHandler($fileSubmission), )); } public function getFileTranslation(FileSubmissionInterface $fileSubmission): ResponseModelInterface { return (new FileTranslation())->hydrate($this->executeRequest( - $this->deeplRequestFactory->createDeeplFileTranslationRequestHandler($fileSubmission) + $this->deeplRequestFactory->createDeeplFileTranslationRequestHandler($fileSubmission), )); } @@ -129,7 +135,7 @@ public function getSupportedLanguages(): ResponseModelInterface { return (new SupportedLanguages())->hydrate( $this->executeRequest( - $this->deeplRequestFactory->createDeeplSupportedLanguageRetrievalRequestHandler() + $this->deeplRequestFactory->createDeeplSupportedLanguageRetrievalRequestHandler(), ) ); } @@ -138,7 +144,7 @@ public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface { return (new GlossariesSupportedLanguagesPairs())->hydrate( $this->executeRequest( - $this->deeplRequestFactory->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler() + $this->deeplRequestFactory->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(), ) ); } @@ -147,7 +153,7 @@ public function getGlossariesList(): ResponseModelInterface { return (new Glossaries())->hydrate( $this->executeRequest( - $this->deeplRequestFactory->createDeeplGlossariesListRetrievalRequestHandler() + $this->deeplRequestFactory->createDeeplGlossariesListRetrievalRequestHandler(), ) ); } @@ -194,20 +200,23 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s $request = $this->requestFactory ->createRequest( $requestHandler->getMethod(), - sprintf('%s%s', $this->deeplRequestFactory->getDeeplBaseUri(), $requestHandler->getPath()) + sprintf('%s%s', $this->getDeeplBaseUri(), $requestHandler->getPath()), + ) + ->withHeader( + 'Authorization', + $this->getAuthHeader(), ) ->withHeader( 'Content-Type', - $requestHandler->getContentType() + $requestHandler->getContentType(), ) - ->withBody($requestHandler->getBody()); - - if ($requestHandler->getAuthHeader() !== null) { - $request = $request->withHeader('Authorization', $requestHandler->getAuthHeader()); - } + ->withBody( + $requestHandler->getBody(), + ); - if ($requestHandler->getAcceptHeader() !== null) { - $request = $request->withHeader('Accept', $requestHandler->getAcceptHeader()); + $acceptHeader = $requestHandler->getAcceptHeader(); + if ($acceptHeader !== null) { + $request = $request->withHeader('Accept', $acceptHeader); } try { @@ -216,7 +225,7 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s throw new RequestException( $exception->getMessage(), $exception->getCode(), - $exception + $exception, ); } @@ -253,4 +262,18 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s /** @var stdClass $result */ return $result; } + + private function getAuthHeader(): string + { + return sprintf('DeepL-Auth-Key %s', $this->apiKey); + } + + private function getDeeplBaseUri(): string + { + if (str_contains($this->apiKey, ':fx')) { + return self::DEEPL_FREE_BASE_URI; + } + + return self::DEEPL_PAID_BASE_URI; + } } diff --git a/src/DeeplClientFactory.php b/src/DeeplClientFactory.php index 6e580c2..bee3f12 100644 --- a/src/DeeplClientFactory.php +++ b/src/DeeplClientFactory.php @@ -23,8 +23,8 @@ public static function create( StreamFactoryInterface $streamFactory = null ): DeeplClientInterface { return new DeeplClient( + $authKey, new DeeplRequestFactory( - $authKey, $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory() ), $httpClient ?? Psr18ClientDiscovery::find(), diff --git a/src/Handler/AbstractDeeplHandler.php b/src/Handler/AbstractDeeplHandler.php index 9ab0c29..6893d93 100644 --- a/src/Handler/AbstractDeeplHandler.php +++ b/src/Handler/AbstractDeeplHandler.php @@ -6,13 +6,13 @@ abstract class AbstractDeeplHandler implements DeeplRequestHandlerInterface { - public function getAuthHeader(): ?string + public function getAcceptHeader(): ?string { return null; } - public function getAcceptHeader(): ?string + public function getContentType(): string { - return null; + return 'application/json'; } } diff --git a/src/Handler/DeeplBatchTranslationRequestHandler.php b/src/Handler/DeeplBatchTranslationRequestHandler.php index eb7d5d8..2b39697 100644 --- a/src/Handler/DeeplBatchTranslationRequestHandler.php +++ b/src/Handler/DeeplBatchTranslationRequestHandler.php @@ -16,18 +16,14 @@ final class DeeplBatchTranslationRequestHandler extends AbstractDeeplHandler private const SEPARATOR = ','; public const API_ENDPOINT = '/v2/translate'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private BatchTranslationConfigInterface $translation; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - BatchTranslationConfigInterface $translation + BatchTranslationConfigInterface $translation, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->translation = $translation; } @@ -44,37 +40,28 @@ public function getPath(): string public function getBody(): StreamInterface { - $query = http_build_query( - array_filter( - [ - 'target_lang' => $this->translation->getTargetLang(), - 'tag_handling' => implode( - self::SEPARATOR, - $this->translation->getTagHandling() - ), - 'non_splitting_tags' => implode( - self::SEPARATOR, - $this->translation->getNonSplittingTags() - ), - 'ignore_tags' => implode(self::SEPARATOR, $this->translation->getIgnoreTags()), - 'split_sentences' => $this->translation->getSplitSentences(), - 'preserve_formatting' => $this->translation->getPreserveFormatting(), - 'glossary_id' => $this->translation->getGlossaryId(), - 'auth_key' => $this->authKey, - ] - ) - ); + $body = array_filter([ + 'target_lang' => $this->translation->getTargetLang(), + 'tag_handling' => implode( + self::SEPARATOR, + $this->translation->getTagHandling(), + ), + 'non_splitting_tags' => implode( + self::SEPARATOR, + $this->translation->getNonSplittingTags(), + ), + 'ignore_tags' => implode(self::SEPARATOR, $this->translation->getIgnoreTags()), + 'split_sentences' => $this->translation->getSplitSentences(), + 'preserve_formatting' => $this->translation->getPreserveFormatting(), + 'glossary_id' => $this->translation->getGlossaryId(), + 'text' => [], + ]); // add the text parameters separately as http_build_query would create `text[]` params foreach ($this->translation->getText() as $text) { - $query .= '&text=' . $text; + $body['text'][] = $text; } - return $this->streamFactory->createStream($query); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; + return $this->streamFactory->createStream(json_encode($body, JSON_THROW_ON_ERROR)); } } diff --git a/src/Handler/DeeplFileRequestHandler.php b/src/Handler/DeeplFileRequestHandler.php index a644b64..68f8fff 100644 --- a/src/Handler/DeeplFileRequestHandler.php +++ b/src/Handler/DeeplFileRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplFileRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/document/%s/result'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private FileSubmissionInterface $fileSubmission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - FileSubmissionInterface $fileSubmission + FileSubmissionInterface $fileSubmission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->fileSubmission = $fileSubmission; } @@ -41,19 +37,14 @@ public function getPath(): string public function getBody(): StreamInterface { return $this->streamFactory->createStream( - http_build_query( + json_encode( array_filter( [ - 'auth_key' => $this->authKey, 'document_key' => $this->fileSubmission->getDocumentKey(), - ] - ) - ) + ], + ), + JSON_THROW_ON_ERROR + ), ); } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplFileSubmissionRequestHandler.php b/src/Handler/DeeplFileSubmissionRequestHandler.php index d65700b..427adf5 100644 --- a/src/Handler/DeeplFileSubmissionRequestHandler.php +++ b/src/Handler/DeeplFileSubmissionRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplFileSubmissionRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/document'; - private string $authKey; - private FileTranslationConfigInterface $fileTranslation; private MultipartStreamBuilder $multipartStreamBuilder; public function __construct( - string $authKey, FileTranslationConfigInterface $fileTranslation, - MultipartStreamBuilder $multipartStreamBuilder + MultipartStreamBuilder $multipartStreamBuilder, ) { - $this->authKey = $authKey; $this->fileTranslation = $fileTranslation; $this->multipartStreamBuilder = $multipartStreamBuilder; } @@ -42,7 +38,6 @@ public function getBody(): StreamInterface { $streamBuilder = $this->multipartStreamBuilder ->setBoundary('boundary') - ->addResource('auth_key', $this->authKey) ->addResource('file', $this->fileTranslation->getFileContent(), ['filename' => $this->fileTranslation->getFileName()]) ->addResource('target_lang', $this->fileTranslation->getTargetLang()); diff --git a/src/Handler/DeeplFileTranslationStatusRequestHandler.php b/src/Handler/DeeplFileTranslationStatusRequestHandler.php index a0679b9..5f8da1a 100644 --- a/src/Handler/DeeplFileTranslationStatusRequestHandler.php +++ b/src/Handler/DeeplFileTranslationStatusRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplFileTranslationStatusRequestHandler extends AbstractDeeplHandle { public const API_ENDPOINT = '/v2/document/%s'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private FileSubmissionInterface $fileSubmission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - FileSubmissionInterface $fileSubmission + FileSubmissionInterface $fileSubmission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->fileSubmission = $fileSubmission; } @@ -41,19 +37,14 @@ public function getPath(): string public function getBody(): StreamInterface { return $this->streamFactory->createStream( - http_build_query( + json_encode( array_filter( [ - 'auth_key' => $this->authKey, 'document_key' => $this->fileSubmission->getDocumentKey(), - ] - ) - ) + ], + ), + JSON_THROW_ON_ERROR + ), ); } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php b/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php index 2571b8b..efa9ba9 100644 --- a/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php +++ b/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php @@ -11,15 +11,11 @@ final class DeeplGlossariesListRetrievalRequestHandler extends AbstractDeeplHand { public const API_ENDPOINT = '/v2/glossaries'; - private string $authKey; - private StreamFactoryInterface $streamFactory; public function __construct( - string $authKey, - StreamFactoryInterface $streamFactory + StreamFactoryInterface $streamFactory, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; } @@ -37,14 +33,4 @@ public function getBody(): StreamInterface { return $this->streamFactory->createStream(); } - - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php b/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php index c6602e1..9520724 100644 --- a/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php +++ b/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php @@ -11,15 +11,11 @@ final class DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler extend { public const API_ENDPOINT = '/v2/glossary-language-pairs'; - private string $authKey; - private StreamFactoryInterface $streamFactory; public function __construct( - string $authKey, - StreamFactoryInterface $streamFactory + StreamFactoryInterface $streamFactory, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; } @@ -37,14 +33,4 @@ public function getBody(): StreamInterface { return $this->streamFactory->createStream(); } - - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossaryCreateRequestHandler.php b/src/Handler/DeeplGlossaryCreateRequestHandler.php index 2261c65..de27d2f 100644 --- a/src/Handler/DeeplGlossaryCreateRequestHandler.php +++ b/src/Handler/DeeplGlossaryCreateRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplGlossaryCreateRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/glossaries'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private GlossarySubmissionInterface $submission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - GlossarySubmissionInterface $submission + GlossarySubmissionInterface $submission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->submission = $submission; } @@ -41,21 +37,12 @@ public function getPath(): string public function getBody(): StreamInterface { return $this->streamFactory->createStream( - http_build_query( + json_encode( array_filter( $this->submission->toArrayRequest(), - ) - ) + ), + JSON_THROW_ON_ERROR + ), ); } - - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossaryDeleteRequestHandler.php b/src/Handler/DeeplGlossaryDeleteRequestHandler.php index 5e7b8d2..80035dc 100644 --- a/src/Handler/DeeplGlossaryDeleteRequestHandler.php +++ b/src/Handler/DeeplGlossaryDeleteRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplGlossaryDeleteRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/glossaries/%s'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private GlossaryIdSubmissionInterface $submission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - GlossaryIdSubmissionInterface $submission + GlossaryIdSubmissionInterface $submission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->submission = $submission; } @@ -42,14 +38,4 @@ public function getBody(): StreamInterface { return $this->streamFactory->createStream(); } - - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php b/src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php index 9bbd940..e587bba 100644 --- a/src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php +++ b/src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplGlossaryEntriesRetrieveRequestHandler extends AbstractDeeplHand { public const API_ENDPOINT = '/v2/glossaries/%s/entries'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private GlossaryIdSubmissionInterface $submission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - GlossaryIdSubmissionInterface $submission + GlossaryIdSubmissionInterface $submission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->submission = $submission; } @@ -43,18 +39,8 @@ public function getBody(): StreamInterface return $this->streamFactory->createStream(); } - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - public function getAcceptHeader(): ?string { return 'text/tab-separated-values'; } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplGlossaryRetrieveRequestHandler.php b/src/Handler/DeeplGlossaryRetrieveRequestHandler.php index 1e1e66c..938ef51 100644 --- a/src/Handler/DeeplGlossaryRetrieveRequestHandler.php +++ b/src/Handler/DeeplGlossaryRetrieveRequestHandler.php @@ -12,18 +12,14 @@ final class DeeplGlossaryRetrieveRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/glossaries/%s'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private GlossaryIdSubmissionInterface $submission; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - GlossaryIdSubmissionInterface $submission + GlossaryIdSubmissionInterface $submission, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->submission = $submission; } @@ -42,14 +38,4 @@ public function getBody(): StreamInterface { return $this->streamFactory->createStream(); } - - public function getAuthHeader(): ?string - { - return sprintf('DeepL-Auth-Key %s', $this->authKey); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplRequestFactory.php b/src/Handler/DeeplRequestFactory.php index dc40774..6c0eaa5 100644 --- a/src/Handler/DeeplRequestFactory.php +++ b/src/Handler/DeeplRequestFactory.php @@ -15,46 +15,36 @@ final class DeeplRequestFactory implements DeeplRequestFactoryInterface { - public const DEEPL_PAID_BASE_URI = 'https://api.deepl.com'; - public const DEEPL_FREE_BASE_URI = 'https://api-free.deepl.com'; - - private string $authKey; - private StreamFactoryInterface $streamFactory; public function __construct( - string $authKey, - StreamFactoryInterface $streamFactory + StreamFactoryInterface $streamFactory, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; } public function createDeeplTranslationRequestHandler( - TranslationConfigInterface $translation + TranslationConfigInterface $translation, ): DeeplRequestHandlerInterface { return new DeeplTranslationRequestHandler( - $this->authKey, $this->streamFactory, - $translation + $translation, ); } public function createDeeplBatchTranslationRequestHandler( - BatchTranslationConfigInterface $translation + BatchTranslationConfigInterface $translation, ): DeeplRequestHandlerInterface { return new DeeplBatchTranslationRequestHandler( - $this->authKey, $this->streamFactory, - $translation + $translation, ); } public function createDeeplUsageRequestHandler(): DeeplRequestHandlerInterface { return new DeeplUsageRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } @@ -62,11 +52,10 @@ public function createDeeplFileSubmissionRequestHandler( FileTranslationConfigInterface $fileTranslation ): DeeplRequestHandlerInterface { return new DeeplFileSubmissionRequestHandler( - $this->authKey, $fileTranslation, new MultipartStreamBuilder( - $this->streamFactory - ) + $this->streamFactory, + ), ); } @@ -74,9 +63,8 @@ public function createDeeplFileTranslationStatusRequestHandler( FileSubmissionInterface $fileSubmission ): DeeplRequestHandlerInterface { return new DeeplFileTranslationStatusRequestHandler( - $this->authKey, $this->streamFactory, - $fileSubmission + $fileSubmission, ); } @@ -84,33 +72,29 @@ public function createDeeplFileTranslationRequestHandler( FileSubmissionInterface $fileSubmission ): DeeplRequestHandlerInterface { return new DeeplFileRequestHandler( - $this->authKey, $this->streamFactory, - $fileSubmission + $fileSubmission, ); } public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequestHandlerInterface { return new DeeplSupportedLanguageRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface { return new DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface { return new DeeplGlossariesListRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } @@ -118,9 +102,8 @@ public function createDeeplGlossaryCreateRequestHandler( GlossarySubmissionInterface $submission ): DeeplRequestHandlerInterface { return new DeeplGlossaryCreateRequestHandler( - $this->authKey, $this->streamFactory, - $submission + $submission, ); } @@ -128,38 +111,26 @@ public function createDeeplGlossaryRetrieveRequestHandler( GlossaryIdSubmissionInterface $submission ): DeeplRequestHandlerInterface { return new DeeplGlossaryRetrieveRequestHandler( - $this->authKey, $this->streamFactory, - $submission + $submission, ); } public function createDeeplGlossaryDeleteRequestHandler( - GlossaryIdSubmissionInterface $submission + GlossaryIdSubmissionInterface $submission, ): DeeplRequestHandlerInterface { return new DeeplGlossaryDeleteRequestHandler( - $this->authKey, $this->streamFactory, - $submission + $submission, ); } public function createDeeplGlossaryEntriesRetrieveRequestHandler( - GlossaryIdSubmissionInterface $submission + GlossaryIdSubmissionInterface $submission, ): DeeplRequestHandlerInterface { return new DeeplGlossaryEntriesRetrieveRequestHandler( - $this->authKey, $this->streamFactory, - $submission + $submission, ); } - - public function getDeeplBaseUri(): string - { - if (strpos($this->authKey, ':fx') !== false) { - return DeeplRequestFactory::DEEPL_FREE_BASE_URI; - } - - return DeeplRequestFactory::DEEPL_PAID_BASE_URI; - } } diff --git a/src/Handler/DeeplRequestFactoryInterface.php b/src/Handler/DeeplRequestFactoryInterface.php index af2c77b..f8ef022 100644 --- a/src/Handler/DeeplRequestFactoryInterface.php +++ b/src/Handler/DeeplRequestFactoryInterface.php @@ -54,6 +54,4 @@ public function createDeeplGlossaryDeleteRequestHandler( public function createDeeplGlossaryEntriesRetrieveRequestHandler( GlossaryIdSubmissionInterface $submission ): DeeplRequestHandlerInterface; - - public function getDeeplBaseUri(): string; } diff --git a/src/Handler/DeeplRequestHandlerInterface.php b/src/Handler/DeeplRequestHandlerInterface.php index de350e6..5f94937 100644 --- a/src/Handler/DeeplRequestHandlerInterface.php +++ b/src/Handler/DeeplRequestHandlerInterface.php @@ -18,7 +18,5 @@ public function getBody(): StreamInterface; public function getContentType(): string; - public function getAuthHeader(): ?string; - public function getAcceptHeader(): ?string; } diff --git a/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php b/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php index a3e495b..4360363 100644 --- a/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php +++ b/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php @@ -11,15 +11,11 @@ final class DeeplSupportedLanguageRetrievalRequestHandler extends AbstractDeeplH { public const API_ENDPOINT = '/v2/languages?type=target'; - private string $authKey; - private StreamFactoryInterface $streamFactory; public function __construct( - string $authKey, - StreamFactoryInterface $streamFactory + StreamFactoryInterface $streamFactory, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; } @@ -35,19 +31,6 @@ public function getPath(): string public function getBody(): StreamInterface { - return $this->streamFactory->createStream( - http_build_query( - array_filter( - [ - 'auth_key' => $this->authKey, - ] - ) - ) - ); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; + return $this->streamFactory->createStream(); } } diff --git a/src/Handler/DeeplTranslationRequestHandler.php b/src/Handler/DeeplTranslationRequestHandler.php index d915bb1..8116f5b 100644 --- a/src/Handler/DeeplTranslationRequestHandler.php +++ b/src/Handler/DeeplTranslationRequestHandler.php @@ -13,18 +13,14 @@ final class DeeplTranslationRequestHandler extends AbstractDeeplHandler private const SEPARATOR = ','; public const API_ENDPOINT = '/v2/translate'; - private string $authKey; - private StreamFactoryInterface $streamFactory; private TranslationConfigInterface $translation; public function __construct( - string $authKey, StreamFactoryInterface $streamFactory, - TranslationConfigInterface $translation + TranslationConfigInterface $translation, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; $this->translation = $translation; } @@ -42,33 +38,28 @@ public function getPath(): string public function getBody(): StreamInterface { return $this->streamFactory->createStream( - http_build_query( + json_encode( array_filter( [ - 'text' => $this->translation->getText(), + 'text' => [$this->translation->getText()], 'target_lang' => $this->translation->getTargetLang(), 'source_lang' => $this->translation->getSourceLang(), 'tag_handling' => implode( self::SEPARATOR, - $this->translation->getTagHandling() + $this->translation->getTagHandling(), ), 'non_splitting_tags' => implode( self::SEPARATOR, - $this->translation->getNonSplittingTags() + $this->translation->getNonSplittingTags(), ), 'ignore_tags' => implode(self::SEPARATOR, $this->translation->getIgnoreTags()), 'split_sentences' => $this->translation->getSplitSentences(), 'preserve_formatting' => $this->translation->getPreserveFormatting(), 'glossary_id' => $this->translation->getGlossaryId(), - 'auth_key' => $this->authKey, - ] - ) + ], + ), + JSON_THROW_ON_ERROR ) ); } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; - } } diff --git a/src/Handler/DeeplUsageRequestHandler.php b/src/Handler/DeeplUsageRequestHandler.php index 00c5933..303edac 100644 --- a/src/Handler/DeeplUsageRequestHandler.php +++ b/src/Handler/DeeplUsageRequestHandler.php @@ -11,15 +11,11 @@ final class DeeplUsageRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/usage'; - private string $authKey; - private StreamFactoryInterface $streamFactory; public function __construct( - string $authKey, - StreamFactoryInterface $streamFactory + StreamFactoryInterface $streamFactory, ) { - $this->authKey = $authKey; $this->streamFactory = $streamFactory; } @@ -35,19 +31,6 @@ public function getPath(): string public function getBody(): StreamInterface { - return $this->streamFactory->createStream( - http_build_query( - array_filter( - [ - 'auth_key' => $this->authKey, - ] - ) - ) - ); - } - - public function getContentType(): string - { - return 'application/x-www-form-urlencoded'; + return $this->streamFactory->createStream(); } } diff --git a/src/Model/SupportedLanguages.php b/src/Model/SupportedLanguages.php index 85fd66b..2129064 100644 --- a/src/Model/SupportedLanguages.php +++ b/src/Model/SupportedLanguages.php @@ -31,9 +31,9 @@ public function getLanguages(): array fn (stdClass $item): array => [ 'language_code' => $item->language, 'name' => $item->name, - 'supports_formality' => $item->supports_formality, + 'supports_formality' => $item->supports_formality ?? false, ], - $this->languages + $this->languages, ); } } diff --git a/tests/DeeplClientTest.php b/tests/DeeplClientTest.php index 60875d3..cbb2bbe 100644 --- a/tests/DeeplClientTest.php +++ b/tests/DeeplClientTest.php @@ -4,8 +4,10 @@ namespace Scn\DeeplApiConnector; +use Generator; use GuzzleHttp\Exception\ClientException; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; @@ -41,6 +43,8 @@ class DeeplClientTest extends TestCase private RequestFactoryInterface&MockObject $requestFactory; + private string $apiKey = ''; + public function setUp(): void { $this->deeplRequestFactory = $this->createMock(DeeplRequestFactoryInterface::class); @@ -48,19 +52,21 @@ public function setUp(): void $this->requestFactory = $this->createMock(RequestFactoryInterface::class); $this->subject = new DeeplClient( + $this->apiKey, $this->deeplRequestFactory, $this->httpClient, - $this->requestFactory + $this->requestFactory, ); } - public function testGetUsageCanThrowException(): void + #[Test] + public function getUsageOnRequestException(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') - ->willReturn('some method'); + ->willReturn('some_method'); $requestHandler->method('getPath') - ->willReturn('some path'); + ->willReturn('/some/path'); $this->deeplRequestFactory->method('createDeeplUsageRequestHandler') ->willReturn($requestHandler); @@ -69,11 +75,12 @@ public function testGetUsageCanThrowException(): void $request = $this->createRequestExpectations( $requestHandler, - 'some method', - 'some path' + 'some_method', + '/some/path', + 'application/json', ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willThrowException($clientException); @@ -82,7 +89,8 @@ public function testGetUsageCanThrowException(): void $this->subject->getUsage(); } - public function testGetUsageCanReturnUsageModel(): void + #[Test] + public function getUsage(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -96,26 +104,27 @@ public function testGetUsageCanReturnUsageModel(): void $json = json_encode(['some string' => 'with value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') ->willReturn(['application/json']); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + 'application/json', ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -123,7 +132,8 @@ public function testGetUsageCanReturnUsageModel(): void self::assertInstanceOf(UsageInterface::class, $this->subject->getUsage()); } - public function testGetTranslationCanThrowException(): void + #[Test] + public function getTranslationOnRequestException(): void { $translation = $this->createMock(TranslationConfigInterface::class); @@ -141,10 +151,11 @@ public function testGetTranslationCanThrowException(): void $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + 'application/json', ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willThrowException($clientException); @@ -153,7 +164,8 @@ public function testGetTranslationCanThrowException(): void $this->subject->getTranslation($translation); } - public function testGetTranslationCanReturnTranslationModel(): void + #[Test] + public function getTranslation(): void { $translation = $this->createMock(TranslationConfigInterface::class); @@ -169,26 +181,29 @@ public function testGetTranslationCanReturnTranslationModel(): void $json = json_encode(['some string' => 'with value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -196,7 +211,8 @@ public function testGetTranslationCanReturnTranslationModel(): void self::assertInstanceOf(TranslationInterface::class, $this->subject->getTranslation($translation)); } - public function testTranslateCanReturnJsonEncodedObject(): void + #[Test] + public function translate(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -210,26 +226,28 @@ public function testTranslateCanReturnJsonEncodedObject(): void $json = json_encode(['some string' => 'with value', 'some other string' => ['more text' => 'more values']]); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $contentType = 'application/json'; + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + 'application/json' ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -237,7 +255,8 @@ public function testTranslateCanReturnJsonEncodedObject(): void self::assertInstanceOf(TranslationInterface::class, $this->subject->translate('some text', 'some language')); } - public function testTranslateBatchPerformsBatchTranslations(): void + #[Test] + public function translateBatch(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $stream = $this->createMock(StreamInterface::class); @@ -251,25 +270,27 @@ public function testTranslateBatchPerformsBatchTranslations(): void $this->deeplRequestFactory->method('createDeeplBatchTranslationRequestHandler') ->willReturn($requestHandler); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn(json_encode(['translations' => ['content']])); - $response->expects($this->once()) + $contentType = 'application/json'; + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -277,7 +298,8 @@ public function testTranslateBatchPerformsBatchTranslations(): void self::assertInstanceOf(BatchTranslationInterface::class, $this->subject->translateBatch(['some text'], 'some language')); } - public function testTranslateFileCanReturnInstanceOfResponseModel(): void + #[Test] + public function translateFile(): void { $fileTranslation = $this->createMock(FileTranslationConfigInterface::class); @@ -293,26 +315,28 @@ public function testTranslateFileCanReturnInstanceOfResponseModel(): void $json = json_encode(['some string' => 'with value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -320,7 +344,8 @@ public function testTranslateFileCanReturnInstanceOfResponseModel(): void self::assertInstanceOf(FileSubmissionInterface::class, $this->subject->translateFile($fileTranslation)); } - public function testGetFileTranslationStatusCanReturnInstanceOfResponseModel(): void + #[Test] + public function getFileTranslationStatus(): void { $fileSubmission = $this->createMock(FileSubmissionInterface::class); @@ -336,26 +361,28 @@ public function testGetFileTranslationStatusCanReturnInstanceOfResponseModel(): $json = json_encode(['some string' => 'with value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -363,7 +390,8 @@ public function testGetFileTranslationStatusCanReturnInstanceOfResponseModel(): self::assertInstanceOf(FileTranslationStatusInterface::class, $this->subject->getFileTranslationStatus($fileSubmission)); } - public function testGetFileTranslationCanReturnInstanceOfResponseModel(): void + #[Test] + public function getFileTranslation(): void { $fileSubmission = $this->createMock(FileSubmissionInterface::class); @@ -375,26 +403,28 @@ public function testGetFileTranslationCanReturnInstanceOfResponseModel(): void $json = json_encode(['some string' => 'with value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['plain/text']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -402,17 +432,15 @@ public function testGetFileTranslationCanReturnInstanceOfResponseModel(): void self::assertInstanceOf(FileTranslationInterface::class, $this->subject->getFileTranslation($fileSubmission)); } - public static function errorStatusCodeProvider(): array + public static function errorStatusCodeProvider(): Generator { - return [ - [404], - [403], - [500], - ]; + yield [404]; + yield [403]; + yield [500]; } - #[DataProvider(methodName: 'errorStatusCodeProvider')] - public function testResponseWithErrorStatusCodeTriggersError(int $statusCode): void + #[Test, DataProvider(methodName: 'errorStatusCodeProvider')] + public function responseWithErrorStatusCodeTriggersError(int $statusCode): void { $response = $this->createMock(ResponseInterface::class); $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); @@ -428,15 +456,16 @@ public function testResponseWithErrorStatusCodeTriggersError(int $statusCode): v $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + 'application/json', ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getStatusCode') ->willReturn($statusCode); @@ -446,7 +475,8 @@ public function testResponseWithErrorStatusCodeTriggersError(int $statusCode): v $this->subject->getUsage(); } - public function testGetSupportedLanguagesReturnsSupportedLanguagesModel(): void + #[Test] + public function getSupportedLanguages(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -460,26 +490,28 @@ public function testGetSupportedLanguagesReturnsSupportedLanguagesModel(): void $json = json_encode(['with value', 'some-other value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -487,7 +519,8 @@ public function testGetSupportedLanguagesReturnsSupportedLanguagesModel(): void self::assertInstanceOf(SupportedLanguages::class, $this->subject->getSupportedLanguages()); } - public function testGetGlossariesSupportedLanguagesPairsGetCorrectModel(): void + #[Test] + public function getGlossariesSupportedLanguagesPairs(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -501,26 +534,28 @@ public function testGetGlossariesSupportedLanguagesPairsGetCorrectModel(): void $json = json_encode(['supported_languages' => ['with value', 'some-other value']]); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -528,7 +563,8 @@ public function testGetGlossariesSupportedLanguagesPairsGetCorrectModel(): void self::assertInstanceOf(GlossariesSupportedLanguagesPairs::class, $this->subject->getGlossariesSupportedLanguagesPairs()); } - public function testGetGlossariesListGetCorrectModel(): void + #[Test] + public function getGlossariesList(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -542,26 +578,28 @@ public function testGetGlossariesListGetCorrectModel(): void $json = json_encode(['glossaries' => ['with value', 'some-other value']]); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -569,7 +607,8 @@ public function testGetGlossariesListGetCorrectModel(): void self::assertInstanceOf(Glossaries::class, $this->subject->getGlossariesList()); } - public function testCreateGlossaryGetCorrectModel(): void + #[Test] + public function createGlossary(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -583,26 +622,28 @@ public function testCreateGlossaryGetCorrectModel(): void $json = json_encode(['with value', 'some-other value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -612,7 +653,8 @@ public function testCreateGlossaryGetCorrectModel(): void self::assertInstanceOf(Glossary::class, $this->subject->createGlossary($submission)); } - public function testRetrieveGlossaryGetCorrectModel(): void + #[Test] + public function retrieveGlossary(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -626,26 +668,28 @@ public function testRetrieveGlossaryGetCorrectModel(): void $json = json_encode(['with value', 'some-other value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -654,7 +698,8 @@ public function testRetrieveGlossaryGetCorrectModel(): void self::assertInstanceOf(Glossary::class, $this->subject->retrieveGlossary($submission)); } - public function testDeleteGlossaryGetCorrectBoolean(): void + #[Test] + public function deleteGlossary(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -668,26 +713,28 @@ public function testDeleteGlossaryGetCorrectBoolean(): void $json = json_encode(['with value', 'some-other value']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -696,7 +743,8 @@ public function testDeleteGlossaryGetCorrectBoolean(): void self::assertTrue($this->subject->deleteGlossary($submission)); } - public function testRetrieveGlossaryEntriesGetCorrectModel(): void + #[Test] + public function retrieveGlossaryEntries(): void { $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); $requestHandler->method('getMethod') @@ -710,26 +758,28 @@ public function testRetrieveGlossaryEntriesGetCorrectModel(): void $json = json_encode(['content' => 'test']); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once()) + $stream->expects(self::once()) ->method('getContents') ->willReturn($json); + $contentType = 'application/json'; $response = $this->createMock(ResponseInterface::class); - $response->expects($this->once()) + $response->expects(self::once()) ->method('getHeader') ->with('Content-Type') - ->willReturn(['application/json']); - $response->expects($this->once()) + ->willReturn([$contentType]); + $response->expects(self::once()) ->method('getBody') ->willReturn($stream); $request = $this->createRequestExpectations( $requestHandler, 'some method', - 'some path' + 'some path', + $contentType, ); - $this->httpClient->expects($this->once()) + $this->httpClient->expects(self::once()) ->method('sendRequest') ->with($request) ->willReturn($response); @@ -741,40 +791,45 @@ public function testRetrieveGlossaryEntriesGetCorrectModel(): void private function createRequestExpectations( MockObject $requestHandler, string $method, - string $path + string $path, + string $contentType, ): MockObject { - $base_uri = 'http://something'; + $base_uri = 'https://api.deepl.com'; $request = $this->createMock(RequestInterface::class); $stream = $this->createMock(StreamInterface::class); - $contentType = 'some-content-type'; - $requestHandler->expects($this->once()) + $requestHandler->expects(self::once()) ->method('getMethod') ->willReturn('some method'); - $requestHandler->expects($this->once()) + $requestHandler->expects(self::once()) ->method('getPath') ->willReturn('some path'); - $requestHandler->expects($this->once()) + $requestHandler->expects(self::once()) ->method('getBody') ->willReturn($stream); - $requestHandler->expects($this->once()) + $requestHandler->expects(self::once()) ->method('getContentType') ->willReturn($contentType); - $this->requestFactory->expects($this->once()) + $this->requestFactory->expects(self::once()) ->method('createRequest') ->with($method, sprintf('%s%s', $base_uri, $path)) ->willReturn($request); - $this->deeplRequestFactory->expects($this->once()) - ->method('getDeeplBaseUri') - ->willReturn($base_uri); - - $request->expects($this->once()) + $callNr = 1; + $request->expects(self::exactly(2)) ->method('withHeader') - ->with('Content-Type', $contentType) + ->with(self::callback( + function (...$header) use (&$callNr, $contentType): bool { + return match ($callNr++) { + 1 => ($header[0] === 'Authorization' && $header[1] ==='DeepL-Auth-Key '.$this->apiKey), + 2 => ($header[0] === 'Content-Type' && $header[1] === $contentType), + }; + } + )) ->willReturnSelf(); - $request->expects($this->once()) + + $request->expects(self::once()) ->method('withBody') ->with($stream) ->willReturnSelf(); diff --git a/tests/Handler/DeeplBatchTranslationRequestHandlerTest.php b/tests/Handler/DeeplBatchTranslationRequestHandlerTest.php index 07b6bfd..d4d43aa 100644 --- a/tests/Handler/DeeplBatchTranslationRequestHandlerTest.php +++ b/tests/Handler/DeeplBatchTranslationRequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; @@ -15,11 +16,9 @@ class DeeplBatchTranslationRequestHandlerTest extends TestCase { private DeeplBatchTranslationRequestHandler $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; - /** @var BatchTranslationConfigInterface|MockObject */ - private $translation; + private BatchTranslationConfigInterface&MockObject $translation; protected function setUp(): void { @@ -27,60 +26,57 @@ protected function setUp(): void $this->translation = $this->createMock(BatchTranslationConfigInterface::class); $this->subject = new DeeplBatchTranslationRequestHandler( - 'some key', $this->streamFactory, - $this->translation + $this->translation, ); } public function testGetPathCanReturnPath(): void { - static::assertSame(DeeplTranslationRequestHandler::API_ENDPOINT, $this->subject->getPath()); + self::assertSame(DeeplTranslationRequestHandler::API_ENDPOINT, $this->subject->getPath()); } - public function testGetBodyCanReturnFilteredArray(): void + #[Test] + public function getBodyFiltered(): void { - $text1 = 'some-text'; - $text2 = 'some-other-text'; - $stream = $this->createMock(StreamInterface::class); - $query = http_build_query( + $query = json_encode( [ - 'target_lang' => 'some target language', - 'auth_key' => 'some key', - ] + 'target_lang' => $lang = 'DE', + 'text' => [ + $text1 = 'some-text', + $text2 = 'some-other-text', + ]], ); - $query .= sprintf('&text=%s', $text1); - $query .= sprintf('&text=%s', $text2); - - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getText') ->willReturn([$text1, $text2]); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTargetLang') - ->willReturn('some target language'); + ->willReturn($lang); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with($query) ->willReturn($stream); - static::assertSame( + self::assertSame( $stream, - $this->subject->getBody() + $this->subject->getBody(), ); } - public function testGetBodyCanReturnArrayWithOptionalTags(): void + #[Test] + public function getBodyWithOptionalTags(): void { $stream = $this->createMock(StreamInterface::class); $text = 'some text to translate'; - $query = http_build_query( + $body = json_encode( [ 'target_lang' => 'some target language', 'tag_handling' => 'a,b', @@ -88,61 +84,61 @@ public function testGetBodyCanReturnArrayWithOptionalTags(): void 'ignore_tags' => 'ef,fa,qa', 'split_sentences' => '1', 'preserve_formatting' => '1', - 'auth_key' => 'some key', + 'text' => [$text], ] ); - $query .= sprintf('&text=%s', $text); - - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getText') ->willReturn([$text]); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTargetLang') ->willReturn('some target language'); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTagHandling') ->willReturn(['a', 'b']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getNonSplittingTags') ->willReturn(['b', 'a', 'c']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getIgnoreTags') ->willReturn(['ef', 'fa', 'qa']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getSplitSentences') ->willReturn(TextHandlingEnum::SPLITSENTENCES_ON); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getPreserveFormatting') ->willReturn(TextHandlingEnum::PRESERVEFORMATTING_ON); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') - ->with($query) + ->with($body) ->willReturn($stream); - static::assertSame( + self::assertSame( $stream, - $this->subject->getBody() + $this->subject->getBody(), ); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { - static::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); + self::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } } diff --git a/tests/Handler/DeeplFileRequestHandlerTest.php b/tests/Handler/DeeplFileRequestHandlerTest.php index e9f4514..343beaa 100644 --- a/tests/Handler/DeeplFileRequestHandlerTest.php +++ b/tests/Handler/DeeplFileRequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; @@ -12,18 +13,11 @@ class DeeplFileRequestHandlerTest extends TestCase { - /** - * @var DeeplFileRequestHandler - */ - private $subject; + private DeeplFileRequestHandler $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; - /** - * @var FileSubmissionInterface|MockObject - */ - private $fileSubmission; + private FileSubmissionInterface&MockObject $fileSubmission; public function setUp(): void { @@ -31,50 +25,48 @@ public function setUp(): void $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplFileRequestHandler( - 'some key', $this->streamFactory, - $this->fileSubmission + $this->fileSubmission, ); } - public function testGetPathCanReturnPath(): void + #[Test] + public function getPath(): void { - $this->fileSubmission->expects($this->once()) + $this->fileSubmission->expects(self::once()) ->method('getDocumentId') ->willReturn('documentId'); self::assertSame(sprintf(DeeplFileRequestHandler::API_ENDPOINT, 'documentId'), $this->subject->getPath()); } - public function testGetBodyCanReturnFilteredArray(): void + #[Test] + public function getBodyFiltered(): void { $stream = $this->createMock(StreamInterface::class); - $this->fileSubmission->expects($this->once()) + $this->fileSubmission->expects(self::once()) ->method('getDocumentKey') ->willReturn('document key'); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with( - http_build_query( - [ - 'auth_key' => 'some key', - 'document_key' => 'document key', - ] - ) + json_encode(['document_key' => 'document key']), ) ->willReturn($stream); self::assertSame($stream, $this->subject->getBody()); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { self::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - self::assertSame('application/x-www-form-urlencoded', $this->subject->getContentType()); + self::assertSame('application/json', $this->subject->getContentType()); } } diff --git a/tests/Handler/DeeplFileSubmissionRequestHandlerTest.php b/tests/Handler/DeeplFileSubmissionRequestHandlerTest.php index 8004bfa..23e4bab 100644 --- a/tests/Handler/DeeplFileSubmissionRequestHandlerTest.php +++ b/tests/Handler/DeeplFileSubmissionRequestHandlerTest.php @@ -5,6 +5,7 @@ namespace Scn\DeeplApiConnector\Handler; use Http\Message\MultipartStream\MultipartStreamBuilder; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; @@ -12,121 +13,117 @@ class DeeplFileSubmissionRequestHandlerTest extends TestCase { - /** - * @var DeeplFileRequestHandler - */ - private $subject; + private DeeplFileSubmissionRequestHandler $subject; - /** @var MultipartStreamBuilder|MockObject */ - private $streamBuilder; + private MultipartStreamBuilder&MockObject $streamBuilder; - /** - * @var FileTranslationConfigInterface|MockObject - */ - private $fileTranslation; + private FileTranslationConfigInterface&MockObject $fileTranslation; public function setUp(): void { - $this->streamBuilder = $this->createMock(MultipartStreamBuilder::class); $this->fileTranslation = $this->createMock(FileTranslationConfigInterface::class); + $this->streamBuilder = $this->createMock(MultipartStreamBuilder::class); $this->subject = new DeeplFileSubmissionRequestHandler( - 'some key', $this->fileTranslation, - $this->streamBuilder + $this->streamBuilder, ); } - public function testGetPathCanReturnPath(): void + #[Test] + public function getPath(): void { self::assertSame(DeeplFileSubmissionRequestHandler::API_ENDPOINT, $this->subject->getPath()); } - public function testGetBodyCanReturnFilteredArray(): void + #[Test] + public function getBodyFiltered(): void { $stream = $this->createMock(StreamInterface::class); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getFileName') ->willReturn('file name'); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getFileContent') ->willReturn('file content'); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getSourceLang') ->willReturn('source lang'); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getTargetLang') ->willReturn('target lang'); - $this->streamBuilder->expects($this->once()) + $this->streamBuilder->expects(self::once()) ->method('setBoundary') ->with('boundary') ->willReturnSelf(); - $this->streamBuilder->expects($this->exactly(4)) + + $this->streamBuilder->expects(self::exactly(3)) ->method('addResource') ->willReturnOnConsecutiveCalls( - ['auth_key', 'some key'], ['file', 'file content', ['filename' => 'file name']], ['target_lang', 'target lang'], - ['source_lang', 'source lang'] + ['source_lang', 'source lang'], ) ->willReturnSelf(); - $this->streamBuilder->expects($this->once()) + $this->streamBuilder->expects(self::once()) ->method('build') ->willReturn($stream); self::assertSame($stream, $this->subject->getBody()); } - public function testGetBodyIgnoresSourceLangIfEmpty(): void + #[Test] + public function getBodyOnIgnoresSourceLangIfEmpty(): void { $stream = $this->createMock(StreamInterface::class); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getFileName') ->willReturn('file name'); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getFileContent') ->willReturn('file content'); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getSourceLang') ->willReturn(''); - $this->fileTranslation->expects($this->once()) + $this->fileTranslation->expects(self::once()) ->method('getTargetLang') ->willReturn('target lang'); - $this->streamBuilder->expects($this->once()) + $this->streamBuilder->expects(self::once()) ->method('setBoundary') ->with('boundary') ->willReturnSelf(); - $this->streamBuilder->expects($this->exactly(3)) + $this->streamBuilder->expects(self::exactly(2)) ->method('addResource') ->willReturnOnConsecutiveCalls( - ['auth_key', 'some key'], ['file', 'file content', ['filename' => 'file name']], - ['target_lang', 'target lang'] + ['target_lang', 'target lang'], ) ->willReturnSelf(); - $this->streamBuilder->expects($this->once()) + $this->streamBuilder->expects(self::once()) ->method('build') ->willReturn($stream); self::assertSame($stream, $this->subject->getBody()); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { self::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { self::assertSame('multipart/form-data;boundary="boundary"', $this->subject->getContentType()); } diff --git a/tests/Handler/DeeplFileTranslationStatusRequestHandlerTest.php b/tests/Handler/DeeplFileTranslationStatusRequestHandlerTest.php index 0f0002b..8e5e66b 100644 --- a/tests/Handler/DeeplFileTranslationStatusRequestHandlerTest.php +++ b/tests/Handler/DeeplFileTranslationStatusRequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; @@ -12,18 +13,11 @@ class DeeplFileTranslationStatusRequestHandlerTest extends TestCase { - /** - * @var DeeplFileTranslationStatusRequestHandler - */ - private $subject; + private DeeplFileTranslationStatusRequestHandler $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; - /** - * @var FileSubmissionInterface|MockObject - */ - private $fileSubmission; + private FileSubmissionInterface&MockObject $fileSubmission; public function setUp(): void { @@ -31,50 +25,48 @@ public function setUp(): void $this->fileSubmission = $this->createMock(FileSubmissionInterface::class); $this->subject = new DeeplFileTranslationStatusRequestHandler( - 'some key', $this->streamFactory, - $this->fileSubmission + $this->fileSubmission, ); } + #[Test] public function testGetPathCanReturnPath(): void { - $this->fileSubmission->expects($this->once()) + $this->fileSubmission->expects(self::once()) ->method('getDocumentId') ->willReturn('documentId'); self::assertSame(sprintf(DeeplFileTranslationStatusRequestHandler::API_ENDPOINT, 'documentId'), $this->subject->getPath()); } - public function testGetBodyCanReturnFilteredArray(): void + #[Test] + public function getBody(): void { $stream = $this->createMock(StreamInterface::class); - $this->fileSubmission->expects($this->once()) + $this->fileSubmission->expects(self::once()) ->method('getDocumentKey') ->willReturn('document key'); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with( - http_build_query( - [ - 'auth_key' => 'some key', - 'document_key' => 'document key', - ] - ) + json_encode(['document_key' => 'document key']), ) ->willReturn($stream); self::assertSame($stream, $this->subject->getBody()); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { self::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - self::assertSame('application/x-www-form-urlencoded', $this->subject->getContentType()); + self::assertSame('application/json', $this->subject->getContentType()); } } diff --git a/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php b/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php index 74ab0f7..fe98aa6 100644 --- a/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php @@ -4,15 +4,15 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\TestCase; class DeeplGlossariesListRetrievalRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; - - private StreamFactoryInterface $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; private DeeplGlossariesListRetrievalRequestHandler $subject; @@ -21,55 +21,50 @@ protected function setUp(): void $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplGlossariesListRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( - DeeplRequestHandlerInterface::METHOD_GET, - $this->subject->getMethod() + self::assertSame( + 'GET', + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - static::assertSame( + self::assertSame( '/v2/glossaries', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php b/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php index 5d19c82..f3aa138 100644 --- a/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php @@ -4,15 +4,15 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\TestCase; class DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; - - private StreamFactoryInterface $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; private DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler $subject; @@ -21,55 +21,50 @@ protected function setUp(): void $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_GET, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - static::assertSame( + self::assertSame( '/v2/glossary-language-pairs', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php b/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php index 0f60c94..899beaa 100644 --- a/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php @@ -4,6 +4,8 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\GlossarySubmission; @@ -11,76 +13,67 @@ class DeeplGlossaryCreateRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; + private StreamFactoryInterface&MockObject $streamFactory; - private StreamFactoryInterface $streamFactory; + private GlossarySubmission&MockObject $submission; private DeeplGlossaryCreateRequestHandler $subject; - private GlossarySubmission $submission; - protected function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->submission = $this->createMock(GlossarySubmission::class); $this->subject = new DeeplGlossaryCreateRequestHandler( - $this->authKey, $this->streamFactory, $this->submission, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_POST, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - static::assertSame( + self::assertSame( '/v2/glossaries', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->submission->expects($this->once()) + $this->submission->expects(self::once()) ->method('toArrayRequest') - ->willReturn([ - 'test' => 'test1', - ]); + ->willReturn(['test' => 'test1']); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php b/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php index 934afaa..d1488ac 100644 --- a/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php @@ -4,6 +4,8 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; @@ -11,75 +13,68 @@ class DeeplGlossaryDeleteRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; + private StreamFactoryInterface&MockObject $streamFactory; - private StreamFactoryInterface $streamFactory; + private GlossaryIdSubmission&MockObject $submission; private DeeplGlossaryDeleteRequestHandler $subject; - private GlossaryIdSubmission $submission; - protected function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->submission = $this->createMock(GlossaryIdSubmission::class); $this->subject = new DeeplGlossaryDeleteRequestHandler( - $this->authKey, $this->streamFactory, $this->submission, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_DELETE, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - $this->submission->expects($this->once()) + $this->submission->expects(self::once()) ->method('getId') ->with() ->willReturn('1'); - static::assertSame( + self::assertSame( '/v2/glossaries/1', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php b/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php index b36964e..f188f91 100644 --- a/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php @@ -4,6 +4,8 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; @@ -11,83 +13,76 @@ class DeeplGlossaryEntriesRetrieveRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; + private StreamFactoryInterface&MockObject $streamFactory; - private StreamFactoryInterface $streamFactory; + private GlossaryIdSubmission&MockObject $submission; private DeeplGlossaryEntriesRetrieveRequestHandler $subject; - private GlossaryIdSubmission $submission; - protected function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->submission = $this->createMock(GlossaryIdSubmission::class); $this->subject = new DeeplGlossaryEntriesRetrieveRequestHandler( - $this->authKey, $this->streamFactory, $this->submission, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_GET, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - $this->submission->expects($this->once()) + $this->submission->expects(self::once()) ->method('getId') ->with() ->willReturn('1'); - static::assertSame( + self::assertSame( '/v2/glossaries/1/entries', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetAcceptHeader(): void + #[Test] + public function getAcceptHeader(): void { - static::assertSame( + self::assertSame( 'text/tab-separated-values', - $this->subject->getAcceptHeader() + $this->subject->getAcceptHeader(), ); } - public function testGetBodyReturnsValue(): void + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php b/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php index 06098f0..171a5da 100644 --- a/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php +++ b/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php @@ -4,6 +4,8 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; @@ -11,75 +13,68 @@ class DeeplGlossaryRetrieveRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; + private StreamFactoryInterface&MockObject $streamFactory; - private StreamFactoryInterface $streamFactory; + private GlossaryIdSubmission&MockObject $submission; private DeeplGlossaryRetrieveRequestHandler $subject; - private GlossaryIdSubmission $submission; - protected function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->submission = $this->createMock(GlossaryIdSubmission::class); $this->subject = new DeeplGlossaryRetrieveRequestHandler( - $this->authKey, $this->streamFactory, $this->submission, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_GET, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - $this->submission->expects($this->once()) + $this->submission->expects(self::once()) ->method('getId') ->with() ->willReturn('1'); - static::assertSame( + self::assertSame( '/v2/glossaries/1', - $this->subject->getPath() - ); - } - - public function testGetContentTypeReturnsValue(): void - { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + $this->subject->getPath(), ); } - public function testGetAuthHeader(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'DeepL-Auth-Key some-auth-key', - $this->subject->getAuthHeader() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with() ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplRequestFactoryTest.php b/tests/Handler/DeeplRequestFactoryTest.php index 95d56f4..e7e8db4 100644 --- a/tests/Handler/DeeplRequestFactoryTest.php +++ b/tests/Handler/DeeplRequestFactoryTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Scn\DeeplApiConnector\Model\BatchTranslationConfigInterface; @@ -18,107 +19,147 @@ class DeeplRequestFactoryTest extends TestCase { private DeeplRequestFactory $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; protected function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplRequestFactory( - 'some api key', - $this->streamFactory + $this->streamFactory, ); } - public function testCreateDeeplUsageRequestHandlerCanReturnInstanceOfUsageRequestHandler(): void + #[Test] + public function createDeeplUsageRequestHandler(): void { - self::assertInstanceOf(DeeplUsageRequestHandler::class, $this->subject->createDeeplUsageRequestHandler()); - } - - public function testCreateDeeplTranslationRequestHandler(): void - { - $translation = $this->createMock(TranslationConfigInterface::class); - self::assertInstanceOf(DeeplTranslationRequestHandler::class, $this->subject->createDeeplTranslationRequestHandler($translation)); - } - - public function testCreateDeeplBatchTranslationRequestHandler(): void - { - $translation = $this->createMock(BatchTranslationConfigInterface::class); - - self::assertInstanceOf(DeeplBatchTranslationRequestHandler::class, $this->subject->createDeeplBatchTranslationRequestHandler($translation)); + self::assertInstanceOf( + DeeplUsageRequestHandler::class, + $this->subject->createDeeplUsageRequestHandler(), + ); } - public function testCreateDeeplFileSubmissionRequestHandler(): void + #[Test] + public function createDeeplTranslationRequestHandler(): void { - $fileTranslation = $this->createMock(FileTranslationConfigInterface::class); - self::assertInstanceOf(DeeplFileSubmissionRequestHandler::class, $this->subject->createDeeplFileSubmissionRequestHandler($fileTranslation)); + self::assertInstanceOf( + DeeplTranslationRequestHandler::class, + $this->subject->createDeeplTranslationRequestHandler( + $this->createMock(TranslationConfigInterface::class), + ), + ); } - public function testCreateDeeplFileTranslationStatusRequestHandler(): void + #[Test] + public function createDeeplBatchTranslationRequestHandler(): void { - $fileSubmission = $this->createMock(FileSubmissionInterface::class); - self::assertInstanceOf(DeeplFileTranslationStatusRequestHandler::class, $this->subject->createDeeplFileTranslationStatusRequestHandler($fileSubmission)); + self::assertInstanceOf( + DeeplBatchTranslationRequestHandler::class, + $this->subject->createDeeplBatchTranslationRequestHandler( + $this->createMock(BatchTranslationConfigInterface::class), + ), + ); } - public function testCreateDeeplFileTranslationRequestHandler(): void + #[Test] + public function createDeeplFileSubmissionRequestHandler(): void { - $fileSubmission = $this->createMock(FileSubmissionInterface::class); - self::assertInstanceOf(DeeplFileRequestHandler::class, $this->subject->createDeeplFileTranslationRequestHandler($fileSubmission)); + self::assertInstanceOf( + DeeplFileSubmissionRequestHandler::class, + $this->subject->createDeeplFileSubmissionRequestHandler( + $this->createMock(FileTranslationConfigInterface::class), + ), + ); } - public function testCreateDeeplSupportedLanguageRetrievalRequestHandler(): void + #[Test] + public function createDeeplFileTranslationStatusRequestHandler(): void { - self::assertInstanceOf(DeeplSupportedLanguageRetrievalRequestHandler::class, $this->subject->createDeeplSupportedLanguageRetrievalRequestHandler()); + self::assertInstanceOf( + DeeplFileTranslationStatusRequestHandler::class, + $this->subject->createDeeplFileTranslationStatusRequestHandler( + $this->createMock(FileSubmissionInterface::class), + ), + ); } - public function testGetDeeplBaseUriCanReturnPaidBaseUri(): void + #[Test] + public function createDeeplFileTranslationRequestHandler(): void { - self::assertSame(DeeplRequestFactory::DEEPL_PAID_BASE_URI, $this->subject->getDeeplBaseUri()); + self::assertInstanceOf( + DeeplFileRequestHandler::class, + $this->subject->createDeeplFileTranslationRequestHandler( + $this->createMock(FileSubmissionInterface::class), + ), + ); } - public function testGetDeeplFreeUriCanReturnFreeBaseUri(): void + #[Test] + public function createDeeplSupportedLanguageRetrievalRequestHandler(): void { - $this->subject = new DeeplRequestFactory('something:fx', $this->streamFactory); - self::assertSame(DeeplRequestFactory::DEEPL_FREE_BASE_URI, $this->subject->getDeeplBaseUri()); + self::assertInstanceOf( + DeeplSupportedLanguageRetrievalRequestHandler::class, + $this->subject->createDeeplSupportedLanguageRetrievalRequestHandler(), + ); } - public function testCreateDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): void + #[Test] + public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): void { - self::assertInstanceOf(DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler::class, $this->subject->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler()); + self::assertInstanceOf( + DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler::class, + $this->subject->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(), + ); } - public function testCreateDeeplGlossariesListRetrievalRequestHandler(): void + #[Test] + public function createDeeplGlossariesListRetrievalRequestHandler(): void { - self::assertInstanceOf(DeeplGlossariesListRetrievalRequestHandler::class, $this->subject->createDeeplGlossariesListRetrievalRequestHandler()); + self::assertInstanceOf( + DeeplGlossariesListRetrievalRequestHandler::class, + $this->subject->createDeeplGlossariesListRetrievalRequestHandler(), + ); } public function testCreateDeeplGlossaryCreateRequestHandler(): void { - $submission = $this->createMock(GlossarySubmission::class); - - self::assertInstanceOf(DeeplGlossaryCreateRequestHandler::class, $this->subject->createDeeplGlossaryCreateRequestHandler($submission)); + self::assertInstanceOf( + DeeplGlossaryCreateRequestHandler::class, + $this->subject->createDeeplGlossaryCreateRequestHandler( + $this->createMock(GlossarySubmission::class), + ), + ); } - public function testCreateDeeplGlossaryRetrieveRequestHandler(): void + #[Test] + public function createDeeplGlossaryRetrieveRequestHandler(): void { - $submission = $this->createMock(GlossaryIdSubmission::class); - - self::assertInstanceOf(DeeplGlossaryRetrieveRequestHandler::class, $this->subject->createDeeplGlossaryRetrieveRequestHandler($submission)); + self::assertInstanceOf( + DeeplGlossaryRetrieveRequestHandler::class, + $this->subject->createDeeplGlossaryRetrieveRequestHandler( + $this->createMock(GlossaryIdSubmission::class), + ), + ); } - public function testCreateDeeplGlossaryDeleteRequestHandler(): void + #[Test] + public function createDeeplGlossaryDeleteRequestHandler(): void { - $submission = $this->createMock(GlossaryIdSubmission::class); - - self::assertInstanceOf(DeeplGlossaryDeleteRequestHandler::class, $this->subject->createDeeplGlossaryDeleteRequestHandler($submission)); + self::assertInstanceOf( + DeeplGlossaryDeleteRequestHandler::class, + $this->subject->createDeeplGlossaryDeleteRequestHandler( + $this->createMock(GlossaryIdSubmission::class), + ), + ); } public function testCreateDeeplGlossaryEntriesRetrieveRequestHandler(): void { - $submission = $this->createMock(GlossaryIdSubmission::class); - - self::assertInstanceOf(DeeplGlossaryEntriesRetrieveRequestHandler::class, $this->subject->createDeeplGlossaryEntriesRetrieveRequestHandler($submission)); + self::assertInstanceOf( + DeeplGlossaryEntriesRetrieveRequestHandler::class, + $this->subject->createDeeplGlossaryEntriesRetrieveRequestHandler( + $this->createMock(GlossaryIdSubmission::class), + ) + ); } - //TODO tests } diff --git a/tests/Handler/DeeplSupportedLanguageRetrievalRequestHandlerTest.php b/tests/Handler/DeeplSupportedLanguageRetrievalRequestHandlerTest.php index 380cf5d..b0c9115 100644 --- a/tests/Handler/DeeplSupportedLanguageRetrievalRequestHandlerTest.php +++ b/tests/Handler/DeeplSupportedLanguageRetrievalRequestHandlerTest.php @@ -4,15 +4,15 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\TestCase; class DeeplSupportedLanguageRetrievalRequestHandlerTest extends TestCase { - private string $authKey = 'some-auth-key'; - - private StreamFactoryInterface $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; private DeeplSupportedLanguageRetrievalRequestHandler $subject; @@ -21,53 +21,49 @@ protected function setUp(): void $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplSupportedLanguageRetrievalRequestHandler( - $this->authKey, - $this->streamFactory + $this->streamFactory, ); } - public function testGetMethodReturnsValue(): void + #[Test] + public function getMethod(): void { - static::assertSame( + self::assertSame( DeeplRequestHandlerInterface::METHOD_GET, - $this->subject->getMethod() + $this->subject->getMethod(), ); } - public function testGetPathReturnsValue(): void + #[Test] + public function getPath(): void { - static::assertSame( + self::assertSame( '/v2/languages?type=target', - $this->subject->getPath() + $this->subject->getPath(), ); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - static::assertSame( - 'application/x-www-form-urlencoded', - $this->subject->getContentType() + self::assertSame( + 'application/json', + $this->subject->getContentType(), ); } - public function testGetBodyReturnsValue(): void + #[Test] + public function getBody(): void { $body = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') - ->with( - http_build_query( - [ - 'auth_key' => $this->authKey, - ] - ) - ) ->willReturn($body); - static::assertSame( + self::assertSame( $body, - $this->subject->getBody() + $this->subject->getBody(), ); } } diff --git a/tests/Handler/DeeplTranslationRequestHandlerTest.php b/tests/Handler/DeeplTranslationRequestHandlerTest.php index 5d4a8e2..c46c7d2 100644 --- a/tests/Handler/DeeplTranslationRequestHandlerTest.php +++ b/tests/Handler/DeeplTranslationRequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; @@ -13,18 +14,11 @@ class DeeplTranslationRequestHandlerTest extends TestCase { - /** - * @var DeeplTranslationRequestHandler - */ - private $subject; + private DeeplTranslationRequestHandler $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; - /** - * @var TranslationConfigInterface|MockObject - */ - private $translation; + private TranslationConfigInterface&MockObject $translation; public function setUp(): void { @@ -32,38 +26,38 @@ public function setUp(): void $this->translation = $this->createMock(TranslationConfigInterface::class); $this->subject = new DeeplTranslationRequestHandler( - 'some key', $this->streamFactory, - $this->translation + $this->translation, ); } - public function testGetPathCanReturnPath(): void + #[Test] + public function getPath(): void { self::assertSame(DeeplTranslationRequestHandler::API_ENDPOINT, $this->subject->getPath()); } - public function testGetBodyCanReturnFilteredArray(): void + #[Test] + public function getBodyFiltered(): void { $stream = $this->createMock(StreamInterface::class); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getText') ->willReturn('some text to translate'); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTargetLang') - ->willReturn('some target language'); + ->willReturn('DE'); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with( - http_build_query( + json_encode( [ - 'text' => 'some text to translate', - 'target_lang' => 'some target language', - 'auth_key' => 'some key', - ] + 'text' => ['some text to translate'], + 'target_lang' => 'DE', + ], ) ) ->willReturn($stream); @@ -71,57 +65,57 @@ public function testGetBodyCanReturnFilteredArray(): void self::assertSame($stream, $this->subject->getBody()); } - public function testGetBodyCanReturnArrayWithOptionalTags(): void + #[Test] + public function getBodyWithOptionalTags(): void { $stream = $this->createMock(StreamInterface::class); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getText') ->willReturn('some text to translate'); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTargetLang') - ->willReturn('some target language'); + ->willReturn('DE'); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getSourceLang') - ->willReturn('some source lang'); + ->willReturn('EN'); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getTagHandling') ->willReturn(['a', 'b']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getNonSplittingTags') ->willReturn(['b', 'a', 'c']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getIgnoreTags') ->willReturn(['ef', 'fa', 'qa']); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getSplitSentences') ->willReturn(TextHandlingEnum::SPLITSENTENCES_ON); - $this->translation->expects($this->once()) + $this->translation->expects(self::once()) ->method('getPreserveFormatting') ->willReturn(TextHandlingEnum::PRESERVEFORMATTING_ON); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') ->with( - http_build_query( + json_encode( [ - 'text' => 'some text to translate', - 'target_lang' => 'some target language', - 'source_lang' => 'some source lang', + 'text' => ['some text to translate'], + 'target_lang' => 'DE', + 'source_lang' => 'EN', 'tag_handling' => 'a,b', 'non_splitting_tags' => 'b,a,c', 'ignore_tags' => 'ef,fa,qa', 'split_sentences' => '1', 'preserve_formatting' => '1', - 'auth_key' => 'some key', - ] + ], ) ) ->willReturn($stream); @@ -129,13 +123,15 @@ public function testGetBodyCanReturnArrayWithOptionalTags(): void self::assertSame($stream, $this->subject->getBody()); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { self::assertSame(DeeplRequestHandlerInterface::METHOD_POST, $this->subject->getMethod()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - self::assertSame('application/x-www-form-urlencoded', $this->subject->getContentType()); + self::assertSame('application/json', $this->subject->getContentType()); } } diff --git a/tests/Handler/DeeplUsageRequestHandlerTest.php b/tests/Handler/DeeplUsageRequestHandlerTest.php index 4332bee..23a185f 100644 --- a/tests/Handler/DeeplUsageRequestHandlerTest.php +++ b/tests/Handler/DeeplUsageRequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Scn\DeeplApiConnector\Handler; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; @@ -11,50 +12,46 @@ class DeeplUsageRequestHandlerTest extends TestCase { - /** - * @var DeeplUsageRequestHandler - */ - private $subject; + private DeeplUsageRequestHandler $subject; - /** @var StreamFactoryInterface|MockObject */ - private $streamFactory; + private StreamFactoryInterface&MockObject $streamFactory; public function setUp(): void { $this->streamFactory = $this->createMock(StreamFactoryInterface::class); $this->subject = new DeeplUsageRequestHandler( - 'some key', - $this->streamFactory + $this->streamFactory, ); } - public function testGetPathCanReturnApiPath(): void + #[Test] + public function getPath(): void { self::assertSame(DeeplUsageRequestHandler::API_ENDPOINT, $this->subject->getPath()); } - public function testGetMethodCanReturnMethod(): void + #[Test] + public function getMethod(): void { self::assertSame(DeeplRequestHandlerInterface::METHOD_GET, $this->subject->getMethod()); } - public function testGetBodyCanReturnArray(): void + #[Test] + public function getBody(): void { $stream = $this->createMock(StreamInterface::class); - $this->streamFactory->expects($this->once()) + $this->streamFactory->expects(self::once()) ->method('createStream') - ->with( - http_build_query(['auth_key' => 'some key']) - ) ->willReturn($stream); self::assertSame($stream, $this->subject->getBody()); } - public function testGetContentTypeReturnsValue(): void + #[Test] + public function getContentType(): void { - self::assertSame('application/x-www-form-urlencoded', $this->subject->getContentType()); + self::assertSame('application/json', $this->subject->getContentType()); } }