Skip to content

Commit

Permalink
Laravel 8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Feb 28, 2023
1 parent de0eccd commit 109130c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 22 deletions.
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 13 additions & 4 deletions src/SpreadsheetFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/SpreadsheetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

if (! function_exists('lang_path')) {
function lang_path(string $path = ''): string
{
return resource_path($path);
}
}
4 changes: 2 additions & 2 deletions tests/Commands/GenerateTranslationsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function testRunsParserAndFileGeneratorServices(): void
$generator->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);
}
}
13 changes: 9 additions & 4 deletions tests/SpreadsheetFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/SpreadsheetParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '', ''],
]);

Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/TranslationFileGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<?php return [\r\n'k' => 'v'\r\n];",
file_get_contents($this->filepath)
Expand All @@ -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);
}

Expand Down

0 comments on commit 109130c

Please sign in to comment.