Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run cleanup command after upload and ask to cleanup terms in poeditor #24

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"guzzlehttp/guzzle": "^7.0",
"illuminate/filesystem": "^10.0",
"illuminate/http": "^10.0",
"illuminate/support": "^10.2",
"illuminate/support": "^10.10",
"symfony/var-exporter": "^6.0"
},
"require-dev": {
Expand Down
17 changes: 16 additions & 1 deletion src/Commands/UploadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ public function handle() : int
}

$translations = app(TranslationManager::class)->getTranslations($this->getLocale());
$cleanup = collect(app(Poeditor::class)->download($this->getPoeditorLocale()))
->dot()
->keys()
->diff(collect($translations)->dot()->keys())
->whenNotEmpty(function ($locallyDeletedTranslationsKeys) use (&$cleanup) {
$this->error('The following translation keys do not exist locally but do exist in POEditor:');

$this->table(
['Translation Key'],
$locallyDeletedTranslationsKeys->map(fn ($key) => [$key])->all()
);

return $this->confirm('Do you want to delete those translation keys in POEditor? (y/n)');
}, fn () => false);

$response = app(Poeditor::class)->upload(
$this->getPoeditorLocale(),
$translations,
$this->hasOption('force') && $this->option('force')
$this->hasOption('force') && $this->option('force'),
$cleanup
);

$this->info('All translations have been uploaded:');
Expand Down
15 changes: 8 additions & 7 deletions src/Poeditor/Poeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function download(string $language) : array
return collect(Http::get($exportUrl)->json())->dot()->filter()->undot()->toArray();
}

public function upload(string $language, array $translations, bool $overwrite = false) : UploadResponse
{
public function upload(
string $language,
array $translations,
bool $overwrite = false,
bool $sync = false
) : UploadResponse {
$response = Http::asMultipart()
->attach('file', json_encode($translations), 'translations.json')
->post('https://api.poeditor.com/v2/projects/upload', [
Expand All @@ -38,15 +42,12 @@ public function upload(string $language, array $translations, bool $overwrite =
'language' => $language,
'updating' => 'terms_translations',
'overwrite' => (int) $overwrite,
'sync_terms' => (int) $sync,
'fuzzy_trigger' => 1,
]);

if ($response->json('response.status') === 'fail') {
if (class_exists(Sleep::class)) {
Sleep::for(10)->seconds();
} else {
sleep(10);
}
Sleep::for(10)->seconds();

return $this->upload($language, $translations, $overwrite);
}
Expand Down
10 changes: 0 additions & 10 deletions tests/DownloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace NextApps\PoeditorSync\Tests;

use Illuminate\Filesystem\Filesystem;
use NextApps\PoeditorSync\Poeditor\Poeditor;

class DownloadCommandTest extends TestCase
{
Expand Down Expand Up @@ -384,13 +383,4 @@ public function it_automatically_runs_validate_command_after_download_if_configu
->expectsOutput('All translations are valid!')
->assertExitCode(0);
}

public function mockPoeditorDownload(string $language, array $data) : void
{
if (get_class(app(Poeditor::class)) === Poeditor::class) {
$this->mock(Poeditor::class);
}

app(Poeditor::class)->shouldReceive('download')->with($language)->andReturn($data);
}
}
18 changes: 7 additions & 11 deletions tests/PoeditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public function it_uploads_translations()
'name' => 'overwrite',
'contents' => 0,
],
[
'name' => 'sync_terms',
'contents' => 0,
],
[
'name' => 'fuzzy_trigger',
'contents' => 1,
Expand Down Expand Up @@ -203,23 +207,15 @@ public function it_retries_uploads_translations_if_poeditor_upload_rate_limit_wa
]),
]);

if (class_exists(Sleep::class)) {
Sleep::fake();
} else {
$startTime = time();
}
Sleep::fake();

$response = app(Poeditor::class)->upload($this->faker->locale(), ['key' => 'value']);

$this->assertInstanceOf(UploadResponse::class, $response);

Http::assertSentCount(2);

if (class_exists(Sleep::class)) {
Sleep::assertSleptTimes(1);
Sleep::assertSequence([Sleep::for(10)->seconds()]);
} else {
$this->assertTrue(time() - $startTime > 9);
}
Sleep::assertSleptTimes(1);
Sleep::assertSequence([Sleep::for(10)->seconds()]);
}
}
133 changes: 123 additions & 10 deletions tests/UploadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public function it_uploads_php_translations_of_default_locale()
],
]);

$this->mockPoeditorDownload('en', [
'first-en-php-file' => [
'foo' => 'bar',
],
'second-en-php-file' => [
'bar' => 'foo',
],
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand All @@ -48,6 +57,11 @@ public function it_uploads_json_translations_of_default_locale()
'foo_bar' => 'bar foo',
]);

$this->mockPoeditorDownload('en', [
'foo' => 'bar',
'foo_bar' => 'bar foo',
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand Down Expand Up @@ -77,6 +91,23 @@ public function it_uploads_vendor_translations()
],
]);

$this->mockPoeditorDownload('en', [
'vendor' => [
'first-package' => [
'first-package-php-file' => [
'bar_foo_bar' => 'foo bar foo',
],
'bar_foo' => 'foo bar',
],
'second-package' => [
'second-package-php-file' => [
'foo_bar_foo' => 'bar foo bar',
],
'foo_bar' => 'bar foo',
],
],
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand All @@ -103,6 +134,21 @@ public function it_uploads_php_and_json_and_vendor_translations()
],
]);

