Passed
Pull Request — main (#11)
by Angel
02:48
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 Shamaseen\Repository\RepositoryServiceProvider;
8
9
class TestCase extends \Orchestra\Testbench\TestCase
10
{
11
    // Shamaseen\Repository\Tests\results\app
12
    protected array $configs = [
13
        'repository.base_path' => 'app',
14
        'repository.stubs_path' => __DIR__.'/results/resources/stubs',
15
        'repository.lang_path' => __DIR__.'/results/resources/lang',
16
        'repository.controllers_path' => 'Http/Controllers',
17
        'repository.repositories_path' => 'Repositories',
18
        'repository.models_path' => 'Models',
19
        'repository.requests_path' => 'Http/Requests',
20
        'repository.json_resources_path' => 'Http/Resources',
21
    ];
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
        $this->app->setBasePath(realpath(__DIR__).'/results');
27
        $this->deletePathIfExists(realpath(__DIR__).'/results');
28
        $this->makePathIfNotExist(realpath(__DIR__).'/results/app');
29
        $this->alterConfig();
30
        $this->makePathIfNotExist(config('repository.stubs_path'));
31
        $this->makePathIfNotExist(config('repository.lang_path'));
32
        $this->publishStubs();
33
    }
34
35
    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

35
    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...
36
    {
37
        return [
38
            RepositoryServiceProvider::class,
39
        ];
40
    }
41
42
    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

42
    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...
43
    {
44
        // perform environment setup
45
    }
46
47
    public function makePathIfNotExist(string $path)
48
    {
49
        if (!is_dir($path)) {
50
            mkdir($path, 0775, true);
51
        }
52
    }
53
54
    public function deletePathIfExists($dir)
55
    {
56
        if (!is_dir($dir)) {
57
            return;
58
        }
59
60
        $files = scandir($dir);
61
        foreach ($files as $file) {
62
            if ($file != '.' && $file != '..') {
63
                $path = $dir . '/' . $file;
64
                if (is_dir($path)) {
65
                    $this->deletePathIfExists($path); // Recursively delete subdirectories
66
                } else {
67
                    unlink($path); // Delete files
68
                }
69
            }
70
        }
71
72
        rmdir($dir); // Delete the empty directory
73
    }
74
75
    /**
76
     * Using `vendor:publish --tag="repository-stubs"` didn't work as the test config path is not set yet.
77
     */
78
    public function publishStubs()
79
    {
80
        $iterator = new FilesystemIterator(__DIR__.'/../src/stubs');
81
        foreach ($iterator as $i) {
82
            /**
83
             * @var DirectoryIterator $i
84
             */
85
            if ($i->isFile()) {
86
                copy($i->getPathname(), $this->configs['repository.stubs_path'].'/'.$i->getFilename());
87
            }
88
        }
89
90
        // change fillable to guarded for the next test.
91
        $path = 'tests/results/resources/stubs/Model.stub';
92
        $modelContent = file_get_contents($path);
93
        $modelContent = str_replace('$fillable', '$guarded', $modelContent);
94
        file_put_contents($path, $modelContent);
95
    }
96
97
    public function alterConfig()
98
    {
99
        foreach ($this->configs as $key => $value) {
100
            config()->set($key, $value);
101
        }
102
    }
103
}
104