From d77896181a51e8e36d257829193ddd8bedfbf932 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 26 Jun 2024 20:54:27 +0300 Subject: [PATCH] Work with tests --- src/Integrations/Deepl.php | 5 +- .../{GoogleFree.php => Google.php} | 10 ++-- src/Integrations/Integration.php | 4 +- src/ServiceProvider.php | 14 +++--- src/Services/Translate.php | 4 +- tests/{Concerns => Constants}/Value.php | 2 +- tests/Datasets/Translators.php | 8 ++-- tests/Helpers/Mocks.php | 46 +++++++++++++++++++ tests/Pest.php | 5 ++ tests/TestCase.php | 22 ++++++++- tests/Unit/Translate/FacadeTest.php | 2 +- tests/Unit/Translate/IntegrationCanTest.php | 2 +- .../Unit/Translate/IntegrationCannotTest.php | 2 +- 13 files changed, 95 insertions(+), 31 deletions(-) rename src/Integrations/{GoogleFree.php => Google.php} (75%) rename tests/{Concerns => Constants}/Value.php (93%) create mode 100644 tests/Helpers/Mocks.php diff --git a/src/Integrations/Deepl.php b/src/Integrations/Deepl.php index 3fc6c9e..b165292 100644 --- a/src/Integrations/Deepl.php +++ b/src/Integrations/Deepl.php @@ -15,12 +15,11 @@ class Deepl extends Integration ]; public function __construct( - protected DeeplTranslator $translator, - protected array $options + protected DeeplTranslator $translator ) {} protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection { - return collect($this->translator->translateText($text, $this->lang($from), $this->lang($to), $this->options)); + return collect($this->translator->translateText($text, $this->lang($from), $this->lang($to))); } } diff --git a/src/Integrations/GoogleFree.php b/src/Integrations/Google.php similarity index 75% rename from src/Integrations/GoogleFree.php rename to src/Integrations/Google.php index 8cf36d2..0a74d65 100644 --- a/src/Integrations/GoogleFree.php +++ b/src/Integrations/Google.php @@ -8,7 +8,7 @@ use LaravelLang\LocaleList\Locale; use Stichoza\GoogleTranslate\GoogleTranslate; -class GoogleFree extends Integration +class Google extends Integration { protected array $map = [ Locale::French->value => 'fr', @@ -16,8 +16,7 @@ class GoogleFree extends Integration public function __construct( protected GoogleTranslate $translator, - protected array $options = [], - protected bool|string $preserve = true + protected string|true $regex = true ) {} protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection @@ -30,9 +29,8 @@ protected function request(iterable|string $text, Locale|string $to, Locale|stri protected function translator(Locale|string $to, Locale|string|null $from): GoogleTranslate { return $this->translator - ->preserveParameters($this->preserve) + ->preserveParameters($this->regex) ->setSource($this->lang($from)) - ->setTarget($this->lang($to)) - ->setOptions($this->options); + ->setTarget($this->lang($to)); } } diff --git a/src/Integrations/Integration.php b/src/Integrations/Integration.php index 58efa3a..367dae8 100644 --- a/src/Integrations/Integration.php +++ b/src/Integrations/Integration.php @@ -29,8 +29,8 @@ public function translate( Locale|string|null $from = null ): array|string { return is_array($text) - ? $this->request($text, $to, $from)->pluck('text')->all() - : $this->request($text, $to, $from)->pluck('text')->first(); + ? $this->request($text, $to, $from)->all() + : $this->request($text, $to, $from)->first(); } protected function lang(Locale|string|null $lang): ?string diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6cb8f90..66f3a7d 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -8,7 +8,7 @@ use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider as BaseServiceProvider; use LaravelLang\Translator\Integrations\Deepl; -use LaravelLang\Translator\Integrations\GoogleFree; +use LaravelLang\Translator\Integrations\Google; use LaravelLang\Translator\Integrations\Yandex; use LaravelLang\Translator\Requests\YandexCloud; use Stichoza\GoogleTranslate\GoogleTranslate; @@ -20,23 +20,21 @@ public function boot(): void $this->app->singleton(Deepl::class, function (Application $app) { return new Deepl( translator: new Translator($app['config']->get('services.deepl.key')), - options : $app['config']->get('services.deepl.options', []) ); }); - $this->app->singleton(GoogleFree::class, function (Application $app) { - return new GoogleFree( + $this->app->singleton(Google::class, function (Application $app) { + return new Google( translator: new GoogleTranslate(), - options : $app['config']->get('services.google_translate.options', []), - preserve : $app['config']->get('services.deepl.preserve_parameters') ?: true, + regex : $app['config']->get('services.google_translate.regex_parameters') ?: true, ); }); $this->app->singleton(Yandex::class, function (Application $app) { return new Yandex( new YandexCloud( - key : $app['config']->get('services.yandex.key'), - folderId: $app['config']->get('services.yandex.folder_id'), + key : $app['config']->get('services.yandex_translate.key'), + folderId: $app['config']->get('services.yandex_translate.folder_id'), ) ); }); diff --git a/src/Services/Translate.php b/src/Services/Translate.php index 3debe2d..bb06988 100644 --- a/src/Services/Translate.php +++ b/src/Services/Translate.php @@ -7,7 +7,7 @@ use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Contracts\Translator; use LaravelLang\Translator\Integrations\Deepl; -use LaravelLang\Translator\Integrations\GoogleFree; +use LaravelLang\Translator\Integrations\Google; use LaravelLang\Translator\Integrations\Yandex; class Translate @@ -33,7 +33,7 @@ public function viaGoogleFree( Locale|string $to, Locale|string|null $from = null ): array|string { - return $this->via(GoogleFree::class, $text, $to, $from); + return $this->via(Google::class, $text, $to, $from); } public function viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from = null): array|string diff --git a/tests/Concerns/Value.php b/tests/Constants/Value.php similarity index 93% rename from tests/Concerns/Value.php rename to tests/Constants/Value.php index 3bf5b3a..4d7a2f4 100644 --- a/tests/Concerns/Value.php +++ b/tests/Constants/Value.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Concerns; +namespace Tests\Constants; class Value { diff --git a/tests/Datasets/Translators.php b/tests/Datasets/Translators.php index 17e8c86..2cee832 100644 --- a/tests/Datasets/Translators.php +++ b/tests/Datasets/Translators.php @@ -3,11 +3,11 @@ declare(strict_types=1); use LaravelLang\Translator\Integrations\Deepl; -use LaravelLang\Translator\Integrations\GoogleFree; +use LaravelLang\Translator\Integrations\Google; use LaravelLang\Translator\Integrations\Yandex; dataset('translators', fn () => [ - Deepl::class => [Deepl::class], - GoogleFree::class => [GoogleFree::class], - Yandex::class => [Yandex::class], + Deepl::class => [Deepl::class], + Google::class => [Google::class], + Yandex::class => [Yandex::class], ]); diff --git a/tests/Helpers/Mocks.php b/tests/Helpers/Mocks.php new file mode 100644 index 0000000..26fe2de --- /dev/null +++ b/tests/Helpers/Mocks.php @@ -0,0 +1,46 @@ + $value) { + is_string($key) + ? $service->shouldReceive($key)->andReturn($value) + : $service->shouldReceive($value)->andReturnSelf(); + } + + app()->forgetInstance($translator); + app()->singleton($translator, fn () => new $translator($service)); +} + +function mockTranslators( + array|string|null $deepl = null, + array|string|null $google = null, + array|string|null $yandex = null, +): void { + mockTranslator(Deepl::class, DeeplTranslator::class, [ + 'translateText' => $deepl ?? Value::Text1French, + ]); + + mockTranslator(Google::class, GoogleTranslate::class, [ + 'translate' => $google ?? Value::Text1French, + 'preserveParameters', + 'setSource', + 'setTarget', + ]); + + mockTranslator(Yandex::class, YandexCloud::class, [ + 'translate' => $yandex ?? [Value::Text1French], + ]); +} diff --git a/tests/Pest.php b/tests/Pest.php index b3d9bbc..1e49adb 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1 +1,6 @@ compact() + ->beforeEach(fn () => mockTranslators()) + ->in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5042227..18fc3aa 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,24 @@ namespace Tests; -use PHPUnit\Framework\TestCase as BaseTestCase; +use Illuminate\Config\Repository; +use LaravelLang\Translator\ServiceProvider; +use Orchestra\Testbench\TestCase as BaseTestCase; -abstract class TestCase extends BaseTestCase {} +abstract class TestCase extends BaseTestCase +{ + protected function getPackageProviders($app): array + { + return [ServiceProvider::class]; + } + + protected function defineEnvironment($app): void + { + tap($app['config'], function (Repository $config) { + $config->set('services.deepl.key', 'foo'); + + $config->set('services.yandex_translate.key', 'foo'); + $config->set('services.yandex_translate.folder_id', 'bar'); + }); + } +} diff --git a/tests/Unit/Translate/FacadeTest.php b/tests/Unit/Translate/FacadeTest.php index ea573a2..1dc8adc 100644 --- a/tests/Unit/Translate/FacadeTest.php +++ b/tests/Unit/Translate/FacadeTest.php @@ -4,7 +4,7 @@ use LaravelLang\LocaleList\Locale; use LaravelLang\Translator\Facades\Translate; -use Tests\Concerns\Value; +use Tests\Constants\Value; test('translate', function () { expect(Translate::text(Value::Text1English, Locale::French))->toBe( diff --git a/tests/Unit/Translate/IntegrationCanTest.php b/tests/Unit/Translate/IntegrationCanTest.php index 93f2fd4..77eab7e 100644 --- a/tests/Unit/Translate/IntegrationCanTest.php +++ b/tests/Unit/Translate/IntegrationCanTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use LaravelLang\LocaleList\Locale; -use Tests\Concerns\Value; +use Tests\Constants\Value; test('as string', function (string $translator) { $translator = translator($translator); diff --git a/tests/Unit/Translate/IntegrationCannotTest.php b/tests/Unit/Translate/IntegrationCannotTest.php index c325ea4..95eafd3 100644 --- a/tests/Unit/Translate/IntegrationCannotTest.php +++ b/tests/Unit/Translate/IntegrationCannotTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use Tests\Concerns\Value; +use Tests\Constants\Value; test('as string', function (string $translator) { $translator = translator($translator);