Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

CreatesApplication::setUpBaseLineSqlLiteDatabase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
2
3
use GeneaLabs\LaravelModelCaching\Providers\Service as LaravelModelCachingService;
4
use Illuminate\Auth\Middleware\Authenticate;
5
use Illuminate\Support\Facades\Artisan;
6
use Laravel\Nova\Http\Middleware\Authorize;
7
use Laravel\Nova\Http\Middleware\BootTools;
8
use Laravel\Nova\Http\Middleware\DispatchServingNovaEvent;
9
10
trait CreatesApplication
11
{
12
    private static $baseLineDatabaseMigrated = false;
13
14
    protected $cache;
15
    protected $testingSqlitePath;
16
17
    protected function cache()
18
    {
19
        $cache = app('cache');
20
21
        if (config('laravel-model-caching.store')) {
22
            $cache = $cache->store(config('laravel-model-caching.store'));
23
        }
24
25
        return $cache;
26
    }
27
28
    public function setUp() : void
29
    {
30
        parent::setUp();
31
32
        $this->setUpBaseLineSqlLiteDatabase();
33
34
        $databasePath = __DIR__ . "/database";
35
        $this->testingSqlitePath = "{$databasePath}/";
36
        $baselinePath = "{$databasePath}/baseline.sqlite";
37
        $testingPath = "{$databasePath}/testing.sqlite";
38
39
        ! file_exists($testingPath)
40
            ?: unlink($testingPath);
41
        copy($baselinePath, $testingPath);
42
43
        require(__DIR__ . '/routes/web.php');
44
45
        $this->withFactories(__DIR__ . '/database/factories');
0 ignored issues
show
Bug introduced by
It seems like withFactories() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
47
        view()->addLocation(__DIR__ . '/resources/views', 'laravel-model-caching');
48
49
        $this->cache = app('cache')
50
            ->store(config('laravel-model-caching.store'));
51
        $this->cache()->flush();
52
    }
53
54
    /**
55
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
56
     */
57
    protected function getPackageProviders($app)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

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

Loading history...
58
    {
59
        return [
60
            LaravelModelCachingService::class,
61
        ];
62
    }
63
64
    public function setUpBaseLineSqlLiteDatabase()
65
    {
66
        if (self::$baseLineDatabaseMigrated) {
67
            return;
68
        }
69
70
        self::$baseLineDatabaseMigrated = true;
71
72
        $file = __DIR__ . '/database/baseline.sqlite';
73
        $this->app['config']->set('database.default', 'baseline');
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
        $this->app['config']->set('database.connections.baseline', [
75
            'driver' => 'sqlite',
76
            "url" => null,
77
            'database' => $file,
78
            'prefix' => '',
79
            "foreign_key_constraints" => false,
80
        ]);
81
82
        ! file_exists($file)
83
            ?: unlink($file);
84
        touch($file);
85
86
        $this->withFactories(__DIR__ . '/database/factories');
0 ignored issues
show
Bug introduced by
It seems like withFactories() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
        $this->loadMigrationsFrom(__DIR__ . '/database/migrations');
0 ignored issues
show
Bug introduced by
It seems like loadMigrationsFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
89
        Artisan::call('db:seed', [
90
            '--class' => 'DatabaseSeeder',
91
            '--database' => 'baseline',
92
        ]);
93
94
        $this->app['config']->set('database.default', 'testing');
95
    }
96
97
    protected function getEnvironmentSetUp($app)
98
    {
99
        $app['config']->set('database.default', 'testing');
100
        $app['config']->set('database.connections.testing', [
101
            'driver' => 'sqlite',
102
            'database' => __DIR__ . '/database/testing.sqlite',
103
            'prefix' => '',
104
            "foreign_key_constraints" => false,
105
        ]);
106
        $app['config']->set('database.redis.client', "phpredis");
107
        $app['config']->set('database.redis.cache', [
108
            'host' => env('REDIS_HOST', '127.0.0.1'),
109
            'port' => env('REDIS_PORT', 6379),
110
        ]);
111
        $app['config']->set('database.redis.default', [
112
            'host' => env('REDIS_HOST', '127.0.0.1'),
113
            'port' => env('REDIS_PORT', 6379),
114
        ]);
115
        $app['config']->set('database.redis.model-cache', [
116
            'host' => env('REDIS_HOST', '127.0.0.1'),
117
            'password' => env('REDIS_PASSWORD', null),
118
            'port' => env('REDIS_PORT', 6379),
119
            'database' => 1,
120
        ]);
121
        $app['config']->set('cache.stores.model', [
122
            'driver' => 'redis',
123
            'connection' => 'model-cache',
124
        ]);
125
        $app['config']->set('laravel-model-caching.store', 'model');
126
        $app['config']->set("nova", [
127
            'name' => 'Nova Site',
128
            'url' => env('APP_URL', '/'),
129
            'path' => '/nova',
130
            'guard' => env('NOVA_GUARD', null),
131
            'middleware' => [
132
                'web',
133
                Authenticate::class,
134
                DispatchServingNovaEvent::class,
135
                BootTools::class,
136
                Authorize::class,
137
            ],
138
            'pagination' => 'simple',
139
        ]);
140
    }
141
}
142