TestCase::makePathIfNotExist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 2
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\Generator\Generator as GeneratorService;
10
use Shamaseen\Repository\PathResolver;
11
use Shamaseen\Repository\RepositoryServiceProvider;
12
13
class TestCase extends \Orchestra\Testbench\TestCase
14
{
15
    protected PathResolver $pathResolver;
16
    protected GeneratorService $generator;
17
18
    protected string $databaseName = 'tests';
19
    protected string $modelName = 'Test';
20
    protected string $userPath = 'Tests';
21
22
    protected array $configs = [
23
        'repository.stubs_path' => __DIR__.'/results/resources/stubs',
24
        'repository.lang_path' => __DIR__.'/results/resources/lang',
25
    ];
26
27
    public function setUp(): void
28
    {
29
        parent::setUp();
30
31
        $this->app->setBasePath(realpath(__DIR__).'/results');
0 ignored issues
show
Bug introduced by
The method setBasePath() does not exist on null. ( Ignorable by Annotation )

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

31
        $this->app->/** @scrutinizer ignore-call */ 
32
                    setBasePath(realpath(__DIR__).'/results');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        $this->deletePathContentsIfExists(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
        $this->generator = $this->app->make(GeneratorService::class);
41
        config(['generator.base_path' => base_path(config('repository.base_path'))]);
42
43
        $this->pathResolver = new PathResolver($this->modelName, $this->userPath, config('repository.base_path'));
44
    }
45
46
    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

46
    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...
47
    {
48
        return [
49
            RepositoryServiceProvider::class,
50
        ];
51
    }
52
53
    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

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