|
8 | 8 |
|
9 | 9 | class JsonFaker implements JsonFakerContract |
10 | 10 | { |
| 11 | + protected array $compositions = []; |
| 12 | + |
11 | 13 | protected string $testingPath; |
12 | 14 |
|
13 | | - protected bool $autoRollBack; |
14 | | - |
15 | | - protected array $compositions = []; |
| 15 | + protected string $baseTestingPath; |
16 | 16 |
|
17 | 17 | 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', |
20 | 21 | ) { |
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); |
22 | 26 |
|
23 | | - $this->testingPath = $tests_dir.DIRECTORY_SEPARATOR.$temp_path; |
| 27 | + $this->testingPath = $this->baseTestingPath.DIRECTORY_SEPARATOR.$this->tempTestingPath; |
24 | 28 |
|
25 | 29 | 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}"); |
27 | 31 | } |
28 | 32 | } |
29 | 33 |
|
30 | 34 | /* =================================== */ |
31 | 35 | // Chainables |
32 | 36 | /* =================================== */ |
33 | 37 |
|
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); |
37 | 44 | } |
38 | 45 |
|
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); |
42 | 53 |
|
43 | 54 | foreach ($compositions as $lang => $jsons) { |
44 | 55 | $static->addLocale($lang, $jsons); |
@@ -91,10 +102,14 @@ public function getPath(): string |
91 | 102 |
|
92 | 103 | public function rollback(): void |
93 | 104 | { |
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"); |
96 | 111 | } |
97 | 112 |
|
98 | | - rm_tree(dirname($this->testingPath)); |
| 113 | + rm_tree($target); |
99 | 114 | } |
100 | 115 | } |
0 commit comments