Skip to content

Commit

Permalink
fix: remove the usage of storage facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed Mar 3, 2024
1 parent 4d0763e commit 43d1366
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
16 changes: 7 additions & 9 deletions src/Linguist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Hyperlinkgroup\Linguist;

use Hyperlinkgroup\Linguist\Exceptions\ConfigBrokenException;
use Hyperlinkgroup\Linguist\Exceptions\DirectoryCreationException;
use Hyperlinkgroup\Linguist\Exceptions\NoLanguageActivatedException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class Linguist
Expand All @@ -32,7 +31,6 @@ public function __construct()

/**
* @throws ConfigBrokenException
* @throws DirectoryCreationException
* @throws NoLanguageActivatedException
*/
public function handle(): void
Expand Down Expand Up @@ -167,7 +165,7 @@ protected function ensureDirectoriesExist(): void
$paths->push(storage_path($this->getTemporaryDirectory()));

$paths->each(function ($path) {
Storage::createDirectory($path);
File::ensureDirectoryExists($path);
});
}

Expand Down Expand Up @@ -215,7 +213,7 @@ public function downloadFiles(): self
$response = $this->getHttp()
->get($route);

Storage::put(storage_path($this->getTemporaryDirectory() . "/$language.json"), $response->body());
File::put(storage_path($this->getTemporaryDirectory() . "/$language.json"), $response->body());
}

return $this;
Expand All @@ -226,16 +224,16 @@ public function downloadFiles(): self
*/
public function moveFiles(): self
{
$files = Storage::files(storage_path($this->getTemporaryDirectory()));
$files = File::files(storage_path($this->getTemporaryDirectory()));

foreach ($files as $file) {
$language = Str::beforeLast(Str::afterLast($file, '/'), '.');
$language = $file->getFilenameWithoutExtension();
$destination = base_path("lang/$language/$this->project.json");

Storage::move($file, $destination);
File::move($file, $destination);
}

Storage::deleteDirectory(storage_path($this->getTemporaryDirectory()));
File::deleteDirectory(storage_path($this->getTemporaryDirectory()));

return $this;
}
Expand Down
65 changes: 48 additions & 17 deletions tests/LinguistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,46 @@
use Hyperlinkgroup\Linguist\Exceptions\NoLanguageActivatedException;
use Hyperlinkgroup\Linguist\Facades\Linguist;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

use function PHPUnit\Framework\assertFileDoesNotExist;
use function PHPUnit\Framework\assertFileExists;

beforeEach(function () {
cleanUp();
});

afterEach(function () {
cleanUp();
});

function cleanUp(): void
{
if (File::exists(base_path('lang'))) {
collect(File::files(base_path('lang')))->each(function (SplFileInfo $file) {
File::delete($file->getPathname());
});

File::deleteDirectory(base_path('lang'));
}

if (File::exists(storage_path('tmp/translations'))) {
collect(File::files(storage_path('tmp/translations')))->each(function (SplFileInfo $file) {
File::delete($file->getPathname());
});
}

if (File::exists(storage_path('tmp'))) {
collect(File::files(storage_path('tmp')))->each(function (SplFileInfo $file) {
File::delete($file->getPathname());
});
}

File::deleteDirectory(storage_path('tmp'));
}

test('that we get an exception when project is not set', function () {
config(['linguist.project' => '']);

Expand Down Expand Up @@ -57,8 +94,6 @@
})->throws(NoLanguageActivatedException::class);

test('that we can create directories', function () {
Storage::fake('local');

$languages = collect(['EN', 'DE']);
config(['linguist.temporary_directory' => 'tmp/translations']);

Expand All @@ -67,15 +102,13 @@
->createDirectories();

$languages->each(function (string $language) {
Storage::assertExists(base_path("lang/$language"));
assertFileExists(base_path("lang/$language"));
});

Storage::assertExists(storage_path(config('linguist.temporary_directory')));
assertFileExists(storage_path(config('linguist.temporary_directory')));
});

test('that we can download the files', function () {
Storage::fake('local');

$languages = collect(['EN', 'DE']);
config(['linguist.temporary_directory' => 'tmp/translations']);
config(['linguist.project' => 'project']);
Expand All @@ -100,16 +133,15 @@

Linguist::start()
->setLanguages($languages)
->createDirectories()
->downloadFiles();

$languages->each(function (string $language) {
Storage::assertExists(storage_path(config('linguist.temporary_directory') . "/$language.json"));
assertFileExists(storage_path(config('linguist.temporary_directory') . "/$language.json"));
});
});

test('that we can move the files', function () {
Storage::fake('local');

$languages = collect(['EN', 'DE']);
config(['linguist.temporary_directory' => 'tmp/translations']);
config(['linguist.project' => 'project']);
Expand All @@ -118,22 +150,21 @@
->setLanguages($languages)
->createDirectories();

Storage::put(storage_path('tmp/translations/EN.json'), json_encode(['Test' => 'Test English Translation'], JSON_THROW_ON_ERROR));
Storage::put(storage_path('tmp/translations/DE.json'), json_encode(['Test' => 'Test German Translation'], JSON_THROW_ON_ERROR));
File::put(storage_path('tmp/translations/EN.json'), json_encode(['Test' => 'Test English Translation'], JSON_THROW_ON_ERROR));
File::put(storage_path('tmp/translations/DE.json'), json_encode(['Test' => 'Test German Translation'], JSON_THROW_ON_ERROR));

Linguist::start()
->moveFiles();

$languages->each(function (string $language) {
Storage::assertExists(base_path("lang/$language/project.json"));
assertFileExists(base_path("lang/$language/project.json"));
});

Storage::assertMissing(storage_path(config('linguist.temporary_directory')));
assertFileDoesNotExist(storage_path(config('linguist.temporary_directory')));
});

test('that we can execute the command', function () {
Storage::fake('local');

config(['linguist.temporary_directory' => 'tmp/translations']);
config(['linguist.project' => 'project']);
config(['linguist.token' => 'token']);

Expand Down Expand Up @@ -162,8 +193,8 @@
Linguist::start()->handle();

$languages->each(function (string $language) {
Storage::assertExists(base_path("lang/$language/project.json"));
assertFileExists(base_path("lang/$language/project.json"));
});

Storage::assertMissing(storage_path(config('linguist.temporary_directory')));
assertFileDoesNotExist(storage_path(config('linguist.temporary_directory')));
});

0 comments on commit 43d1366

Please sign in to comment.