blessing-skin-server/tests/ServicesTest/TranslationsTest/JavaScriptTest.php

125 lines
3.9 KiB
PHP
Raw Normal View History

2019-09-07 17:18:58 +08:00
<?php
namespace Tests;
2020-06-23 18:17:16 +08:00
use App\Services\Plugin;
use App\Services\PluginManager;
2019-12-14 11:10:37 +08:00
use App\Services\Translations\JavaScript;
2019-09-07 17:18:58 +08:00
use Illuminate\Cache\Repository;
use Illuminate\Filesystem\Filesystem;
2019-12-14 11:10:37 +08:00
use Illuminate\Support\Str;
2019-09-07 17:18:58 +08:00
class JavaScriptTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
2020-06-23 18:17:16 +08:00
app()->forgetInstance(JavaScript::class);
2019-09-07 17:18:58 +08:00
}
public function testGenerateFreshFile()
{
2020-06-23 18:17:16 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('getEnabledPlugins')
->with()
->once()
->andReturn(collect([new Plugin('/reina', ['namespace' => 'れいな'])]));
});
2019-09-07 17:18:58 +08:00
$this->mock(Filesystem::class, function ($mock) {
2020-06-23 18:17:16 +08:00
$mock->shouldReceive('exists')
->with('/reina/lang/en/front-end.yml')
->once()
->andReturn(true);
2019-09-07 17:18:58 +08:00
$mock->shouldReceive('lastModified')
->with(resource_path('lang/en/front-end.yml'))
->once()
->andReturn(1);
2020-06-23 18:17:16 +08:00
$mock->shouldReceive('lastModified')
->with('/reina/lang/en/front-end.yml')
->once()
->andReturn(2);
2019-09-07 17:18:58 +08:00
$mock->shouldReceive('put')
2019-09-07 21:23:38 +08:00
->withArgs(function ($path, $content) {
$this->assertEquals(public_path('lang/en.js'), $path);
$this->assertTrue(Str::startsWith($content, 'blessing.i18n'));
2020-06-23 18:17:16 +08:00
$this->assertStringContainsString('"れいな::front-end"', $content);
2019-12-14 11:10:37 +08:00
2019-09-07 21:23:38 +08:00
return true;
})
2019-09-07 17:18:58 +08:00
->once()
->andReturn(1);
});
$this->mock(Repository::class, function ($mock) {
$mock->shouldReceive('get')
->with('front-end-trans-en', 0)
->once()
->andReturn(0);
$mock->shouldReceive('put')
2020-06-23 18:17:16 +08:00
->with('front-end-trans-en', 2)
2019-09-07 17:18:58 +08:00
->once();
});
2020-06-23 18:17:16 +08:00
$this->assertEquals(
url('lang/en.js?t=2'),
resolve(JavaScript::class)->generate('en')
);
2019-09-07 17:18:58 +08:00
}
public function testGenerateCached()
{
2020-06-23 18:17:16 +08:00
$this->mock(PluginManager::class, function ($mock) {
$mock->shouldReceive('getEnabledPlugins')
->with()
->once()
->andReturn(collect([new Plugin('/reina', ['namespace' => 'れいな'])]));
});
2019-09-07 17:18:58 +08:00
$this->mock(Filesystem::class, function ($mock) {
2020-06-23 18:17:16 +08:00
$mock->shouldReceive('exists')
->with('/reina/lang/en/front-end.yml')
->once()
->andReturn(true);
2019-09-07 17:18:58 +08:00
$mock->shouldReceive('lastModified')
->with(resource_path('lang/en/front-end.yml'))
->once()
->andReturn(1);
2020-06-23 18:17:16 +08:00
$mock->shouldReceive('lastModified')
->with('/reina/lang/en/front-end.yml')
->once()
->andReturn(2);
2019-09-07 17:18:58 +08:00
$mock->shouldReceive('exists')
->with(public_path('lang/en.js'))
->once()
->andReturn(true);
});
$this->mock(Repository::class, function ($mock) {
$mock->shouldReceive('get')
->with('front-end-trans-en', 0)
->once()
2020-06-23 18:17:16 +08:00
->andReturn(2);
2019-09-07 17:18:58 +08:00
});
2020-06-23 18:17:16 +08:00
$this->assertEquals(
url('lang/en.js?t=2'),
resolve(JavaScript::class)->generate('en')
);
2019-09-07 17:18:58 +08:00
}
2019-09-08 18:57:19 +08:00
public function testResetTime()
{
$this->spy(Repository::class, function ($spy) {
$spy->shouldReceive('put')
->with('front-end-trans-en', 0)
->once();
});
resolve(JavaScript::class)->resetTime('en');
}
2019-11-26 16:53:19 +08:00
public function testFallbackLocale()
{
$this->get('/', ['Accept-Language' => 'xyz'])
->assertSuccessful()
->assertSee('lang/en.js');
}
2019-09-07 17:18:58 +08:00
}