From 45423d1d710f5f7e497b2bc372a910772bf99740 Mon Sep 17 00:00:00 2001 From: Sohel Rana Date: Tue, 12 Sep 2023 01:14:05 +0600 Subject: [PATCH] [10.x] Improve test cases and achieve 100% code coverage (#48360) * Add test case method for checking all registered namespace Return Properly * Add test case method for checking all added jsonPaths return properly * Add test case method for checking expected exception when invalid json provide * Fix some coding style! --- .../Translation/TranslationFileLoaderTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index ee1ee417ea2e..e5b3479944ba 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -121,4 +121,35 @@ public function testLoadMethodForJSONProperlyCallsLoaderForMultiplePaths() $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()); + } }