Passed
Push — main ( afb7e6...11a524 )
by Mohammad
03:40
created

TestCase::dropDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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