Test Setup Failed
Pull Request — master (#441)
by Sven
130:00 queued 64:58
created

TestsBase::beautifyQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
3
use Orchestra\Testbench\TestCase;
4
use Dimsav\Translatable\Test\Model\Country;
5
6
class TestsBase extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    protected $queriesCount;
9
    protected static $db2Setup = false;
10
11
    const DB_NAME = 'translatable_test';
12
    const DB_NAME2 = 'translatable_test2';
13
    const DB_USERNAME = 'homestead';
14
    const DB_PASSWORD = 'secret';
15
16
    public function setUp()
17
    {
18
        $this->makeSureDatabaseExists(static::DB_NAME);
19
20
        if (! static::$db2Setup) {
21
            $this->makeSureDatabaseExists(static::DB_NAME2);
22
        }
23
24
        parent::setUp();
25
26
        if (! static::$db2Setup) {
27
            $this->makeSureSchemaIsCreated('mysql2');
28
            $this->truncateAllTablesButMigrations(static::DB_NAME2);
29
            static::$db2Setup = true;
30
        }
31
32
        $this->makeSureSchemaIsCreated('mysql');
33
        $this->enableQueryCounter();
34
        $this->refreshSeedData();
35
    }
36
37
    private function refreshSeedData()
38
    {
39
        $this->truncateAllTablesButMigrations(static::DB_NAME);
40
        $seeder = new AddFreshSeeds;
41
        $seeder->run();
42
    }
43
44
    private function makeSureDatabaseExists($dbName)
45
    {
46
        $this->runQuery('CREATE DATABASE IF NOT EXISTS '.$dbName);
47
    }
48
49
    private function makeSureSchemaIsCreated($dbConnectionName)
50
    {
51
        $migrationsPath = '../../../../tests/migrations';
52
        $artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');
53
54
        // Makes sure the migrations table is created
55
        $artisan->call('migrate', [
56
            '--database' => $dbConnectionName,
57
            '--path'     => $migrationsPath,
58
        ]);
59
    }
60
61
    private function truncateAllTablesButMigrations($dbName)
62
    {
63
        $db = $this->app->make('db');
64
        $db->statement('SET FOREIGN_KEY_CHECKS=0;');
65
66
        foreach ($tables = $db->select('SHOW TABLES') as $table) {
67
            $table = $table->{'Tables_in_'.$dbName};
68
            if ($table != 'migrations') {
69
                $db->table($table)->truncate();
70
            }
71
        }
72
        $db->statement('SET FOREIGN_KEY_CHECKS=1;');
73
    }
74
75
    /**
76
     * @param $query
77
     * return void
78
     */
79
    private function runQuery($query)
80
    {
81
        $dbUsername = static::DB_USERNAME;
82
        $dbPassword = static::DB_PASSWORD;
83
84
        $command = "mysql -u $dbUsername ";
85
        $command .= $dbPassword ? " -p$dbPassword" : '';
86
        $command .= " -e '$query'";
87
88
        exec($command.' 2>/dev/null');
89
    }
90
91
    public function testRunningMigration()
92
    {
93
        $country = Country::find(1);
94
        $this->assertEquals('gr', $country->code);
95
    }
96
97
    protected function getPackageProviders($app)
98
    {
99
        return ['Dimsav\Translatable\TranslatableServiceProvider'];
100
    }
101
102
    protected function getEnvironmentSetUp($app)
103
    {
104
        $app['path.base'] = __DIR__.'/..';
105
        $app['config']->set('database.default', 'mysql');
106
        $app['config']->set('database.connections.mysql', [
107
            'driver'   => 'mysql',
108
            'host' => '127.0.0.1',
109
            'database' => static::DB_NAME,
110
            'username' => static::DB_USERNAME,
111
            'password' => static::DB_PASSWORD,
112
            'charset' => 'utf8',
113
            'collation' => 'utf8_unicode_ci',
114
            'strict' => false,
115
        ]);
116
        $app['config']->set('database.connections.mysql2', [
117
            'driver'   => 'mysql',
118
            'host' => '127.0.0.1',
119
            'database' => static::DB_NAME2,
120
            'username' => static::DB_USERNAME,
121
            'password' => static::DB_PASSWORD,
122
            'charset' => 'utf8',
123
            'collation' => 'utf8_unicode_ci',
124
            'strict' => false,
125
        ]);
126
        $locales = ['el', 'en', 'fr', 'de', 'id', 'en-GB', 'en-US', 'de-DE', 'de-CH'];
127
        $app['config']->set('translatable.locales', $locales);
128
    }
129
130
    protected function getPackageAliases($app)
131
    {
132
        return ['Eloquent' => 'Illuminate\Database\Eloquent\Model'];
133
    }
134
135
    protected function enableQueryCounter()
136
    {
137
        $that = $this;
138
        DB::listen(function ($query) use ($that) {
0 ignored issues
show
Unused Code introduced by
The parameter $query 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...
139
            $that->queriesCount++;
140
            // echo("\n--- Query {$that->queriesCount}--- $query->sql\n");
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
        });
142
    }
143
}
144