1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
<?php
namespace Illuminate\Tests\Translation;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class TranslationFileLoaderTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testLoadMethodWithoutNamespacesProperlyCallsLoader()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/foo.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/foo.php')->andReturn(['messages']);
$this->assertEquals(['messages'], $loader->load('en', 'foo', null));
}
public function testLoadMethodWithNamespacesProperlyCallsLoader()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('exists')->once()->with('bar/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(false);
$files->shouldReceive('getRequire')->once()->with('bar/en/foo.php')->andReturn(['foo' => 'bar']);
$loader->addNamespace('namespace', 'bar');
$this->assertEquals(['foo' => 'bar'], $loader->load('en', 'foo', 'namespace'));
}
public function testLoadMethodWithNamespacesProperlyCallsLoaderAndLoadsLocalOverrides()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('exists')->once()->with('bar/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with('bar/en/foo.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(['foo' => 'override', 'baz' => 'boom']);
$loader->addNamespace('namespace', 'bar');
$this->assertEquals(['foo' => 'override', 'baz' => 'boom'], $loader->load('en', 'foo', 'namespace'));
}
public function testEmptyArraysReturnedWhenFilesDontExist()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/foo.php')->andReturn(false);
$files->shouldReceive('getRequire')->never();
$this->assertEquals([], $loader->load('en', 'foo', null));
}
public function testEmptyArraysReturnedWhenFilesDontExistForNamespacedItems()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('getRequire')->never();
$this->assertEquals([], $loader->load('en', 'foo', 'bar'));
}
public function testLoadMethodForJSONProperlyCallsLoader()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/en.json')->andReturn(true);
$files->shouldReceive('get')->once()->with(__DIR__.'/en.json')->andReturn('{"foo":"bar"}');
$this->assertEquals(['foo' => 'bar'], $loader->load('en', '*', '*'));
}
public function testLoadMethodForJSONProperlyCallsLoaderForMultiplePaths()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$loader->addJsonPath(__DIR__.'/another');
$files->shouldReceive('exists')->once()->with(__DIR__.'/en.json')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en.json')->andReturn(true);
$files->shouldReceive('get')->once()->with(__DIR__.'/en.json')->andReturn('{"foo":"bar"}');
$files->shouldReceive('get')->once()->with(__DIR__.'/another/en.json')->andReturn('{"foo":"backagebar", "baz": "backagesplash"}');
$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', '*', '*'));
}
}
|