Skip to content

Commit

Permalink
Merge pull request #21 from nextapps-be/feature/simplify-tests
Browse files Browse the repository at this point in the history
Simplify tests
  • Loading branch information
gdebrauwer authored Feb 5, 2024
2 parents 69f22a0 + 798c7b8 commit f52d188
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 191 deletions.
164 changes: 59 additions & 105 deletions tests/DownloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Filesystem\Filesystem;
use NextApps\PoeditorSync\Poeditor\Poeditor;
use Symfony\Component\VarExporter\VarExporter;

class DownloadCommandTest extends TestCase
{
Expand Down Expand Up @@ -41,10 +40,10 @@ public function it_saves_php_translations_of_every_locale()

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

$this->assertPhpTranslationFile(lang_path('en/first-en-php-file.php'), ['foo' => 'bar']);
$this->assertPhpTranslationFile(lang_path('en/second-en-php-file.php'), ['foo_bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl/first-nl-php-file.php'), ['bar_foo' => 'foo bar']);
$this->assertPhpTranslationFile(lang_path('nl/second-nl-php-file.php'), ['bar' => 'foo']);
$this->assertPhpTranslationFile('en/first-en-php-file.php', ['foo' => 'bar']);
$this->assertPhpTranslationFile('en/second-en-php-file.php', ['foo_bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl/first-nl-php-file.php', ['bar_foo' => 'foo bar']);
$this->assertPhpTranslationFile('nl/second-nl-php-file.php', ['bar' => 'foo']);
}

/** @test */
Expand All @@ -64,11 +63,11 @@ public function it_saves_json_translations_of_every_locale()

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

$this->assertJsonTranslationFile(lang_path('en.json'), [
$this->assertJsonTranslationFile('en.json', [
'first-en-json-key' => 'bar',
'second-en-json-key' => 'bar foo',
]);
$this->assertJsonTranslationFile(lang_path('nl.json'), [
$this->assertJsonTranslationFile('nl.json', [
'first-nl-json-key' => 'foo bar',
'second-nl-json-key' => 'foo',
]);
Expand Down Expand Up @@ -119,39 +118,29 @@ public function it_saves_vendor_php_and_json_translations_of_every_locale()

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

$this->assertFalse(file_exists(lang_path('en/vendor.php')));
$this->assertFalse(file_exists(lang_path('nl/vendor.php')));
$this->assertTranslationFileDoesntExist('en/vendor.php');
$this->assertTranslationFileDoesntExist('nl/vendor.php');

$this->assertPhpTranslationFile(
lang_path('vendor/first-package/en/first-package-en-php-file.php'),
['foo' => 'bar']
);
$this->assertJsonTranslationFile(lang_path('vendor/first-package/en.json'), [
$this->assertPhpTranslationFile('vendor/first-package/en/first-package-en-php-file.php', ['foo' => 'bar']);
$this->assertPhpTranslationFile('vendor/second-package/en/second-package-en-php-file.php', ['bar' => 'foo']);

$this->assertJsonTranslationFile('vendor/first-package/en.json', [
'first-package-first-en-json-key' => 'bar foo',
'first-package-second-en-json-key' => 'foo bar',
]);
$this->assertPhpTranslationFile(
lang_path('vendor/second-package/en/second-package-en-php-file.php'),
['bar' => 'foo']
);
$this->assertJsonTranslationFile(lang_path('vendor/second-package/en.json'), [
$this->assertJsonTranslationFile('vendor/second-package/en.json', [
'second-package-first-en-json-key' => 'bar foo bar',
'second-package-second-en-json-key' => 'foo bar foo',
]);

$this->assertPhpTranslationFile(
lang_path('vendor/first-package/nl/first-package-nl-php-file.php'),
['bar' => 'foo']
);
$this->assertJsonTranslationFile(lang_path('vendor/first-package/nl.json'), [
$this->assertPhpTranslationFile('vendor/first-package/nl/first-package-nl-php-file.php', ['bar' => 'foo']);
$this->assertPhpTranslationFile('vendor/second-package/nl/second-package-nl-php-file.php', ['foo' => 'bar']);

$this->assertJsonTranslationFile('vendor/first-package/nl.json', [
'first-package-first-nl-json-key' => 'foo bar',
'first-package-second-nl-json-key' => 'bar foo',
]);
$this->assertPhpTranslationFile(
lang_path('vendor/second-package/nl/second-package-nl-php-file.php'),
['foo' => 'bar']
);
$this->assertJsonTranslationFile(lang_path('vendor/second-package/nl.json'), [
$this->assertJsonTranslationFile('vendor/second-package/nl.json', [
'second-package-first-nl-json-key' => 'foo bar foo',
'second-package-second-nl-json-key' => 'bar foo bar',
]);
Expand All @@ -177,18 +166,18 @@ public function it_saves_php_and_json_and_vendor_translations_of_locale()

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

$this->assertFalse(file_exists(lang_path('en/vendor.php')));
$this->assertTranslationFileDoesntExist('en/vendor.php');

$this->assertPhpTranslationFile(lang_path('en/php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en.json'), ['json-key' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('vendor/package-name/en/package-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('vendor/package-name/en.json'), ['package-json-key' => 'foo bar foo']);
$this->assertPhpTranslationFile('en/php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en.json', ['json-key' => 'bar foo']);
$this->assertPhpTranslationFile('vendor/package-name/en/package-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('vendor/package-name/en.json', ['package-json-key' => 'foo bar foo']);
}

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

$this->mockPoeditorDownload('en', [
'new-php-file' => [
Expand All @@ -198,15 +187,15 @@ public function it_removes_old_php_translations_of_locale()

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

$this->assertFalse(file_exists(lang_path('en/old-php-file.php')));
$this->assertTrue(file_exists(lang_path('en/new-php-file.php')));
$this->assertTranslationFileDoesntExist('en/old-php-file.php');
$this->assertTranslationFileExists('en/new-php-file.php');
}

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

config()->set('poeditor-sync.excluded_files', ['excluded-php-file.php']);

Expand All @@ -218,29 +207,27 @@ public function it_does_not_remove_php_translations_of_locale_if_php_translation

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

$this->assertTrue(file_exists(lang_path('en/excluded-php-file.php')));
$this->assertFalse(file_exists(lang_path('en/not-excluded-php-file.php')));
$this->assertTrue(file_exists(lang_path('en/some-php-file.php')));
$this->assertTranslationFileExists('en/excluded-php-file.php');
$this->assertTranslationFileDoesntExist('en/not-excluded-php-file.php');
$this->assertTranslationFileExists('en/some-php-file.php');
}

/** @test */
public function it_overrides_old_json_translation_of_locale()
{
file_put_contents(lang_path('en.json'), json_encode(['foo' => 'bar'], JSON_PRETTY_PRINT));
$this->createJsonTranslationFile('en.json', ['foo' => 'bar']);

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

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

$this->assertJsonTranslationFile(lang_path('en.json'), ['bar' => 'foo']);
$this->assertJsonTranslationFile('en.json', ['bar' => 'foo']);
}

/** @test */
public function it_removes_old_php_vendor_translations_of_locale()
{
app(Filesystem::class)->makeDirectory(lang_path('vendor/package-name/en/'), 0755, true);

file_put_contents(lang_path('vendor/package-name/en/old-php-file.php'), ['foo' => 'bar']);
$this->createPhpTranslationFile('vendor/package-name/en/old-php-file.php', ['foo' => 'bar']);

$this->mockPoeditorDownload('en', [
'vendor' => [
Expand All @@ -254,16 +241,14 @@ public function it_removes_old_php_vendor_translations_of_locale()

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

$this->assertFalse(file_exists(lang_path('vendor/package-name/en/old-php-file.php')));
$this->assertTrue(file_exists(lang_path('vendor/package-name/en/new-php-file.php')));
$this->assertTranslationFileDoesntExist('vendor/package-name/en/old-php-file.php');
$this->assertTranslationFileExists('vendor/package-name/en/new-php-file.php');
}

/** @test */
public function it_overrides_old_json_vendor_translations_of_locale()
{
app(Filesystem::class)->makeDirectory(lang_path('vendor/package-name'), 0755, true);

file_put_contents(lang_path('vendor/package-name/en.json'), json_encode(['foo' => 'bar'], JSON_PRETTY_PRINT));
$this->createJsonTranslationFile('vendor/package-name/en.json', ['foo' => 'bar']);

$this->mockPoeditorDownload('en', [
'vendor' => [
Expand All @@ -275,7 +260,7 @@ public function it_overrides_old_json_vendor_translations_of_locale()

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

$this->assertJsonTranslationFile(lang_path('vendor/package-name/en.json'), ['bar' => 'foo']);
$this->assertJsonTranslationFile('vendor/package-name/en.json', ['bar' => 'foo']);
}

/** @test */
Expand All @@ -300,12 +285,12 @@ public function it_does_not_save_vendor_translations_if_disabled_in_config()

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

$this->assertFalse(file_exists(lang_path('vendor/package-name/en/php-file.php')));
$this->assertFalse(file_exists(lang_path('vendor/package-name/en.json')));
$this->assertFalse(file_exists(lang_path('en/vendor.php')));
$this->assertTranslationFileDoesntExist('vendor/package-name/en/php-file.php');
$this->assertTranslationFileDoesntExist('vendor/package-name/en.json');
$this->assertTranslationFileDoesntExist('en/vendor.php');

$this->assertTrue(file_exists(lang_path('en/php-file.php')));
$this->assertTrue(file_exists(lang_path('en.json')));
$this->assertTranslationFileExists('en/php-file.php');
$this->assertTranslationFileExists('en.json');
}

/** @test */
Expand All @@ -319,8 +304,8 @@ public function it_does_not_create_json_translation_file_if_no_json_translations

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

$this->assertTrue(file_exists(lang_path('en/php-file.php')));
$this->assertFalse(file_exists(lang_path('en.json')));
$this->assertTranslationFileExists('en/php-file.php');
$this->assertTranslationFileDoesntExist('en.json');
}

/** @test */
Expand All @@ -344,10 +329,10 @@ public function it_maps_poeditor_locales_on_internal_locales()

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

$this->assertPhpTranslationFile(lang_path('en/en-php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en.json'), ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl.json'), ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile('en/en-php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en.json', ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl.json', ['bar foo' => 'foo bar']);
}

/** @test */
Expand All @@ -372,12 +357,12 @@ public function it_maps_poeditor_locales_on_multiple_internal_locales()

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

$this->assertPhpTranslationFile(lang_path('en_GB/en-php-file.php'), ['foo' => 'bar']);
$this->assertJsonTranslationFile(lang_path('en_GB.json'), ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile(lang_path('nl_BE/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl_BE.json'), ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile(lang_path('nl_NL/nl-php-file.php'), ['bar' => 'foo']);
$this->assertJsonTranslationFile(lang_path('nl_NL.json'), ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile('en_GB/en-php-file.php', ['foo' => 'bar']);
$this->assertJsonTranslationFile('en_GB.json', ['foo bar' => 'bar foo']);
$this->assertPhpTranslationFile('nl_BE/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl_BE.json', ['bar foo' => 'foo bar']);
$this->assertPhpTranslationFile('nl_NL/nl-php-file.php', ['bar' => 'foo']);
$this->assertJsonTranslationFile('nl_NL.json', ['bar foo' => 'foo bar']);
}

/** @test */
Expand All @@ -400,43 +385,12 @@ public function it_automatically_runs_validate_command_after_download_if_configu
->assertExitCode(0);
}

/**
* Mock the POEditor "download" method.
*
* @param string $language
* @param array $data
*
* @return void
*/
public function mockPoeditorDownload(string $language, array $data)
public function mockPoeditorDownload(string $language, array $data) : void
{
if (get_class(app(Poeditor::class)) !== Poeditor::class) {
app(Poeditor::class)->shouldReceive('download')
->with($language)
->andReturn($data);

return;
if (get_class(app(Poeditor::class)) === Poeditor::class) {
$this->mock(Poeditor::class);
}

$this->mock(Poeditor::class, function ($mock) use ($language, $data) {
$mock->shouldReceive('download')
->with($language)
->andReturn($data);
});
}

public function assertPhpTranslationFile(string $filename, array $data) : void
{
$this->assertTrue(file_exists($filename));
$this->assertEquals(
'<?php' . PHP_EOL . PHP_EOL . 'return ' . VarExporter::export($data) . ';' . PHP_EOL,
file_get_contents($filename)
);
}

public function assertJsonTranslationFile(string $filename, array $data) : void
{
$this->assertTrue(file_exists($filename));
$this->assertEquals(json_encode($data, JSON_PRETTY_PRINT), file_get_contents($filename));
app(Poeditor::class)->shouldReceive('download')->with($language)->andReturn($data);
}
}
47 changes: 47 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace NextApps\PoeditorSync\Tests;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use NextApps\PoeditorSync\PoeditorSyncServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Symfony\Component\VarExporter\VarExporter;

class TestCase extends BaseTestCase
{
Expand All @@ -27,4 +29,49 @@ protected function getPackageProviders($app) : array
PoeditorSyncServiceProvider::class,
];
}

public function createPhpTranslationFile(string $filename, array $data) : void
{
$filename = lang_path($filename);

app(Filesystem::class)->ensureDirectoryExists(dirname($filename));
app(Filesystem::class)->put($filename, '<?php' . PHP_EOL . PHP_EOL . 'return ' . VarExporter::export($data) . ';' . PHP_EOL);
}

public function createJsonTranslationFile(string $filename, array $data) : void
{
$filename = lang_path($filename);

app(Filesystem::class)->ensureDirectoryExists(dirname($filename));
app(Filesystem::class)->put($filename, json_encode($data, JSON_PRETTY_PRINT));
}

public function assertTranslationFileExists(string $filename) : void
{
$this->assertTrue(file_exists(lang_path($filename)));
}

public function assertTranslationFileDoesntExist(string $filename) : void
{
$this->assertFalse(file_exists(lang_path($filename)));
}

public function assertPhpTranslationFile(string $filename, array $data) : void
{
$filename = lang_path($filename);

$this->assertTrue(file_exists($filename));
$this->assertEquals(
'<?php' . PHP_EOL . PHP_EOL . 'return ' . VarExporter::export($data) . ';' . PHP_EOL,
file_get_contents($filename)
);
}

public function assertJsonTranslationFile(string $filename, array $data) : void
{
$filename = lang_path($filename);

$this->assertTrue(file_exists($filename));
$this->assertEquals(json_encode($data, JSON_PRETTY_PRINT), file_get_contents($filename));
}
}
Loading

0 comments on commit f52d188

Please sign in to comment.