$this->mockPoeditorDownload('en', [
'php-file' => [
'bar' => 'foo',
],
'foo_bar' => 'bar foo',
'vendor' => [
'package-name' => [
'package-php-file' => [
'bar_foo_bar' => 'foo bar foo',
],
'bar_foo' => 'foo bar',
],
],
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand All @@ -122,6 +168,13 @@ public function it_uploads_translations_of_provided_locale()
'bar_foo' => 'foo bar',
]);

$this->mockPoeditorDownload('nl', [
'nl-php-file' => [
'foo' => 'bar',
],
'bar_foo' => 'foo bar',
]);

config()->set('poeditor-sync.locales', ['en', 'nl']);

$this->artisan('poeditor:upload nl')->assertExitCode(0);
Expand All @@ -132,6 +185,8 @@ public function it_outputs_upload_response_info()
{
$this->mockPoeditorUpload('en', response: $response = $this->getPoeditorUploadResponse());

$this->mockPoeditorDownload('en', []);

$this->artisan('poeditor:upload')
->expectsOutput('All translations have been uploaded:')
->expectsOutput("{$response->getAddedTermsCount()} terms added")
Expand All @@ -146,10 +201,43 @@ public function it_uploads_with_overwrite_enabled()
{
$this->mockPoeditorUpload('en', [], true);

$this->mockPoeditorDownload('en', []);

$this->artisan('poeditor:upload --force')
->assertExitCode(0);
}

/** @test */
public function it_asks_for_confirmation_when_local_translations_keys_do_not_match_translations_keys_in_poeditor()
{
$this->createPhpTranslationFile('en/php-file.php', ['bar' => 'foo']);

$this->mockPoeditorDownload('en', [
'php-file' => [
'bar' => 'foo',
'baz' => 'bar',
],
]);

$this->mockPoeditorUpload('en', [
'php-file' => [
'bar' => 'foo',
],
], false, true, $response = $this->getPoeditorUploadResponse());

$this->artisan('poeditor:upload')
->expectsOutput('The following translation keys do not exist locally but do exist in POEditor:')
->expectsTable(
['Translation Key'],
[
['php-file.baz'],
]
)
->expectsQuestion('Do you want to delete those translation keys in POEditor? (y/n)', 'y')
->expectsOutput("{$response->getDeletedTermsCount()} terms deleted")
->assertExitCode(0);
}

/** @test */
public function it_does_not_upload_vendor_translations_if_disabled_in_config()
{
Expand All @@ -168,6 +256,13 @@ public function it_does_not_upload_vendor_translations_if_disabled_in_config()
'foo_bar' => 'bar foo',
]);

$this->mockPoeditorDownload('en', [
'php-file' => [
'bar' => 'foo',
],
'foo_bar' => 'bar foo',
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand All @@ -192,6 +287,13 @@ public function it_does_not_upload_translation_files_that_have_been_excluded_in_
'foo_bar' => 'bar foo',
]);

$this->mockPoeditorDownload('en', [
'php-file' => [
'bar' => 'foo',
],
'foo_bar' => 'bar foo',
]);

$this->artisan('poeditor:upload')->assertExitCode(0);
}

Expand All @@ -210,6 +312,13 @@ public function it_maps_internal_locale_on_poeditor_locale()
'foo_bar' => 'bar foo',
]);

$this->mockPoeditorDownload('en-gb', [
'en-php-file' => [
'bar' => 'foo',
],
'foo_bar' => 'bar foo',
]);

$this->artisan('poeditor:upload en')->assertExitCode(0);
}

Expand All @@ -221,8 +330,8 @@ public function it_maps_one_of_multiple_internal_locale_on_the_poeditor_locale()
$this->createPhpTranslationFile('nl_NL/nl-php-file.php', ['bar' => 'foo NL']);
$this->createJsonTranslationFile('nl_NL.json', ['foo_bar' => 'bar foo NL']);

$this->createPhpTranslationFile('nl_BE/nl-php-file.php', ['bar' => 'foo BE']);
$this->createJsonTranslationFile('nl_BE.json', ['foo_bar' => 'bar foo BE']);
$this->createPhpTranslationFile('nl_BE/nl-php-file.php', ['bar' => 'foo NL']);
$this->createJsonTranslationFile('nl_BE.json', ['foo_bar' => 'bar foo NL']);

$this->mockPoeditorUpload('nl', [
'nl-php-file' => [
Expand All @@ -231,15 +340,15 @@ public function it_maps_one_of_multiple_internal_locale_on_the_poeditor_locale()
'foo_bar' => 'bar foo NL',
]);

$this->artisan('poeditor:upload nl_NL')->assertExitCode(0);

$this->mockPoeditorUpload('nl', [
$this->mockPoeditorDownload('nl', [
'nl-php-file' => [
'bar' => 'foo BE',
'bar' => 'foo NL',
],
'foo_bar' => 'bar foo BE',
'foo_bar' => 'bar foo NL',
]);

$this->artisan('poeditor:upload nl_NL')->assertExitCode(0);

$this->artisan('poeditor:upload nl_BE')->assertExitCode(0);
}

Expand Down Expand Up @@ -269,11 +378,15 @@ public function it_throws_error_if_default_locale_is_not_present_in_config_local
->expectsOutput('Invalid locale provided!');
}

public function mockPoeditorUpload(string $language, array $translations = [], bool $overwrite = false, UploadResponse $response = null)
public function mockPoeditorUpload(string $language, array $translations = [], bool $overwrite = false, bool $cleanup = false, UploadResponse $response = null)
{
$this->mock(Poeditor::class)
if (get_class(app(Poeditor::class)) === Poeditor::class) {
$this->mock(Poeditor::class);
}

app(Poeditor::class)
->shouldReceive('upload')
->with($language, $translations, $overwrite)
->with($language, $translations, $overwrite, $cleanup)
->andReturn($response ?? $this->getPoeditorUploadResponse());
}

Expand Down
Loading