Skip to content

Commit a355df6

Browse files
committed
Add tests for the cache functionality as well as for the artisan command
1 parent 91ee11b commit a355df6

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Krisell\LaravelTranslationJsonCache\Tests\TestCase;
4+
5+
class ClearJsonCacheCommandTest extends TestCase
6+
{
7+
/** @test */
8+
public function the_artisan_commands_prints_output_after_being_done()
9+
{
10+
$this->artisan("translation-json-cache:clear")
11+
->expectsOutput("Translation JSON cache files cleared.");
12+
}
13+
14+
/** @test */
15+
public function the_artisan_commands_clear_cache_files()
16+
{
17+
Storage::put('translation-cache-en.php', 'a');
18+
Storage::put('translation-cache-sv.php', 'b');
19+
Storage::put('translation-cache-fr.php', 'c');
20+
21+
$this->assertCount(3, collect(Storage::files())->filter(function ($filename) {
22+
return preg_match('/.*-cache-.*/', $filename);
23+
}));
24+
25+
$this->artisan("translation-json-cache:clear");
26+
27+
$this->assertCount(0, collect(Storage::files())->filter(function ($filename) {
28+
return preg_match('/.*-cache-.*/', $filename);
29+
}));
30+
}
31+
}
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
use Krisell\LaravelTranslationJsonCache\Tests\TestCase;
4+
5+
class TranslationJsonCacheTest extends TestCase
6+
{
7+
/** @test */
8+
function the_cache_file_is_created_when_the_env_variable_is_true()
9+
{
10+
config(['translation-json-cache.active' => true]);
11+
12+
__("Test");
13+
14+
$this->assertTrue(Storage::exists('translation-cache-en.php'));
15+
}
16+
17+
/** @test */
18+
function the_cache_file_is_not_created_when_the_env_variable_is_false ()
19+
{
20+
config(['translation-json-cache.active' => false]);
21+
22+
__("Test");
23+
24+
$this->assertFalse(Storage::exists('translation-cache-en.php'));
25+
}
26+
27+
/** @test */
28+
function the_cache_file_is_not_created_when_the_env_variable_is_missing ()
29+
{
30+
config(['translation-json-cache.active' => null]);
31+
32+
__("Test");
33+
34+
$this->assertFalse(Storage::exists('translation-cache-en.php'));
35+
}
36+
37+
/** @test */
38+
function the_cache_file_is_used_when_the_env_variable_is_set_to_true ()
39+
{
40+
Storage::put('translation-cache-en.php', '<?php return ["Test" => "Testing"];');
41+
config(['translation-json-cache.active' => true]);
42+
43+
$this->assertEquals("Testing", __("Test"));
44+
}
45+
46+
/** @test */
47+
function the_cache_file_is_not_used_when_the_env_variable_is_set_to_false ()
48+
{
49+
Storage::put('translation-cache-en.php', '<?php return ["Test" => "Testing"];');
50+
config(['translation-json-cache.active' => false]);
51+
52+
$this->assertEquals("Test", __("Test"));
53+
}
54+
55+
/** @test */
56+
function the_cache_file_is_not_used_when_the_env_variable_is_missing ()
57+
{
58+
Storage::put('translation-cache-en.php', '<?php return ["Test" => "Testing"];');
59+
config(['translation-json-cache.active' => null]);
60+
61+
$this->assertEquals("Test", __("Test"));
62+
}
63+
64+
/** @test */
65+
function the_data_in_the_cached_file_corresponds_to_the_json_file ()
66+
{
67+
file_put_contents(base_path() . '/resources/lang/en.json', '{"Test":"Testing"}');
68+
config(['translation-json-cache.active' => true]);
69+
70+
$this->assertEquals("Testing", __("Test"));
71+
72+
$data = require storage_path() . '/app/translation-cache-en.php';
73+
$this->assertEquals('Testing', $data['Test']);
74+
}
75+
76+
/** @test */
77+
function the_cached_file_takes_presendece_if_it_differs_from_the_json_file ()
78+
{
79+
file_put_contents(base_path() . '/resources/lang/en.json', '{"Test":"Testing"}');
80+
file_put_contents(storage_path() . '/app/translation-cache-en.php', '<?php return ["Test" => "Testing2"];');
81+
config(['translation-json-cache.active' => true]);
82+
83+
$this->assertEquals("Testing2", __("Test"));
84+
}
85+
}

tests/TestCase.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
namespace Krisell\LaravelTranslationJsonCache\Tests;
44

5+
use Illuminate\Support\Facades\Storage;
56
use Krisell\LaravelTranslationJsonCache\TranslationJsonCacheServiceProvider;
6-
7+
use Krisell\LaravelTranslationJsonCache\ClearJsonCacheCommandServiceProvider;
78

89
class TestCase extends \Orchestra\Testbench\TestCase
910
{
1011
public function setUp(): void
1112
{
1213
parent::setUp();
14+
15+
app()->setLocale('en');
16+
$jsonPath = base_path() . '/resources/lang/en.json';
17+
18+
if (file_exists($jsonPath)) {
19+
unlink($jsonPath);
20+
}
21+
22+
Storage::delete('translation-cache-en.php');
1323
}
1424

1525
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)