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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
<?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 testLoadMethodLoadsTranslationsFromAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);
$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', 'messages'));
}
public function testLoadMethodHandlesMissingAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/missing');
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/missing/en/messages.php')->andReturn(false);
$this->assertEquals(['foo' => 'bar'], $loader->load('en', 'messages'));
}
public function testLoadMethodOverwritesExistingKeysFromAddedPath()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['foo' => 'baz']);
$this->assertEquals(['foo' => 'baz'], $loader->load('en', 'messages'));
}
public function testLoadMethodLoadsTranslationsFromMultipleAddedPaths()
{
$files = m::mock(Filesystem::class);
$loader = new FileLoader($files, __DIR__);
$loader->addPath(__DIR__.'/another');
$loader->addPath(__DIR__.'/yet-another');
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(['qux' => 'quux']);
$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash', 'qux' => 'quux'], $loader->load('en', 'messages'));
}
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 testLoadMethodWithoutNamespacesProperlyCallsLoaderWithMultiplePaths()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), [__DIR__, __DIR__.'/second']);
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/second/en/foo.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/foo.php')->andReturn(['messages' => 'first']);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/second/en/foo.php')->andReturn(['messages' => 'second']);
$this->assertEquals(['messages' => 'second'], $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 testLoadMethodWithNamespacesProperlyCallsLoaderWithMultiplePaths()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), [__DIR__, __DIR__.'/second']);
$files->shouldReceive('exists')->once()->with('test-namespace-dir/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(false);
$files->shouldReceive('exists')->once()->with(__DIR__.'/second/vendor/namespace/en/foo.php')->andReturn(false);
$files->shouldReceive('getRequire')->once()->with('test-namespace-dir/en/foo.php')->andReturn(['foo' => 'bar']);
$loader->addNamespace('namespace', 'test-namespace-dir');
$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 testLoadMethodWithNamespacesProperlyCallsLoaderAndLoadsLocalOverridesWithMultiplePaths()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), [__DIR__, __DIR__.'/second']);
$files->shouldReceive('exists')->once()->with('test-namespace-dir/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/second/vendor/namespace/en/foo.php')->andReturn(true);
$files->shouldReceive('getRequire')->once()->with('test-namespace-dir/en/foo.php')->andReturn(['foo' => 'bar']);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/vendor/namespace/en/foo.php')->andReturn(['foo' => 'override', 'baz' => 'boom']);
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/second/vendor/namespace/en/foo.php')->andReturn(['foo' => 'override-2', 'baz' => 'boom-2']);
$loader->addNamespace('namespace', 'test-namespace-dir');
$this->assertEquals(['foo' => 'override-2', 'baz' => 'boom-2'], $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', '*', '*'));
}
public function testLoadMethodThrowExceptionWhenProvideInvalidJSON()
{
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
$loader->addJsonPath(__DIR__.'/invalid');
$invalidJsonString = '.{"foo":"cricket", "baz": "football"}';
$files->shouldReceive('exists')->once()->with(__DIR__.'/invalid/en.json')->andReturn(true);
$files->shouldReceive('get')->once()->with(__DIR__.'/invalid/en.json')->andReturn($invalidJsonString);
$this->expectException(\RuntimeException::class);
$loader->load('en', '*', '*');
}
public function testAllRegisteredNamespaceReturnProperly()
{
$loader = new FileLoader(m::mock(Filesystem::class), __DIR__);
$loader->addNamespace('namespace', 'foo');
$loader->addNamespace('namespace2', 'bar');
$this->assertEquals(['namespace' => 'foo', 'namespace2' => 'bar'], $loader->namespaces());
}
public function testAllAddedJsonPathsReturnProperly()
{
$loader = new FileLoader(m::mock(Filesystem::class), __DIR__);
$path1 = __DIR__.'/another';
$path2 = __DIR__.'/another2';
$loader->addJsonPath($path1);
$loader->addJsonPath($path2);
$this->assertEquals([$path1, $path2], $loader->jsonPaths());
}
public function testAllAddedPathsReturnProperly()
{
$loader = new FileLoader(m::mock(Filesystem::class), __DIR__);
$path1 = __DIR__.'/another';
$path2 = __DIR__.'/another2';
$loader->addPath($path1);
$loader->addPath($path2);
$this->assertEquals([$path1, $path2], array_slice($loader->paths(), 1));
}
}
|