Completed
Push — main ( 44d9a4...6d0af8 )
by Mohammad
16s queued 13s
created

TestCase::deletePathIfExists()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 11
nc 5
nop 1
dl 0
loc 19
rs 9.2222
c 1
b 0
f 1
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
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    protected function getPackageProviders(/** @scrutinizer ignore-unused */ $app): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        return [
43
            RepositoryServiceProvider::class,
44
        ];
45
    }
46
47
    protected function getEnvironmentSetUp($app)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
    protected function getEnvironmentSetUp(/** @scrutinizer ignore-unused */ $app)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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