Passed
Push — main ( 6d0af8...4ba462 )
by Mohammad
03:48
created

TestCase::deletePathContentsIfExists()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 14
rs 9.6111
c 0
b 0
f 0
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
32
        $this->app->setBasePath(realpath(__DIR__).'/results');
33
        $this->deletePathContentsIfExists(realpath(__DIR__).'/results');
34
        $this->makePathIfNotExist(realpath(__DIR__).'/results/app');
35
        $this->alterConfig();
36
        $this->makePathIfNotExist(config('repository.stubs_path'));
37
        $this->makePathIfNotExist(config('repository.lang_path'));
38
        $this->publishStubs();
39
    }
40
41
    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

41
    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...
42
    {
43
        return [
44
            RepositoryServiceProvider::class,
45
        ];
46
    }
47
48
    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

48
    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...
49
    {
50
        // perform environment setup
51
    }
52
53
    public function makePathIfNotExist(string $path)
54
    {
55
        if (!is_dir($path)) {
56
            mkdir($path, 0775, true);
57
        }
58
    }
59
60
    public function deletePathContentsIfExists($dir): void
61
    {
62
        if (!is_dir($dir)) {
63
            return;
64
        }
65
66
        $files = scandir($dir);
67
        foreach ($files as $file) {
68
            if ($file[0] != '.') {
69
                $path = $dir . '/' . $file;
70
                if (is_dir($path)) {
71
                    $this->deletePathContentsIfExists($path); // Recursively delete subdirectories
72
                } else {
73
                    unlink($path); // Delete files
74
                }
75
            }
76
        }
77
    }
78
79
    /**
80
     * Using `vendor:publish --tag="repository-stubs"` didn't work as the test config path is not set yet.
81
     */
82
    public function publishStubs()
83
    {
84
        $iterator = new FilesystemIterator(__DIR__.'/../src/stubs');
85
        foreach ($iterator as $i) {
86
            /**
87
             * @var DirectoryIterator $i
88
             */
89
            if ($i->isFile()) {
90
                copy($i->getPathname(), $this->configs['repository.stubs_path'].'/'.$i->getFilename());
91
            }
92
        }
93
94
        // change fillable to guarded for the next test.
95
        $path = base_path('resources/stubs/Model.stub');
96
        $modelContent = file_get_contents($path);
97
        $modelContent = str_replace('$fillable', '$guarded', $modelContent);
98
        file_put_contents($path, $modelContent);
99
    }
100
101
    public function alterConfig()
102
    {
103
        foreach ($this->configs as $key => $value) {
104
            config()->set($key, $value);
105
        }
106
    }
107
108
    public function createDatabase(): void
109
    {
110
        Schema::create($this->databaseName, function (Blueprint $blueprint) {
111
            $blueprint->id();
112
            $blueprint->string('name');
113
            $blueprint->string('type');
114
            $blueprint->timestamps();
115
        });
116
    }
117
118
    public function dropDatabase(): void
119
    {
120
        Schema::drop($this->databaseName);
121
    }
122
}
123