Skip to content

Commit 022034c

Browse files
committed
JsonFaker: base_testing_path+tempTestingPath
1 parent 4b62ce5 commit 022034c

1 file changed

Lines changed: 32 additions & 17 deletions

File tree

src/JsonFaker/JsonFaker.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,48 @@
88

99
class JsonFaker implements JsonFakerContract
1010
{
11+
protected array $compositions = [];
12+
1113
protected string $testingPath;
1214

13-
protected bool $autoRollBack;
14-
15-
protected array $compositions = [];
15+
protected string $baseTestingPath;
1616

1717
public function __construct(
18-
protected bool $auto_roll_back = true,
19-
string $temp_path = '/temp/lang'
18+
protected bool $autoRollBack = true,
19+
protected string $tempTestingPath = '/temp/lang',
20+
string $base_testing_path = '/tests',
2021
) {
21-
$tests_dir = new File('/tests');
22+
// TODO: tempTestingPath cannot be empty
23+
// TODO: baseTestingPath must be a dir
24+
25+
$this->baseTestingPath = new File($base_testing_path);
2226

23-
$this->testingPath = $tests_dir.DIRECTORY_SEPARATOR.$temp_path;
27+
$this->testingPath = $this->baseTestingPath.DIRECTORY_SEPARATOR.$this->tempTestingPath;
2428

2529
if (! mkdir($this->testingPath, 0777, true)) {
26-
throw new Exception("Could not create a temporary directory /tests/$temp_path");
30+
throw new Exception("Could not create a temporary directory /tests/{$this->tempTestingPath}");
2731
}
2832
}
2933

3034
/* =================================== */
3135
// Chainables
3236
/* =================================== */
3337

34-
public static function make(bool $auto_roll_back = true, string $temp_path = '/temp/lang'): static
35-
{
36-
return new static($auto_roll_back, $temp_path);
38+
public static function make(
39+
bool $autoRollBack = true,
40+
string $tempTestingPath = '/temp/lang',
41+
string $base_testing_path = '/tests',
42+
): static {
43+
return new static($autoRollBack, $tempTestingPath, $base_testing_path);
3744
}
3845

39-
public static function makeAndWriteLocals(array $compositions, bool $auto_roll_back = true, string $temp_path = '/temp/lang'): static
40-
{
41-
$static = new static($auto_roll_back, $temp_path);
46+
public static function makeAndWriteLocals(
47+
array $compositions,
48+
bool $autoRollBack = true,
49+
string $tempTestingPath = '/temp/lang',
50+
string $base_testing_path = '/tests',
51+
): static {
52+
$static = new static($autoRollBack, $tempTestingPath, $base_testing_path);
4253

4354
foreach ($compositions as $lang => $jsons) {
4455
$static->addLocale($lang, $jsons);
@@ -91,10 +102,14 @@ public function getPath(): string
91102

92103
public function rollback(): void
93104
{
94-
if (! realpath($this->testingPath)) {
95-
throw new Exception("Trying a manual rollback with {$this->testingPath} does not exist");
105+
$temp_root = explode('/', trim($this->tempTestingPath, '/'))[0] ?? '';
106+
107+
$target = $this->baseTestingPath.DIRECTORY_SEPARATOR.$temp_root;
108+
109+
if (! realpath($target)) {
110+
throw new Exception("Trying a manual rollback with {$target} does not exist");
96111
}
97112

98-
rm_tree(dirname($this->testingPath));
113+
rm_tree($target);
99114
}
100115
}

0 commit comments

Comments
 (0)