diff --git a/composer.json b/composer.json index b1becd9..bac2d2c 100644 --- a/composer.json +++ b/composer.json @@ -18,23 +18,27 @@ ], "require": { "php": "^8.0", - "guzzlehttp/guzzle": "^7.5", + "guzzlehttp/guzzle": "^7.0", "illuminate/console": "^8.0|^9.0|^10.0", "illuminate/contracts": "^8.0|^9.0|^10.0", "illuminate/http": "^8.0|^9.0|^10.0", "illuminate/support": "^8.0|^9.0|^10.0", - "phpoffice/phpspreadsheet": "^1.27" + "phpoffice/phpspreadsheet": "^1.28" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.14", - "nunomaduro/larastan": "^2.4", + "friendsofphp/php-cs-fixer": "^2.0|^3.14", + "mockery/mockery": "^1.4", + "nunomaduro/larastan": "^1.0|^2.4", "orchestra/testbench": "^6.0|^7.0|^8.0", "phpunit/phpunit": "^8.0|^9.0|^10.0" }, "autoload": { "psr-4": { "Orkhanahmadov\\SpreadsheetTranslations\\": "src" - } + }, + "files": [ + "src/functions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/SpreadsheetFileHandler.php b/src/SpreadsheetFileHandler.php index fece95f..8ad6c58 100644 --- a/src/SpreadsheetFileHandler.php +++ b/src/SpreadsheetFileHandler.php @@ -6,11 +6,14 @@ use Exception; use Illuminate\Contracts\Config\Repository; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Client\Factory; +use Illuminate\Support\Str; class SpreadsheetFileHandler { public function __construct( + protected Application $application, protected Repository $config, protected Factory $http ) { @@ -43,10 +46,11 @@ protected function locallyStoredRemoteFile(): string protected function getRemoteFileContents(): string { - return $this->http - ->throw() - ->get($this->filePathConfig()) - ->body(); + if ($this->laravelVersion() > 8) { + $this->http->throw(); + } + + return $this->http->get($this->filePathConfig())->body(); } protected function isRemoteFile(): bool @@ -58,4 +62,9 @@ protected function filePathConfig(): string { return $this->config->get('spreadsheet-translations.filepath'); } + + protected function laravelVersion(): int + { + return (int) Str::of($this->application->version())->explode('.')->first(); + } } diff --git a/src/SpreadsheetParser.php b/src/SpreadsheetParser.php index 206b7e7..ef4ce7f 100644 --- a/src/SpreadsheetParser.php +++ b/src/SpreadsheetParser.php @@ -120,8 +120,8 @@ protected function parseTranslationKey(RowCellIterator $columnIterator): array $key = Str::of($columnIterator->seek($keyColumn)->current()?->getValue())->trim(); return [ - $key->before('.')->toString(), // filename - $key->after('.')->toString(), // identifier + (string) $key->before('.'), // filename + (string) $key->after('.'), // identifier ]; } diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..5ef8cbb --- /dev/null +++ b/src/functions.php @@ -0,0 +1,10 @@ +shouldReceive('generate')->once()->withArgs(['en', $group]); $this->artisan('translations:generate') - ->expectsOutputToContain('Generating translation files for en...') - ->expectsOutputToContain('Generated translation files for en!') + ->expectsOutput('Generating translation files for en...') + ->expectsOutput('Generated translation files for en!') ->assertExitCode(Command::SUCCESS); } } diff --git a/tests/SpreadsheetFileHandlerTest.php b/tests/SpreadsheetFileHandlerTest.php index 43647e1..4aead0b 100644 --- a/tests/SpreadsheetFileHandlerTest.php +++ b/tests/SpreadsheetFileHandlerTest.php @@ -4,9 +4,8 @@ namespace Orkhanahmadov\SpreadsheetTranslations\Tests; -use Illuminate\Http\Client\Request; +use Illuminate\Http\Client\Factory; use Illuminate\Support\Facades\Config; -use Illuminate\Support\Facades\Http; use Orkhanahmadov\SpreadsheetTranslations\SpreadsheetFileHandler; class SpreadsheetFileHandlerTest extends TestCase @@ -21,11 +20,17 @@ public function testLocalFilePath(): void public function testDownloadsRemoteFileWhenFilePathIsUrl(): void { Config::set('spreadsheet-translations.filepath', $url = 'https://remote-file.com/xlsx'); - Http::fake([$url => Http::response($contents = 'remote file contents')]); + + $http = $this->mock(Factory::class); + + if ($this->laravelVersion() > 8) { + $http->shouldReceive('throw')->once()->withNoArgs(); + } + $http->shouldReceive('get')->once()->with($url)->andReturnSelf(); + $http->shouldReceive('body')->once()->withNoArgs()->andReturn($contents = 'remote file contents'); $path = $this->app->make(SpreadsheetFileHandler::class)->getFilePath(); $this->assertSame($contents, file_get_contents($path)); - Http::assertSent(fn (Request $request) => $request->url() === $url); } } diff --git a/tests/SpreadsheetParserTest.php b/tests/SpreadsheetParserTest.php index 30830e4..0f37a0c 100644 --- a/tests/SpreadsheetParserTest.php +++ b/tests/SpreadsheetParserTest.php @@ -113,7 +113,7 @@ public function testIgnoresEmptyTranslationFieldsInMultipleLocaleTranslations(): ['login.welcome', 'welcome page', 'Welcome', ''], ['login.form.first_name', 'form first name field', '', 'Vorname'], ['dashboard.statistics', 'statistics title', 'Statistics', ''], - ["dashboard.", 'should be ignored', '', ''], + ['dashboard.', 'should be ignored', '', ''], ["\r\n", 'should be ignored', '', ''], ]); diff --git a/tests/TestCase.php b/tests/TestCase.php index de3bd66..07ba79a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ namespace Orkhanahmadov\SpreadsheetTranslations\Tests; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Str; use Orkhanahmadov\SpreadsheetTranslations\SpreadsheetTranslationsServiceProvider; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; @@ -20,6 +21,11 @@ protected function getPackageProviders($app) ]; } + protected function laravelVersion(): int + { + return (int) Str::of($this->app->version())->explode('.')->first(); + } + protected function storeSpreadsheetFile(array $rows): void { $spreadsheet = new Spreadsheet(); diff --git a/tests/TranslationFileGeneratorTest.php b/tests/TranslationFileGeneratorTest.php index ed30080..84c0c93 100644 --- a/tests/TranslationFileGeneratorTest.php +++ b/tests/TranslationFileGeneratorTest.php @@ -13,13 +13,13 @@ class TranslationFileGeneratorTest extends TestCase public function testGeneratesTranslationFiles(): void { - $this->assertFileDoesNotExist($this->filepath); + $this->assertFalse(file_exists($this->filepath)); $this->generator->generate('en', [ 'file' => ['k' => 'v'], ]); - $this->assertFileExists($this->filepath); + $this->assertTrue(file_exists($this->filepath)); $this->assertSame( " 'v'\r\n];", file_get_contents($this->filepath) @@ -40,11 +40,11 @@ public function testEscapesSingleQuiteCharacters(): void public function testCreatesFolderWhenDoesNotExist(): void { - $this->assertFileDoesNotExist($filepath = lang_path('fr')); + $this->assertFalse(file_exists($filepath = lang_path('fr'))); $this->generator->generate('fr', []); - $this->assertFileExists($filepath); + $this->assertTrue(file_exists($filepath)); rmdir($filepath); }