Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json faker #1

Merged
merged 8 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
*/
function is_json(string $path): bool
{
// TODO: tell if $this is JSON when $path is_null
return strcmp(strtolower(substr($path, -5)), '.json') === 0;
}

function rm_tree($dir): bool
{
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? rm_tree("$dir/$file") : unlink("$dir/$file");
}

return rmdir($dir);
}
99 changes: 99 additions & 0 deletions tests/JsonFaker/JsonFaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace ElaborateCode\JsonTongue\Tests\JsonFaker;

use ElaborateCode\JsonTongue\Strategies\File;
use Exception;

class JsonFaker
{
protected string $testingPath;

protected bool $autoRollBack;

protected array $compositions = [];

public function __construct(
protected bool $auto_roll_back = true,
string $temp_path = '/temp/lang'
) {
$tests_dir = new File('/tests');

$this->testingPath = $tests_dir.DIRECTORY_SEPARATOR.$temp_path;

if (! mkdir($this->testingPath, 0777, true)) {
throw new Exception("Could not create a temporary directory /tests/$temp_path");
}
}

/* =================================== */
// Chainables
/* =================================== */

public static function make(bool $auto_roll_back = true, string $temp_path = '/temp/lang'): static
{
return new static($auto_roll_back, $temp_path);
}

public static function makeAndWriteLocals(array $compositions, bool $auto_roll_back = true, string $temp_path = '/temp/lang'): static
{
$static = new static($auto_roll_back, $temp_path);

foreach ($compositions as $lang => $jsons) {
$static->addLocale($lang, $jsons);
}

return $static;
}

public function addLocale(string $lang, array $jsons): static
{
$this->compositions[$lang] = $jsons;

return $this;
}

public function write(): static
{
foreach ($this->compositions as $locale => $jsons_list) {
if (! mkdir($this->testingPath."/$locale")) {
throw new Exception('zo');
}

foreach ($jsons_list as $json_name => $content) {
file_put_contents($this->testingPath."/$locale/$json_name", json_encode($content));
}
}

return $this;
}

public function flush(): static
{
$this->compositions = [];

return $this;
}

/* =================================== */
// Simple getters
/* =================================== */

public function getPath(): string
{
return $this->testingPath;
}

/* =================================== */
//
/* =================================== */

public function rollback(): void
{
if (! realpath($this->testingPath)) {
throw new Exception("Trying a manual rollback with {$this->testingPath} does not exist");
}

rm_tree(dirname($this->testingPath));
}
}
50 changes: 7 additions & 43 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
uses()
->afterEach(function () {
if (isset($this->jsonFaker)) {
$this->jsonFaker->rollback();
}
})
->in('Feature', 'Unit');
31 changes: 30 additions & 1 deletion tests/feature/TongueFacadeTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
<?php

use ElaborateCode\JsonTongue\Tests\JsonFaker\JsonFaker;
use ElaborateCode\JsonTongue\TongueFacade;

