|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use DirectoryIterator; |
|
6
|
|
|
use FilesystemIterator; |
|
7
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
8
|
|
|
use Illuminate\Support\Facades\Schema; |
|
9
|
|
|
use Shamaseen\Repository\RepositoryServiceProvider; |
|
10
|
|
|
|
|
11
|
|
|
class TestCase extends \Orchestra\Testbench\TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $databaseName = 'tests'; |
|
14
|
|
|
protected string $modelName = 'Test'; |
|
15
|
|
|
protected string $userPath = 'Tests'; |
|
16
|
|
|
|
|
17
|
|
|
protected array $configs = [ |
|
18
|
|
|
'repository.base_path' => 'app', |
|
19
|
|
|
'repository.stubs_path' => __DIR__.'/results/resources/stubs', |
|
20
|
|
|
'repository.lang_path' => __DIR__.'/results/resources/lang', |
|
21
|
|
|
'repository.controllers_path' => 'Http/Controllers', |
|
22
|
|
|
'repository.repositories_path' => 'Repositories', |
|
23
|
|
|
'repository.models_path' => 'Models', |
|
24
|
|
|
'repository.requests_path' => 'Http/Requests', |
|
25
|
|
|
'repository.json_resources_path' => 'Http/Resources', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp(): void |
|
29
|
|
|
{ |
|
30
|
|
|
parent::setUp(); |
|
31
|
|
|
$this->app->setBasePath(realpath(__DIR__).'/results'); |
|
32
|
|
|
$this->deletePathIfExists(realpath(__DIR__).'/results'); |
|
33
|
|
|
$this->makePathIfNotExist(realpath(__DIR__).'/results/app'); |
|
34
|
|
|
$this->alterConfig(); |
|
35
|
|
|
$this->makePathIfNotExist(config('repository.stubs_path')); |
|
36
|
|
|
$this->makePathIfNotExist(config('repository.lang_path')); |
|
37
|
|
|
$this->publishStubs(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function getPackageProviders($app): array |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
RepositoryServiceProvider::class, |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getEnvironmentSetUp($app) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
// perform environment setup |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function makePathIfNotExist(string $path) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!is_dir($path)) { |
|
55
|
|
|
mkdir($path, 0775, true); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function deletePathIfExists($dir) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_dir($dir)) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$files = scandir($dir); |
|
66
|
|
|
foreach ($files as $file) { |
|
67
|
|
|
if ($file != '.' && $file != '..') { |
|
68
|
|
|
$path = $dir . '/' . $file; |
|
69
|
|
|
if (is_dir($path)) { |
|
70
|
|
|
$this->deletePathIfExists($path); // Recursively delete subdirectories |
|
71
|
|
|
} else { |
|
72
|
|
|
unlink($path); // Delete files |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
rmdir($dir); // Delete the empty directory |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Using `vendor:publish --tag="repository-stubs"` didn't work as the test config path is not set yet. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function publishStubs() |
|
84
|
|
|
{ |
|
85
|
|
|
$iterator = new FilesystemIterator(__DIR__.'/../src/stubs'); |
|
86
|
|
|
foreach ($iterator as $i) { |
|
87
|
|
|
/** |
|
88
|
|
|
* @var DirectoryIterator $i |
|
89
|
|
|
*/ |
|
90
|
|
|
if ($i->isFile()) { |
|
91
|
|
|
copy($i->getPathname(), $this->configs['repository.stubs_path'].'/'.$i->getFilename()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// change fillable to guarded for the next test. |
|
96
|
|
|
$path = base_path('resources/stubs/Model.stub'); |
|
97
|
|
|
$modelContent = file_get_contents($path); |
|
98
|
|
|
$modelContent = str_replace('$fillable', '$guarded', $modelContent); |
|
99
|
|
|
file_put_contents($path, $modelContent); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function alterConfig() |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($this->configs as $key => $value) { |
|
105
|
|
|
config()->set($key, $value); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function createDatabase(): void |
|
110
|
|
|
{ |
|
111
|
|
|
Schema::create($this->databaseName, function (Blueprint $blueprint) { |
|
112
|
|
|
$blueprint->id(); |
|
113
|
|
|
$blueprint->string('name'); |
|
114
|
|
|
$blueprint->string('type'); |
|
115
|
|
|
$blueprint->timestamps(); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function dropDatabase(): void |
|
120
|
|
|
{ |
|
121
|
|
|
Schema::drop($this->databaseName); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.