Passed
Push — master ( 5a91bc...be866d )
by Łukasz
02:23
created

TestCase::truncateAllTablesButMigrations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mrluke\Privileges\Tests;
4
5
use Mrluke\Configuration\Host;
1 ignored issue
show
Bug introduced by
The type Mrluke\Configuration\Host was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Orchestra\Testbench\TestCase as BaseCase;
7
8
/**
9
 * TestsBase - phpunit master file for this package.
10
 *
11
 * @author    Łukasz Sitnicki (mr-luke)
12
 * @link      http://github.com/mr-luke/privileges
13
 * @license   MIT
14
 */
15
class TestCase extends BaseCase
16
{
17
    /**
18
     * DB configuration.
19
     */
20
    const DB_HOST = 'localhost';
21
    const DB_NAME = 'packages';
22
    const DB_USERNAME = 'root';
23
    const DB_PASSWORD = '';
24
    const DB_PREFIX = 'privileges_';
25
26
    /**
27
     * Setup TestCase.
28
     *
29
     * @return void
30
     */
31
    public function setUp() : void
32
    {
33
        $this->makeSureDatabaseExists(static::DB_NAME);
34
35
        parent::setUp();
36
37
        $this->artisan('migrate:refresh', [
38
            '--database' => 'mysql',
39
            '--realpath' => realpath(__DIR__.'/../database/migrations'),
40
        ]);
41
42
        $this->loadMigrationsFrom([
43
            '--database' => 'mysql',
44
            '--realpath' => realpath(__DIR__.'/database/migrations'),
45
        ]);
46
    }
47
48
    /**
49
     * Get application timezone.
50
     *
51
     * @param \Illuminate\Foundation\Application $app
52
     *
53
     * @return string|null
54
     */
55
    protected function getApplicationTimezone($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

55
    protected function getApplicationTimezone(/** @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...
56
    {
57
        return 'Europe/Warsaw';
58
    }
59
60
    /**
61
     * Seting enviroment for Test.
62
     *
63
     * @param \Illuminate\Foundation\Application $app
64
     *
65
     * @return void
66
     */
67
    protected function getEnvironmentSetUp($app) : void
68
    {
69
        $app['path.base'] = __DIR__.'/..';
70
        $app['config']->set('database.default', 'mysql');
71
        $app['config']->set('database.connections.mysql', [
72
            'driver'    => 'mysql',
73
            'host'      => static::DB_HOST,
74
            'database'  => static::DB_NAME,
75
            'username'  => static::DB_USERNAME,
76
            'password'  => static::DB_PASSWORD,
77
            'charset'   => 'utf8',
78
            'collation' => 'utf8_unicode_ci',
79
            'strict'    => true,
80
            'prefix'    => static::DB_PREFIX,
81
        ]);
82
        $app['config']->set('app.faker_locale', 'pl_PL');
83
    }
84
85
    /**
86
     * Setup and return test configuration for Manager.
87
     *
88
     * @return Mrluke\Configuration\Host
0 ignored issues
show
Bug introduced by
The type Mrluke\Privileges\Tests\Mrluke\Configuration\Host was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
     */
90
    protected function getManagerConfiguration() : Host
91
    {
92
        // TODO
93
94
        return new Host([]);
95
    }
96
97
    /**
98
     * Return array of providers.
99
     *
100
     * @param \Illuminate\Foundation\Application $app
101
     *
102
     * @return array
103
     */
104
    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

104
    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...
105
    {
106
        return [
107
            \Mrluke\Privileges\PrivilegesServiceProvider::class,
108
        ];
109
    }
110
111
    /**
112
     * Create database if not exists.
113
     *
114
     * @param string $dbName
115
     *
116
     * @return void
117
     */
118
    private function makeSureDatabaseExists(string $dbName) :void
119
    {
120
        $this->runQuery('CREATE DATABASE IF NOT EXISTS '.$dbName);
121
    }
122
123
    /**
124
     * Peroform seeding.
125
     *
126
     * @return void
127
     */
128
    private function refreshSeedData() : void
0 ignored issues
show
Unused Code introduced by
The method refreshSeedData() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
129
    {
130
        $this->truncateAllTablesButMigrations(static::DB_NAME);
131
        $seeder = new \DataSeeder();
132
        $seeder->run();
133
    }
134
135
    /**
136
     * Run Query.
137
     *
138
     * @param string $query
139
     *
140
     * @return void
141
     */
142
    private function runQuery(string $query) : void
143
    {
144
        $dbUsername = static::DB_USERNAME;
145
        $dbPassword = static::DB_PASSWORD;
146
        $command = "mysql -u $dbUsername ";
147
        $command .= $dbPassword ? " -p$dbPassword" : '';
148
        $command .= " -e '$query'";
149
        exec($command.' 2>/dev/null');
150
    }
151
152
    /**
153
     * Truncate each table except migrations.
154
     *
155
     * @param string $dbName
156
     *
157
     * @return void
158
     */
159
    private function truncateAllTablesButMigrations(string $dbName) : void
160
    {
161
        $db = $this->app->make('db');
162
        $db->statement('SET FOREIGN_KEY_CHECKS=0;');
163
        foreach ($tables = $db->select('SHOW TABLES') as $table) {
0 ignored issues
show
Unused Code introduced by
The assignment to $tables is dead and can be removed.
Loading history...
164
            $table = $table->{'Tables_in_'.$dbName};
165
            $table = str_replace(static::DB_PREFIX, '', $table);
166
            if ($table != 'migrations') {
167
                $db->table($table)->truncate();
168
            }
169
        }
170
        $db->statement('SET FOREIGN_KEY_CHECKS=1;');
171
    }
172
}
173