it('complete', function () {
$tongue = new TongueFacade('/tests/lang');
$this->jsonFaker = JsonFaker::make()
->addLocale('ar', [
'ar.json' => [],
])
->addLocale('en', [
'en.json' => [
'en' => 'en',
'Super' => 'Super',
],
'one.json' => [
'one' => 'one',
],
'two.json' => [
'two' => 'two',
],
])
->addLocale('multi', [
'greetings.json' => [
'en' => [
'Hello' => 'Hello',
],
'fr' => [
'Hello' => 'Salut',
],
],
])
->write();

$tongue = new TongueFacade($this->jsonFaker->getPath());

$this->assertCount(3, $tongue->transcribe());
$this->assertArrayHasKey('ar', $tongue->transcribe());
Expand Down
1 change: 0 additions & 1 deletion tests/lang/ar/ar.json

This file was deleted.

4 changes: 0 additions & 4 deletions tests/lang/en/en.json

This file was deleted.

3 changes: 0 additions & 3 deletions tests/lang/en/one.json

This file was deleted.

3 changes: 0 additions & 3 deletions tests/lang/en/two.json

This file was deleted.

Empty file removed tests/lang/es/.gitkeep
Empty file.
Empty file removed tests/lang/fr/fr.json
Empty file.
8 changes: 0 additions & 8 deletions tests/lang/multi/greetings.json

This file was deleted.

19 changes: 7 additions & 12 deletions tests/unit/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
it('gets correct project root path', function () {
$root_folder = new File;

$this->assertEquals($root_folder->getProjectRoot(), realpath(__DIR__.'./../../'));
$this->assertEquals($root_folder->getProjectRoot(), realpath(__DIR__.'/../../'));
});

it('gets correct tests folder path', function () {
$tests_folder = (string) new File('tests');
$this->assertEquals((string) new File('tests'), realpath(__DIR__.'/../'));

$this->assertEquals($tests_folder, realpath(__DIR__.'./../'));
$this->assertEquals((string) new File('/tests'), realpath(__DIR__.'/../'));

$tests_folder = new File('/tests');

$this->assertEquals((string) $tests_folder, realpath(__DIR__.'./../'));

$tests_folder = new File('\tests');

$this->assertEquals($tests_folder->getPath(), realpath(__DIR__.'./../'));
// Can test '\test' on windows
$this->assertEquals((new File(DIRECTORY_SEPARATOR.'tests'))->getPath(), realpath(__DIR__.'/../'));
});

it('throws an exception when relative path is invalid', function () {
Expand All @@ -29,8 +24,8 @@
it('scans directories', function () {
$dir = new File();

$this->assertContains(realpath(__DIR__.'./../../tests'), $dir->getDirectoryContent());
$this->assertContains(realpath(__DIR__.'./../../vendor'), $dir->getDirectoryContent());
$this->assertContains(realpath(__DIR__.'/../../tests'), $dir->getDirectoryContent());
$this->assertContains(realpath(__DIR__.'/../../vendor'), $dir->getDirectoryContent());
});

it('throws an exception for calling getDirectoryContent on a file', function () {
Expand Down
22 changes: 9 additions & 13 deletions tests/unit/LangFolderTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
<?php

use ElaborateCode\JsonTongue\Composites\LangFolder;
use ElaborateCode\JsonTongue\Strategies\File;
use ElaborateCode\JsonTongue\Tests\JsonFaker\JsonFaker;

it('lists available locales correctly', function () {
$lang_folder = new LangFolder('/tests/lang');
$this->jsonFaker = JsonFaker::make()
->addLocale('ar', [])
->addLocale('en', [])
->addLocale('es', [])
->addLocale('fr', [])
->write();

$lang_folder = new LangFolder($this->jsonFaker->getPath());

$this->assertContains('ar', $lang_folder->getLocalesList());
$this->assertContains('en', $lang_folder->getLocalesList());
$this->assertContains('es', $lang_folder->getLocalesList());
$this->assertContains('fr', $lang_folder->getLocalesList());
});

// it('traverses the right amount of locale folders', function () {
// $lang_folder = new LangFolder(new File('/tests/lang'));

// $locales_counter = 0;
// foreach ($lang_folder as $json_name => $locale_folder) {
// $locales_counter++;
// }

// $this->assertEquals(4, $locales_counter);
// });
25 changes: 11 additions & 14 deletions tests/unit/LocaleFolderTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

use ElaborateCode\JsonTongue\Composites\LocaleFolder;
use ElaborateCode\JsonTongue\Factories\LocaleFolderFactory;
use ElaborateCode\JsonTongue\Strategies\File;
use ElaborateCode\JsonTongue\Tests\JsonFaker\JsonFaker;

it('sets locale lang correctly', function () {
$this->jsonFaker = JsonFaker::make()
->addLocale('en', [])
->write();

$factory = new LocaleFolderFactory;

$en = new File('/tests/lang/en');
$en = new File($this->jsonFaker->getPath().'/en');

$locale = $factory->make($en);

Expand All @@ -17,22 +21,15 @@
});

it('assert JsonsList', function () {
$this->jsonFaker = JsonFaker::make()
->addLocale('en', ['en.json' => []])
->write();

$factory = new LocaleFolderFactory;

$en = new File('/tests/lang/en');
$en = new File($this->jsonFaker->getPath().'/en');

$locale = $factory->make($en);

$this->assertArrayHasKey('en.json', $locale->getJsonsList());
});

// it('traverses the right amount of locale JSONs', function () {
// $lang_folder = new LocaleFolder(new File('/tests/lang/en'));

// $jsons_counter = 0;
// foreach ($lang_folder as $json_name => $locale_folder) {
// $jsons_counter++;
// }

// $this->assertEquals(3, $jsons_counter);
// });
27 changes: 25 additions & 2 deletions tests/unit/LocaleJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

use ElaborateCode\JsonTongue\Composites\LocaleJson;
use ElaborateCode\JsonTongue\Strategies\File;
use ElaborateCode\JsonTongue\Tests\JsonFaker\JsonFaker;

it('gets JSON content correctly', function () {
$file = new File('/tests/lang/en/en.json');
$this->jsonFaker = JsonFaker::make()
->addLocale('en', [
'en.json' => [
'en' => 'en',
'Super' => 'Super',
],
])
->write();

$file = new File($this->jsonFaker->getPath().'/en/en.json');

$json = new LocaleJson($file);

Expand All @@ -13,7 +23,20 @@
});

it('gets multi JSON content correctly', function () {
$file = new File('/tests/lang/multi/greetings.json');
$this->jsonFaker = JsonFaker::make()
->addLocale('multi', [
'greetings.json' => [
'en' => [
'Hello' => 'Hello',
],
'fr' => [
'Hello' => 'Salut',
],
],
])
->write();

$file = new File($this->jsonFaker->getPath().'/multi/greetings.json');

$json = new LocaleJson($file);

Expand Down
Loading