Passed
Push — master ( be866d...b177ff )
by Łukasz
02:20
created

TestCase::getApplicationTimezone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mrluke\Privileges\Tests;
4
5
use Mrluke\Configuration\Host;
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)
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
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
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
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) {
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