From a48decc454b2c2592239931915f7281c97f0b031 Mon Sep 17 00:00:00 2001 From: Jimmy E Date: Thu, 7 Mar 2024 17:05:33 +0100 Subject: [PATCH] Implement Glossary support Fixes #47 --- README.md | 4 + example/use_glossaries.php | 98 +++++++ src/DeeplClient.php | 62 +++++ src/DeeplClientInterface.php | 32 +++ .../GlossarySubmissionEntryFormatEnum.php | 11 + src/Handler/AbstractDeeplHandler.php | 18 ++ .../DeeplBatchTranslationRequestHandler.php | 3 +- src/Handler/DeeplFileRequestHandler.php | 4 +- .../DeeplFileSubmissionRequestHandler.php | 2 +- ...eplFileTranslationStatusRequestHandler.php | 2 +- ...lGlossariesListRetrievalRequestHandler.php | 50 ++++ ...dLanguagesPairsRetrievalRequestHandler.php | 50 ++++ .../DeeplGlossaryCreateRequestHandler.php | 61 +++++ .../DeeplGlossaryDeleteRequestHandler.php | 55 ++++ ...lGlossaryEntriesRetrieveRequestHandler.php | 60 ++++ .../DeeplGlossaryRetrieveRequestHandler.php | 55 ++++ src/Handler/DeeplRequestFactory.php | 58 ++++ src/Handler/DeeplRequestFactoryInterface.php | 22 ++ src/Handler/DeeplRequestHandlerInterface.php | 5 + ...pportedLanguageRetrievalRequestHandler.php | 2 +- .../DeeplTranslationRequestHandler.php | 3 +- src/Handler/DeeplUsageRequestHandler.php | 2 +- src/Model/BatchTranslationConfig.php | 18 +- src/Model/BatchTranslationConfigInterface.php | 4 + src/Model/Glossaries.php | 45 +++ src/Model/GlossariesInterface.php | 20 ++ .../GlossariesSupportedLanguagesPairs.php | 37 +++ ...sariesSupportedLanguagesPairsInterface.php | 13 + src/Model/Glossary.php | 44 +++ src/Model/GlossaryEntries.php | 35 +++ src/Model/GlossaryEntriesInterface.php | 16 ++ src/Model/GlossaryIdSubmission.php | 22 ++ src/Model/GlossaryIdSubmissionInterface.php | 10 + src/Model/GlossaryInterface.php | 21 ++ src/Model/GlossarySubmission.php | 96 +++++++ src/Model/GlossarySubmissionInterface.php | 37 +++ src/Model/TranslationConfig.php | 18 +- src/Model/TranslationConfigInterface.php | 4 + tests/DeeplClientTest.php | 259 +++++++++++++++++- ...ssariesListRetrievalRequestHandlerTest.php | 75 +++++ ...guagesPairsRetrievalRequestHandlerTest.php | 75 +++++ .../DeeplGlossaryCreateRequestHandlerTest.php | 86 ++++++ .../DeeplGlossaryDeleteRequestHandlerTest.php | 85 ++++++ ...ssaryEntriesRetrieveRequestHandlerTest.php | 93 +++++++ ...eeplGlossaryRetrieveRequestHandlerTest.php | 85 ++++++ tests/Handler/DeeplRequestFactoryTest.php | 59 ++++ tests/Model/BatchTranslationConfigTest.php | 6 + .../GlossariesSupportedLanguagesPairsTest.php | 42 +++ tests/Model/GlossariesTest.php | 63 +++++ tests/Model/GlossaryEntriesTest.php | 42 +++ tests/Model/GlossaryIdSubmissionTest.php | 29 ++ tests/Model/GlossarySubmissionTest.php | 61 +++++ tests/Model/GlossaryTest.php | 37 +++ tests/Model/TranslationConfigTest.php | 6 + 54 files changed, 2192 insertions(+), 10 deletions(-) create mode 100644 example/use_glossaries.php create mode 100644 src/Enum/GlossarySubmissionEntryFormatEnum.php create mode 100644 src/Handler/AbstractDeeplHandler.php create mode 100644 src/Handler/DeeplGlossariesListRetrievalRequestHandler.php create mode 100644 src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php create mode 100644 src/Handler/DeeplGlossaryCreateRequestHandler.php create mode 100644 src/Handler/DeeplGlossaryDeleteRequestHandler.php create mode 100644 src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php create mode 100644 src/Handler/DeeplGlossaryRetrieveRequestHandler.php create mode 100644 src/Model/Glossaries.php create mode 100644 src/Model/GlossariesInterface.php create mode 100644 src/Model/GlossariesSupportedLanguagesPairs.php create mode 100644 src/Model/GlossariesSupportedLanguagesPairsInterface.php create mode 100644 src/Model/Glossary.php create mode 100644 src/Model/GlossaryEntries.php create mode 100644 src/Model/GlossaryEntriesInterface.php create mode 100644 src/Model/GlossaryIdSubmission.php create mode 100644 src/Model/GlossaryIdSubmissionInterface.php create mode 100644 src/Model/GlossaryInterface.php create mode 100644 src/Model/GlossarySubmission.php create mode 100644 src/Model/GlossarySubmissionInterface.php create mode 100644 tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php create mode 100644 tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php create mode 100644 tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php create mode 100644 tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php create mode 100644 tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php create mode 100644 tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php create mode 100644 tests/Model/GlossariesSupportedLanguagesPairsTest.php create mode 100644 tests/Model/GlossariesTest.php create mode 100644 tests/Model/GlossaryEntriesTest.php create mode 100644 tests/Model/GlossaryIdSubmissionTest.php create mode 100644 tests/Model/GlossarySubmissionTest.php create mode 100644 tests/Model/GlossaryTest.php diff --git a/README.md b/README.md index c262342..97993a5 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,10 @@ try { See `example/retrieve_supported_languages.php` +#### Working with glossaries + +See [use_glossaries.php](example/use_glossaries.php) + ## Testing ``` bash diff --git a/example/use_glossaries.php b/example/use_glossaries.php new file mode 100644 index 0000000..00fbfce --- /dev/null +++ b/example/use_glossaries.php @@ -0,0 +1,98 @@ +getGlossariesSupportedLanguagesPairs(); +/** + * List all available pairs + */ +var_dump($result->getList()); + +/** @var GlossariesInterface $result */ +$result = $deepl->getGlossariesList(); +/** + * List all glossaries + */ +var_dump($result->getList()); + +$input = (new GlossarySubmission()) + ->setName('en => nl') + ->setSourceLang('en') + ->setTargetLang('nl') + ->setEntries("Hello\tDag\nDog\tHond"); + +// NB.: Note that entries can be a tsv or csv. Related to the documentation : https://www.deepl.com/fr/docs-api/glossaries/formats +// To use CSV, this is the example : + +$input->setEntriesFormat(GlossarySubmissionEntryFormatEnum::FORMAT_CSV) + ->setEntries("Hello,Dag\nDog,Hond"); +/** @var GlossaryInterface $glossary */ +$glossary = $deepl->createGlossary($input); +/** + * Get created glossary + */ +var_dump($glossary->getDetails()); + +$input = (new GlossaryIdSubmission()) + ->setId($glossary->getDetails()['glossary_id']); +/** @var GlossaryInterface $result */ +$result = $deepl->retrieveGlossary($input); +/** + * Get glossary details + */ +var_dump($result->getDetails()); + +/** @var GlossaryEntries $result */ +$result = $deepl->retrieveGlossaryEntries($input); +/** + * Get glossary entries array + */ +var_dump($result->getList()); +/** + * Get glossary entries real result + */ +var_dump($result->getResult()); + +$result = $deepl->deleteGlossary($input); +/** + * True if glossary successfully deleted + */ +var_dump($result); + +/** + * Now, whe can get the glossary from the source and target lang and use it in the TranslationConfig + */ +$source = 'en'; +$target = 'nl'; +/** @var GlossariesInterface $glossaries */ +$glossaries = $deepl->getGlossariesList(); +$glossary = current(array_filter( + $glossaries->getList(), + fn (array $e) => $e['source_lang'] === $source && $e['target_lang'] === $target +)); + +$translationConfig = new TranslationConfig( + 'Hello World', + $target, + $source, + glossaryId: $glossary !== false ? $glossary['glossary_id'] : '' +); +$translationObj = $deepl->getTranslation($translationConfig); +var_dump($translationObj); diff --git a/src/DeeplClient.php b/src/DeeplClient.php index 7e39705..51f1d01 100644 --- a/src/DeeplClient.php +++ b/src/DeeplClient.php @@ -12,11 +12,17 @@ use Scn\DeeplApiConnector\Handler\DeeplRequestHandlerInterface; use Scn\DeeplApiConnector\Model\BatchTranslation; use Scn\DeeplApiConnector\Model\BatchTranslationConfig; +use Scn\DeeplApiConnector\Model\GlossaryEntries; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; +use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface; use Scn\DeeplApiConnector\Model\FileSubmission; use Scn\DeeplApiConnector\Model\FileSubmissionInterface; use Scn\DeeplApiConnector\Model\FileTranslation; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; use Scn\DeeplApiConnector\Model\FileTranslationStatus; +use Scn\DeeplApiConnector\Model\Glossaries; +use Scn\DeeplApiConnector\Model\GlossariesSupportedLanguagesPairs; +use Scn\DeeplApiConnector\Model\Glossary; use Scn\DeeplApiConnector\Model\ResponseModelInterface; use Scn\DeeplApiConnector\Model\SupportedLanguages; use Scn\DeeplApiConnector\Model\Translation; @@ -128,6 +134,54 @@ public function getSupportedLanguages(): ResponseModelInterface ); } + public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface + { + return (new GlossariesSupportedLanguagesPairs())->hydrate( + $this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler() + ) + ); + } + + public function getGlossariesList(): ResponseModelInterface + { + return (new Glossaries())->hydrate( + $this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossariesListRetrievalRequestHandler() + ) + ); + } + + public function createGlossary(GlossarySubmissionInterface $submission): ResponseModelInterface + { + return (new Glossary())->hydrate($this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossaryCreateRequestHandler($submission) + )); + } + + public function retrieveGlossary(GlossaryIdSubmission $submission): ResponseModelInterface + { + return (new Glossary())->hydrate($this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossaryRetrieveRequestHandler($submission) + )); + } + + public function deleteGlossary(GlossaryIdSubmission $submission): bool + { + $this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossaryDeleteRequestHandler($submission) + ); + + return true; + } + + public function retrieveGlossaryEntries(GlossaryIdSubmission $submission): ResponseModelInterface + { + return (new GlossaryEntries())->hydrate($this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossaryEntriesRetrieveRequestHandler($submission) + )); + } + /** * Execute given RequestHandler Request and returns decoded Json Object or throws Exception with Error Code * and maybe given Error Message. @@ -148,6 +202,14 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s ) ->withBody($requestHandler->getBody()); + if ($requestHandler->getAuthHeader() !== null) { + $request = $request->withHeader('Authorization', $requestHandler->getAuthHeader()); + } + + if ($requestHandler->getAcceptHeader() !== null) { + $request = $request->withHeader('Accept', $requestHandler->getAcceptHeader()); + } + try { $response = $this->httpClient->sendRequest($request); } catch (ClientExceptionInterface $exception) { diff --git a/src/DeeplClientInterface.php b/src/DeeplClientInterface.php index 707215c..0f333c9 100644 --- a/src/DeeplClientInterface.php +++ b/src/DeeplClientInterface.php @@ -5,6 +5,8 @@ use Scn\DeeplApiConnector\Enum\LanguageEnum; use Scn\DeeplApiConnector\Model\FileSubmissionInterface; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; +use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface; use Scn\DeeplApiConnector\Model\ResponseModelInterface; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; @@ -59,4 +61,34 @@ public function getFileTranslation(FileSubmissionInterface $fileSubmission): Res * Returns list of supported languages */ public function getSupportedLanguages(): ResponseModelInterface; + + /** + * Returns list of supported languages pairs for glossaries + */ + public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface; + + /** + * Returns list of glossaries + */ + public function getGlossariesList(): ResponseModelInterface; + + /** + * Create a glossary and return glossary object + */ + public function createGlossary(GlossarySubmissionInterface $submission): ResponseModelInterface; + + /** + * Returns a glossary using id + */ + public function retrieveGlossary(GlossaryIdSubmission $submission): ResponseModelInterface; + + /** + * Delete a glossary using id + */ + public function deleteGlossary(GlossaryIdSubmission $submission): bool; + + /** + * Returns list of entries + */ + public function retrieveGlossaryEntries(GlossaryIdSubmission $submission): ResponseModelInterface; } diff --git a/src/Enum/GlossarySubmissionEntryFormatEnum.php b/src/Enum/GlossarySubmissionEntryFormatEnum.php new file mode 100644 index 0000000..e117b4b --- /dev/null +++ b/src/Enum/GlossarySubmissionEntryFormatEnum.php @@ -0,0 +1,11 @@ + implode(static::SEPARATOR, $this->translation->getIgnoreTags()), 'split_sentences' => $this->translation->getSplitSentences(), 'preserve_formatting' => $this->translation->getPreserveFormatting(), + 'glossary_id' => $this->translation->getGlossaryId(), 'auth_key' => $this->authKey, ] ) diff --git a/src/Handler/DeeplFileRequestHandler.php b/src/Handler/DeeplFileRequestHandler.php index d42cb3a..6c7519f 100644 --- a/src/Handler/DeeplFileRequestHandler.php +++ b/src/Handler/DeeplFileRequestHandler.php @@ -1,12 +1,14 @@ authKey = $authKey; + $this->streamFactory = $streamFactory; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return static::API_ENDPOINT; + } + + 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 new file mode 100644 index 0000000..44dc7f1 --- /dev/null +++ b/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php @@ -0,0 +1,50 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return static::API_ENDPOINT; + } + + 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 new file mode 100644 index 0000000..bc2439d --- /dev/null +++ b/src/Handler/DeeplGlossaryCreateRequestHandler.php @@ -0,0 +1,61 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + $this->submission = $submission; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_POST; + } + + public function getPath(): string + { + return static::API_ENDPOINT; + } + + public function getBody(): StreamInterface + { + return $this->streamFactory->createStream( + http_build_query( + array_filter( + $this->submission->toArrayRequest(), + ) + ) + ); + } + + 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 new file mode 100644 index 0000000..095b103 --- /dev/null +++ b/src/Handler/DeeplGlossaryDeleteRequestHandler.php @@ -0,0 +1,55 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + $this->submission = $submission; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_DELETE; + } + + public function getPath(): string + { + return sprintf(static::API_ENDPOINT, $this->submission->getId()); + } + + 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 new file mode 100644 index 0000000..517d20d --- /dev/null +++ b/src/Handler/DeeplGlossaryEntriesRetrieveRequestHandler.php @@ -0,0 +1,60 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + $this->submission = $submission; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return sprintf(static::API_ENDPOINT, $this->submission->getId()); + } + + 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 new file mode 100644 index 0000000..53f48b6 --- /dev/null +++ b/src/Handler/DeeplGlossaryRetrieveRequestHandler.php @@ -0,0 +1,55 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + $this->submission = $submission; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return sprintf(static::API_ENDPOINT, $this->submission->getId()); + } + + 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 a1ceb4f..dc40774 100644 --- a/src/Handler/DeeplRequestFactory.php +++ b/src/Handler/DeeplRequestFactory.php @@ -9,6 +9,8 @@ use Scn\DeeplApiConnector\Model\BatchTranslationConfigInterface; use Scn\DeeplApiConnector\Model\FileSubmissionInterface; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmissionInterface; +use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; final class DeeplRequestFactory implements DeeplRequestFactoryInterface @@ -96,6 +98,62 @@ public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequ ); } + public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface + { + return new DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + + public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface + { + return new DeeplGlossariesListRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + + public function createDeeplGlossaryCreateRequestHandler( + GlossarySubmissionInterface $submission + ): DeeplRequestHandlerInterface { + return new DeeplGlossaryCreateRequestHandler( + $this->authKey, + $this->streamFactory, + $submission + ); + } + + public function createDeeplGlossaryRetrieveRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface { + return new DeeplGlossaryRetrieveRequestHandler( + $this->authKey, + $this->streamFactory, + $submission + ); + } + + public function createDeeplGlossaryDeleteRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface { + return new DeeplGlossaryDeleteRequestHandler( + $this->authKey, + $this->streamFactory, + $submission + ); + } + + public function createDeeplGlossaryEntriesRetrieveRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface { + return new DeeplGlossaryEntriesRetrieveRequestHandler( + $this->authKey, + $this->streamFactory, + $submission + ); + } + public function getDeeplBaseUri(): string { if (strpos($this->authKey, ':fx') !== false) { diff --git a/src/Handler/DeeplRequestFactoryInterface.php b/src/Handler/DeeplRequestFactoryInterface.php index 2abf52d..af2c77b 100644 --- a/src/Handler/DeeplRequestFactoryInterface.php +++ b/src/Handler/DeeplRequestFactoryInterface.php @@ -3,6 +3,8 @@ namespace Scn\DeeplApiConnector\Handler; use Scn\DeeplApiConnector\Model\BatchTranslationConfigInterface; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmissionInterface; +use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface; use Scn\DeeplApiConnector\Model\FileSubmissionInterface; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; @@ -33,5 +35,25 @@ public function createDeeplFileTranslationRequestHandler( public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequestHandlerInterface; + public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface; + + public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface; + + public function createDeeplGlossaryCreateRequestHandler( + GlossarySubmissionInterface $submission + ): DeeplRequestHandlerInterface; + + public function createDeeplGlossaryRetrieveRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface; + + public function createDeeplGlossaryDeleteRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface; + + public function createDeeplGlossaryEntriesRetrieveRequestHandler( + GlossaryIdSubmissionInterface $submission + ): DeeplRequestHandlerInterface; + public function getDeeplBaseUri(): string; } diff --git a/src/Handler/DeeplRequestHandlerInterface.php b/src/Handler/DeeplRequestHandlerInterface.php index bff5e19..de350e6 100644 --- a/src/Handler/DeeplRequestHandlerInterface.php +++ b/src/Handler/DeeplRequestHandlerInterface.php @@ -8,6 +8,7 @@ interface DeeplRequestHandlerInterface { public const METHOD_POST = 'POST'; public const METHOD_GET = 'GET'; + public const METHOD_DELETE = 'DELETE'; public function getMethod(): string; @@ -16,4 +17,8 @@ public function getPath(): string; 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 f7f705d..1e838d3 100644 --- a/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php +++ b/src/Handler/DeeplSupportedLanguageRetrievalRequestHandler.php @@ -7,7 +7,7 @@ use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; -final class DeeplSupportedLanguageRetrievalRequestHandler implements DeeplRequestHandlerInterface +final class DeeplSupportedLanguageRetrievalRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/languages?type=target'; diff --git a/src/Handler/DeeplTranslationRequestHandler.php b/src/Handler/DeeplTranslationRequestHandler.php index 44b05e9..78e881a 100644 --- a/src/Handler/DeeplTranslationRequestHandler.php +++ b/src/Handler/DeeplTranslationRequestHandler.php @@ -8,7 +8,7 @@ use Psr\Http\Message\StreamInterface; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; -final class DeeplTranslationRequestHandler implements DeeplRequestHandlerInterface +final class DeeplTranslationRequestHandler extends AbstractDeeplHandler { private const SEPARATOR = ','; public const API_ENDPOINT = '/v2/translate'; @@ -59,6 +59,7 @@ public function getBody(): StreamInterface 'ignore_tags' => implode(static::SEPARATOR, $this->translation->getIgnoreTags()), 'split_sentences' => $this->translation->getSplitSentences(), 'preserve_formatting' => $this->translation->getPreserveFormatting(), + 'glossary_id' => $this->translation->getGlossaryId(), 'auth_key' => $this->authKey, ] ) diff --git a/src/Handler/DeeplUsageRequestHandler.php b/src/Handler/DeeplUsageRequestHandler.php index 399d4d7..68d3475 100644 --- a/src/Handler/DeeplUsageRequestHandler.php +++ b/src/Handler/DeeplUsageRequestHandler.php @@ -7,7 +7,7 @@ use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; -final class DeeplUsageRequestHandler implements DeeplRequestHandlerInterface +final class DeeplUsageRequestHandler extends AbstractDeeplHandler { public const API_ENDPOINT = '/v2/usage'; diff --git a/src/Model/BatchTranslationConfig.php b/src/Model/BatchTranslationConfig.php index 1d9eccc..d5e837e 100644 --- a/src/Model/BatchTranslationConfig.php +++ b/src/Model/BatchTranslationConfig.php @@ -26,6 +26,8 @@ final class BatchTranslationConfig implements BatchTranslationConfigInterface private string $preserveFormatting; + private string $glossaryId; + /** * @param array $text * @param array $tagHandling @@ -39,7 +41,8 @@ public function __construct( array $nonSplittingTags = [], array $ignoreTags = [], string $splitSentences = TextHandlingEnum::SPLITSENTENCES_ON, - string $preserveFormatting = TextHandlingEnum::PRESERVEFORMATTING_OFF + string $preserveFormatting = TextHandlingEnum::PRESERVEFORMATTING_OFF, + string $glossaryId = '', ) { $this->setText($text); $this->setTargetLang($targetLang); @@ -48,6 +51,7 @@ public function __construct( $this->setIgnoreTags($ignoreTags); $this->setSplitSentences($splitSentences); $this->setPreserveFormatting($preserveFormatting); + $this->setGlossaryId($glossaryId); } /** @@ -139,4 +143,16 @@ public function setPreserveFormatting(string $preserveFormatting): BatchTranslat return $this; } + + public function getGlossaryId(): string + { + return $this->glossaryId; + } + + public function setGlossaryId(string $glossaryId): BatchTranslationConfigInterface + { + $this->glossaryId = $glossaryId; + + return $this; + } } diff --git a/src/Model/BatchTranslationConfigInterface.php b/src/Model/BatchTranslationConfigInterface.php index b6a2499..f7cdacf 100644 --- a/src/Model/BatchTranslationConfigInterface.php +++ b/src/Model/BatchTranslationConfigInterface.php @@ -55,4 +55,8 @@ public function setSplitSentences(string $splitSentences): BatchTranslationConfi public function getPreserveFormatting(): string; public function setPreserveFormatting(string $preserveFormatting): BatchTranslationConfigInterface; + + public function getGlossaryId(): string; + + public function setGlossaryId(string $glossaryId): BatchTranslationConfigInterface; } diff --git a/src/Model/Glossaries.php b/src/Model/Glossaries.php new file mode 100644 index 0000000..30464a5 --- /dev/null +++ b/src/Model/Glossaries.php @@ -0,0 +1,45 @@ + */ + private array $list; + + public function hydrate(stdClass $responseModel): ResponseModelInterface + { + $this->list = $responseModel->glossaries; + + return $this; + } + + /** + * @return array + */ + public function getList(): array + { + return array_map( + function (stdClass $item): array { + /** @var Glossary $glossary */ + $glossary = (new Glossary())->hydrate($item); + + return $glossary->getDetails(); + }, + $this->list + ); + } +} diff --git a/src/Model/GlossariesInterface.php b/src/Model/GlossariesInterface.php new file mode 100644 index 0000000..8fb3e83 --- /dev/null +++ b/src/Model/GlossariesInterface.php @@ -0,0 +1,20 @@ + + */ + public function getList(): array; +} diff --git a/src/Model/GlossariesSupportedLanguagesPairs.php b/src/Model/GlossariesSupportedLanguagesPairs.php new file mode 100644 index 0000000..1edcfd9 --- /dev/null +++ b/src/Model/GlossariesSupportedLanguagesPairs.php @@ -0,0 +1,37 @@ + */ + private array $list; + + public function hydrate(stdClass $responseModel): ResponseModelInterface + { + $this->list = $responseModel->supported_languages; + + return $this; + } + + /** + * @return array + */ + public function getList(): array + { + return array_map( + fn (stdClass $item): array => [ + 'source_lang' => $item->source_lang, + 'target_lang' => $item->target_lang, + ], + $this->list + ); + } +} diff --git a/src/Model/GlossariesSupportedLanguagesPairsInterface.php b/src/Model/GlossariesSupportedLanguagesPairsInterface.php new file mode 100644 index 0000000..b129068 --- /dev/null +++ b/src/Model/GlossariesSupportedLanguagesPairsInterface.php @@ -0,0 +1,13 @@ + + */ + public function getList(): array; +} diff --git a/src/Model/Glossary.php b/src/Model/Glossary.php new file mode 100644 index 0000000..059c08b --- /dev/null +++ b/src/Model/Glossary.php @@ -0,0 +1,44 @@ +details = $responseModel; + + return $this; + } + + /** + * @return array{ + * glossary_id: string, + * name: string, + * ready: boolean, + * source_lang: string, + * target_lang: string, + * creation_time: DateTimeImmutable, + * entry_count: int + * } + */ + public function getDetails(): array + { + return [ + 'glossary_id' => $this->details->glossary_id, + 'ready' => $this->details->ready, + 'name' => $this->details->name, + 'source_lang' => $this->details->source_lang, + 'target_lang' => $this->details->target_lang, + 'creation_time' => $this->details->creation_time, + 'entry_count' => $this->details->entry_count, + ]; + } +} diff --git a/src/Model/GlossaryEntries.php b/src/Model/GlossaryEntries.php new file mode 100644 index 0000000..ee93279 --- /dev/null +++ b/src/Model/GlossaryEntries.php @@ -0,0 +1,35 @@ +result = $responseModel->content; + + return $this; + } + + public function getList(): array + { + return array_map( + fn (string $item): array => [ + 'source' => explode("\t", $item)[0] ?? '', + 'target' => explode("\t", $item)[1] ?? '', + ], + explode("\n", $this->result) + ); + } + + public function getResult(): string + { + return $this->result; + } +} diff --git a/src/Model/GlossaryEntriesInterface.php b/src/Model/GlossaryEntriesInterface.php new file mode 100644 index 0000000..a7f3c14 --- /dev/null +++ b/src/Model/GlossaryEntriesInterface.php @@ -0,0 +1,16 @@ + + */ + public function getList(): array; + + public function getResult(): string; +} diff --git a/src/Model/GlossaryIdSubmission.php b/src/Model/GlossaryIdSubmission.php new file mode 100644 index 0000000..ef8b7d4 --- /dev/null +++ b/src/Model/GlossaryIdSubmission.php @@ -0,0 +1,22 @@ +id; + } + + public function setId(string $id): GlossaryIdSubmission + { + $this->id = $id; + + return $this; + } +} diff --git a/src/Model/GlossaryIdSubmissionInterface.php b/src/Model/GlossaryIdSubmissionInterface.php new file mode 100644 index 0000000..962069d --- /dev/null +++ b/src/Model/GlossaryIdSubmissionInterface.php @@ -0,0 +1,10 @@ +name; + } + + public function setName(string $name): GlossarySubmission + { + $this->name = $name; + + return $this; + } + + public function getSourceLang(): string + { + return $this->sourceLang; + } + + public function setSourceLang(string $sourceLang): GlossarySubmission + { + $this->sourceLang = $sourceLang; + + return $this; + } + + public function getTargetLang(): string + { + return $this->targetLang; + } + + public function setTargetLang(string $targetLang): GlossarySubmission + { + $this->targetLang = $targetLang; + + return $this; + } + + public function getEntries(): string + { + return $this->entries; + } + + public function setEntries(string $entries): GlossarySubmission + { + $this->entries = $entries; + + return $this; + } + + public function getEntriesFormat(): string + { + return $this->entriesFormat; + } + + public function setEntriesFormat(string $entriesFormat): GlossarySubmission + { + $this->entriesFormat = $entriesFormat; + + return $this; + } + + /** + * @return array{ + * name: string, + * source_lang: string, + * target_lang: string, + * entries: string, + * entries_format: string, + * } + */ + public function toArrayRequest(): array + { + return [ + 'name' => $this->getName(), + 'source_lang' => $this->getSourceLang(), + 'target_lang' => $this->getTargetLang(), + 'entries' => $this->getEntries(), + 'entries_format' => $this->getEntriesFormat(), + ]; + } +} diff --git a/src/Model/GlossarySubmissionInterface.php b/src/Model/GlossarySubmissionInterface.php new file mode 100644 index 0000000..d9077cd --- /dev/null +++ b/src/Model/GlossarySubmissionInterface.php @@ -0,0 +1,37 @@ + $tagHandling * @param array $nonSplittingTags @@ -40,7 +42,8 @@ public function __construct( array $nonSplittingTags = [], array $ignoreTags = [], string $splitSentences = TextHandlingEnum::SPLITSENTENCES_ON, - string $preserveFormatting = TextHandlingEnum::PRESERVEFORMATTING_OFF + string $preserveFormatting = TextHandlingEnum::PRESERVEFORMATTING_OFF, + string $glossaryId = '', ) { $this->setText($text); $this->setTargetLang($targetLang); @@ -50,6 +53,7 @@ public function __construct( $this->setIgnoreTags($ignoreTags); $this->setSplitSentences($splitSentences); $this->setPreserveFormatting($preserveFormatting); + $this->setGlossaryId($glossaryId); } public function getText(): string @@ -147,4 +151,16 @@ public function setPreserveFormatting(string $preserveFormatting): TranslationCo return $this; } + + public function getGlossaryId(): string + { + return $this->glossaryId; + } + + public function setGlossaryId(string $glossaryId): TranslationConfigInterface + { + $this->glossaryId = $glossaryId; + + return $this; + } } diff --git a/src/Model/TranslationConfigInterface.php b/src/Model/TranslationConfigInterface.php index 110e70e..352c216 100644 --- a/src/Model/TranslationConfigInterface.php +++ b/src/Model/TranslationConfigInterface.php @@ -53,4 +53,8 @@ public function setSplitSentences(string $splitSentences): TranslationConfigInte public function getPreserveFormatting(): string; public function setPreserveFormatting(string $preserveFormatting): TranslationConfigInterface; + + public function getGlossaryId(): string; + + public function setGlossaryId(string $glossaryId): TranslationConfigInterface; } diff --git a/tests/DeeplClientTest.php b/tests/DeeplClientTest.php index dd97584..85aaafd 100644 --- a/tests/DeeplClientTest.php +++ b/tests/DeeplClientTest.php @@ -4,9 +4,9 @@ namespace Scn\DeeplApiConnector; -use Psr\Http\Client\ClientInterface; use GuzzleHttp\Exception\ClientException; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -19,6 +19,12 @@ use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; use Scn\DeeplApiConnector\Model\FileTranslationInterface; use Scn\DeeplApiConnector\Model\FileTranslationStatusInterface; +use Scn\DeeplApiConnector\Model\Glossaries; +use Scn\DeeplApiConnector\Model\GlossariesSupportedLanguagesPairs; +use Scn\DeeplApiConnector\Model\Glossary; +use Scn\DeeplApiConnector\Model\GlossaryEntries; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; +use Scn\DeeplApiConnector\Model\GlossarySubmission; use Scn\DeeplApiConnector\Model\SupportedLanguages; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; use Scn\DeeplApiConnector\Model\TranslationInterface; @@ -489,6 +495,257 @@ public function testGetSupportedLanguagesReturnsSupportedLanguagesModel(): void $this->assertInstanceOf(SupportedLanguages::class, $this->subject->getSupportedLanguages()); } + public function testGetGlossariesSupportedLanguagesPairsGetCorrectModel(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['supported_languages' => ['with value', 'some-other value']]); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $this->assertInstanceOf(GlossariesSupportedLanguagesPairs::class, $this->subject->getGlossariesSupportedLanguagesPairs()); + } + + public function testGetGlossariesListGetCorrectModel(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossariesListRetrievalRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['glossaries' => ['with value', 'some-other value']]); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $this->assertInstanceOf(Glossaries::class, $this->subject->getGlossariesList()); + } + + public function testCreateGlossaryGetCorrectModel(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossaryCreateRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['with value', 'some-other value']); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $submission = $this->createMock(GlossarySubmission::class); + + $this->assertInstanceOf(Glossary::class, $this->subject->createGlossary($submission)); + } + + public function testRetrieveGlossaryGetCorrectModel(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossaryRetrieveRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['with value', 'some-other value']); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $submission = $this->createMock(GlossaryIdSubmission::class); + $this->assertInstanceOf(Glossary::class, $this->subject->retrieveGlossary($submission)); + } + + public function testDeleteGlossaryGetCorrectBoolean(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossaryDeleteRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['with value', 'some-other value']); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $submission = $this->createMock(GlossaryIdSubmission::class); + $this->assertTrue($this->subject->deleteGlossary($submission)); + } + + public function testRetrieveGlossaryEntriesGetCorrectModel(): void + { + $requestHandler = $this->createMock(DeeplRequestHandlerInterface::class); + $requestHandler->method('getMethod') + ->willReturn('some method'); + $requestHandler->method('getPath') + ->willReturn('some path'); + + $this->deeplRequestFactory->method('createDeeplGlossaryEntriesRetrieveRequestHandler') + ->willReturn($requestHandler); + + $json = json_encode(['content' => 'test']); + + $stream = $this->createMock(StreamInterface::class); + $stream->expects($this->once()) + ->method('getContents') + ->willReturn($json); + + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->willReturn(['application/json']); + $response->expects($this->once()) + ->method('getBody') + ->willReturn($stream); + + $request = $this->createRequestExpectations( + $requestHandler, + 'some method', + 'some path' + ); + + $this->httpClient->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + + $submission = $this->createMock(GlossaryIdSubmission::class); + $this->assertInstanceOf(GlossaryEntries::class, $this->subject->retrieveGlossaryEntries($submission)); + } + private function createRequestExpectations( MockObject $requestHandler, string $method, diff --git a/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php b/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php new file mode 100644 index 0000000..74ab0f7 --- /dev/null +++ b/tests/Handler/DeeplGlossariesListRetrievalRequestHandlerTest.php @@ -0,0 +1,75 @@ +streamFactory = $this->createMock(StreamFactoryInterface::class); + + $this->subject = new DeeplGlossariesListRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + + public function testGetMethodReturnsValue(): void + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_GET, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + static::assertSame( + '/v2/glossaries', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php b/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php new file mode 100644 index 0000000..5d19c82 --- /dev/null +++ b/tests/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandlerTest.php @@ -0,0 +1,75 @@ +streamFactory = $this->createMock(StreamFactoryInterface::class); + + $this->subject = new DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + + public function testGetMethodReturnsValue(): void + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_GET, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + static::assertSame( + '/v2/glossary-language-pairs', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php b/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php new file mode 100644 index 0000000..0f60c94 --- /dev/null +++ b/tests/Handler/DeeplGlossaryCreateRequestHandlerTest.php @@ -0,0 +1,86 @@ +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 + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_POST, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + static::assertSame( + '/v2/glossaries', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->submission->expects($this->once()) + ->method('toArrayRequest') + ->willReturn([ + 'test' => 'test1', + ]); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php b/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php new file mode 100644 index 0000000..934afaa --- /dev/null +++ b/tests/Handler/DeeplGlossaryDeleteRequestHandlerTest.php @@ -0,0 +1,85 @@ +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 + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_DELETE, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + $this->submission->expects($this->once()) + ->method('getId') + ->with() + ->willReturn('1'); + + static::assertSame( + '/v2/glossaries/1', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php b/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php new file mode 100644 index 0000000..b36964e --- /dev/null +++ b/tests/Handler/DeeplGlossaryEntriesRetrieveRequestHandlerTest.php @@ -0,0 +1,93 @@ +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 + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_GET, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + $this->submission->expects($this->once()) + ->method('getId') + ->with() + ->willReturn('1'); + + static::assertSame( + '/v2/glossaries/1/entries', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetAcceptHeader(): void + { + static::assertSame( + 'text/tab-separated-values', + $this->subject->getAcceptHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php b/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php new file mode 100644 index 0000000..06098f0 --- /dev/null +++ b/tests/Handler/DeeplGlossaryRetrieveRequestHandlerTest.php @@ -0,0 +1,85 @@ +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 + { + static::assertSame( + DeeplRequestHandlerInterface::METHOD_GET, + $this->subject->getMethod() + ); + } + + public function testGetPathReturnsValue(): void + { + $this->submission->expects($this->once()) + ->method('getId') + ->with() + ->willReturn('1'); + + static::assertSame( + '/v2/glossaries/1', + $this->subject->getPath() + ); + } + + public function testGetContentTypeReturnsValue(): void + { + static::assertSame( + 'application/x-www-form-urlencoded', + $this->subject->getContentType() + ); + } + + public function testGetAuthHeader(): void + { + static::assertSame( + 'DeepL-Auth-Key some-auth-key', + $this->subject->getAuthHeader() + ); + } + + public function testGetBodyReturnsValue(): void + { + $body = $this->createMock(StreamInterface::class); + + $this->streamFactory->expects($this->once()) + ->method('createStream') + ->with() + ->willReturn($body); + + static::assertSame( + $body, + $this->subject->getBody() + ); + } +} diff --git a/tests/Handler/DeeplRequestFactoryTest.php b/tests/Handler/DeeplRequestFactoryTest.php index 234ee78..993c8f4 100644 --- a/tests/Handler/DeeplRequestFactoryTest.php +++ b/tests/Handler/DeeplRequestFactoryTest.php @@ -9,6 +9,8 @@ use Scn\DeeplApiConnector\Model\BatchTranslationConfigInterface; use Scn\DeeplApiConnector\Model\FileSubmissionInterface; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; +use Scn\DeeplApiConnector\Model\GlossaryIdSubmission; +use Scn\DeeplApiConnector\Model\GlossarySubmission; use Scn\DeeplApiConnector\Model\TranslationConfigInterface; use Scn\DeeplApiConnector\TestCase; @@ -101,4 +103,61 @@ public function testGetDeeplFreeUriCanReturnFreeBaseUri(): void $this->subject = new DeeplRequestFactory('something:fx', $this->streamFactory); $this->assertSame(DeeplRequestFactory::DEEPL_FREE_BASE_URI, $this->subject->getDeeplBaseUri()); } + + public function testCreateDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): void + { + $this->assertInstanceOf( + DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler::class, + $this->subject->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler() + ); + } + + public function testCreateDeeplGlossariesListRetrievalRequestHandler(): void + { + $this->assertInstanceOf( + DeeplGlossariesListRetrievalRequestHandler::class, + $this->subject->createDeeplGlossariesListRetrievalRequestHandler() + ); + } + + public function testCreateDeeplGlossaryCreateRequestHandler(): void + { + $submission = $this->createMock(GlossarySubmission::class); + + $this->assertInstanceOf( + DeeplGlossaryCreateRequestHandler::class, + $this->subject->createDeeplGlossaryCreateRequestHandler($submission) + ); + } + + public function testCreateDeeplGlossaryRetrieveRequestHandler(): void + { + $submission = $this->createMock(GlossaryIdSubmission::class); + + $this->assertInstanceOf( + DeeplGlossaryRetrieveRequestHandler::class, + $this->subject->createDeeplGlossaryRetrieveRequestHandler($submission) + ); + } + + public function testCreateDeeplGlossaryDeleteRequestHandler(): void + { + $submission = $this->createMock(GlossaryIdSubmission::class); + + $this->assertInstanceOf( + DeeplGlossaryDeleteRequestHandler::class, + $this->subject->createDeeplGlossaryDeleteRequestHandler($submission) + ); + } + + public function testCreateDeeplGlossaryEntriesRetrieveRequestHandler(): void + { + $submission = $this->createMock(GlossaryIdSubmission::class); + + $this->assertInstanceOf( + DeeplGlossaryEntriesRetrieveRequestHandler::class, + $this->subject->createDeeplGlossaryEntriesRetrieveRequestHandler($submission) + ); + } + //TODO tests } diff --git a/tests/Model/BatchTranslationConfigTest.php b/tests/Model/BatchTranslationConfigTest.php index 6214168..8f3fe37 100644 --- a/tests/Model/BatchTranslationConfigTest.php +++ b/tests/Model/BatchTranslationConfigTest.php @@ -74,4 +74,10 @@ public function testGetSplitSentencesCanReturnInteger(): void static::assertSame(TextHandlingEnum::SPLITSENTENCES_NONEWLINES, $this->subject->getSplitSentences()); } + + public function testGetGlossaryIdCanReturnInteger(): void + { + $this->subject->setGlossaryId('id'); + $this->assertSame('id', $this->subject->getGlossaryId()); + } } diff --git a/tests/Model/GlossariesSupportedLanguagesPairsTest.php b/tests/Model/GlossariesSupportedLanguagesPairsTest.php new file mode 100644 index 0000000..d466b80 --- /dev/null +++ b/tests/Model/GlossariesSupportedLanguagesPairsTest.php @@ -0,0 +1,42 @@ +subject = new GlossariesSupportedLanguagesPairs(); + } + + public function testGetLanguagesReturnHydratedValues(): void + { + $lang1 = [ + 'source_lang'=> 'de', + 'target_lang'=> 'en', + ]; + $lang2 = [ + 'source_lang'=> 'de', + 'target_lang'=> 'es', + ]; + $result = [ + 'supported_languages'=> [ + (object)$lang1, + (object)$lang2, + ], + ]; + + $this->subject->hydrate((object)$result); + + static::assertSame( + [$lang1, $lang2], + $this->subject->getList() + ); + } +} diff --git a/tests/Model/GlossariesTest.php b/tests/Model/GlossariesTest.php new file mode 100644 index 0000000..998f6b4 --- /dev/null +++ b/tests/Model/GlossariesTest.php @@ -0,0 +1,63 @@ +subject = new Glossaries(); + } + + public function testGetLanguagesReturnHydratedValues(): void + { + $glossary1 = [ + 'glossary_id' => 'fea20bcb-af84-4e1e-ba40-24d64d789df3', + 'ready' => true, + 'name' => 'en => nl', + 'source_lang' => 'en', + 'target_lang' => 'nl', + 'creation_time' => '2024-03-06T13:05:42.030634Z', + 'entry_count' => 1, + ]; + $glossary2 = [ + 'glossary_id' => 'ac7780ac-bd63-4e64-9ec3-0e82aa2c4825', + 'ready' => true, + 'name' => 'fr => de', + 'source_lang' => 'fr', + 'target_lang' => 'de', + 'creation_time' => '2024-03-06T13:26:09.791227Z', + 'entry_count' => 2, + ]; + $glossary3 = [ + 'glossary_id' => '9cc8f5b1-3096-44a4-8a22-3e8b337471cd', + 'ready' => true, + 'name' => 'fr => en', + 'source_lang' => 'fr', + 'target_lang' => 'en', + 'creation_time' => '2024-03-06T14:12:20.589969Z', + 'entry_count' => 1, + ]; + $glossaries = [ + (object)$glossary1, + (object)$glossary2, + (object)$glossary3, + ]; + $result = [ + 'glossaries' => $glossaries + ]; + + $this->subject->hydrate((object)$result); + + static::assertSame( + [$glossary1, $glossary2, $glossary3], + $this->subject->getList() + ); + } +} diff --git a/tests/Model/GlossaryEntriesTest.php b/tests/Model/GlossaryEntriesTest.php new file mode 100644 index 0000000..3a295d3 --- /dev/null +++ b/tests/Model/GlossaryEntriesTest.php @@ -0,0 +1,42 @@ +subject = new GlossaryEntries(); + } + + public function testGetLanguagesReturnHydratedValues(): void + { + $string = "Test\ttest\nTest2\ttest"; + $this->subject->hydrate((object)['content' => $string]); + + static::assertSame( + [ + [ + 'source' => 'Test', + 'target' => 'test', + ], + [ + 'source' => 'Test2', + 'target' => 'test', + ], + ], + $this->subject->getList() + ); + + static::assertSame( + $string, + $this->subject->getResult() + ); + } +} diff --git a/tests/Model/GlossaryIdSubmissionTest.php b/tests/Model/GlossaryIdSubmissionTest.php new file mode 100644 index 0000000..75d1f6e --- /dev/null +++ b/tests/Model/GlossaryIdSubmissionTest.php @@ -0,0 +1,29 @@ +subject = new GlossaryIdSubmission(); + } + + public function testGetIdReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setId($value)->getId() + ); + } +} diff --git a/tests/Model/GlossarySubmissionTest.php b/tests/Model/GlossarySubmissionTest.php new file mode 100644 index 0000000..942e2a1 --- /dev/null +++ b/tests/Model/GlossarySubmissionTest.php @@ -0,0 +1,61 @@ +subject = new GlossarySubmission(); + } + + public function testGetNameReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setName($value)->getName() + ); + } + public function testGetSourceLangReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setSourceLang($value)->getSourceLang() + ); + } + public function testGetTargetLangReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setTargetLang($value)->getTargetLang() + ); + } + public function testGetEntriesReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setEntries($value)->getEntries() + ); + } + public function testGetEntriesFormatReturnString(): void + { + $value = 'some value'; + $this->assertSame( + $value, + $this->subject->setEntriesFormat($value)->getEntriesFormat() + ); + } +} diff --git a/tests/Model/GlossaryTest.php b/tests/Model/GlossaryTest.php new file mode 100644 index 0000000..223ad37 --- /dev/null +++ b/tests/Model/GlossaryTest.php @@ -0,0 +1,37 @@ +subject = new Glossary(); + } + + public function testGetLanguagesReturnHydratedValues(): void + { + $glossary1 = [ + 'glossary_id' => 'fea20bcb-af84-4e1e-ba40-24d64d789df3', + 'ready' => true, + 'name' => 'en => nl', + 'source_lang' => 'en', + 'target_lang' => 'nl', + 'creation_time' => '2024-03-06T13:05:42.030634Z', + 'entry_count' => 1, + ]; + + $this->subject->hydrate((object)$glossary1); + + static::assertSame( + $glossary1, + $this->subject->getDetails() + ); + } +} diff --git a/tests/Model/TranslationConfigTest.php b/tests/Model/TranslationConfigTest.php index 26453f8..a7caa33 100644 --- a/tests/Model/TranslationConfigTest.php +++ b/tests/Model/TranslationConfigTest.php @@ -74,4 +74,10 @@ public function testGetSplitSentencesCanReturnInteger(): void $this->subject->setSplitSentences(TextHandlingEnum::SPLITSENTENCES_NONEWLINES); $this->assertSame(TextHandlingEnum::SPLITSENTENCES_NONEWLINES, $this->subject->getSplitSentences()); } + + public function testGetGlossaryIdCanReturnInteger(): void + { + $this->subject->setGlossaryId('id'); + $this->assertSame('id', $this->subject->getGlossaryId()); + } }