Skip to content

Commit

Permalink
Work with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 26, 2024
1 parent 5c3f272 commit d778961
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/Integrations/Deepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
10 changes: 4 additions & 6 deletions src/Integrations/GoogleFree.php → src/Integrations/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
use LaravelLang\LocaleList\Locale;
use Stichoza\GoogleTranslate\GoogleTranslate;

class GoogleFree extends Integration
class Google extends Integration
{
protected array $map = [
Locale::French->value => 'fr',
];

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
Expand All @@ -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));
}
}
4 changes: 2 additions & 2 deletions src/Integrations/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'),
)
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Concerns/Value.php → tests/Constants/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Concerns;
namespace Tests\Constants;

class Value
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Datasets/Translators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]);
46 changes: 46 additions & 0 deletions tests/Helpers/Mocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

use DeepL\Translator as DeeplTranslator;
use LaravelLang\Translator\Integrations\Deepl;
use LaravelLang\Translator\Integrations\Google;
use LaravelLang\Translator\Integrations\Yandex;
use LaravelLang\Translator\Requests\YandexCloud;
use Stichoza\GoogleTranslate\GoogleTranslate;
use Tests\Constants\Value;

function mockTranslator(string $translator, string $integration, array $methods = []): void
{
$service = mock($integration);

foreach ($methods as $key => $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],
]);
}
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<?php

uses(Tests\TestCase::class)
->compact()
->beforeEach(fn () => mockTranslators())
->in(__DIR__);
22 changes: 20 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Translate/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Translate/IntegrationCanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Translate/IntegrationCannotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d778961

Please sign in to comment.