These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | use Orchestra\Testbench\TestCase; |
||
4 | use Dimsav\Translatable\Test\Model\Country; |
||
5 | |||
6 | class TestsBase extends TestCase |
||
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 | protected function setUp(): void |
||
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 (\Illuminate\Database\Events\QueryExecuted $query) use ($that) { |
||
0 ignored issues
–
show
|
|||
139 | $that->queriesCount++; |
||
140 | // echo("\n--- Query {$that->queriesCount} --- {$this->beautifyQuery($this->insertBindingsIntoQuery($query->sql, $this->formatBindingsForSqlInjection($query->bindings)))}\n"); |
||
141 | }); |
||
142 | } |
||
143 | |||
144 | private function beautifyQuery($query) |
||
145 | { |
||
146 | $capitalizeWords = ['select ', ' from ', ' where ', ' on ', ' join ']; |
||
147 | $newLineWords = ['select ', 'from ', 'where ', 'join ']; |
||
148 | foreach ($capitalizeWords as $word) { |
||
149 | $query = str_replace($word, strtoupper($word), $query); |
||
150 | } |
||
151 | |||
152 | foreach ($newLineWords as $word) { |
||
153 | $query = str_replace($word, "\n$word", $query); |
||
154 | $word = strtoupper($word); |
||
155 | $query = str_replace($word, "\n$word", $query); |
||
156 | } |
||
157 | |||
158 | return $query; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param $bindings |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | private function formatBindingsForSqlInjection($bindings) |
||
167 | { |
||
168 | foreach ($bindings as $i => $binding) { |
||
169 | if ($binding instanceof DateTime) { |
||
170 | $bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); |
||
171 | } else { |
||
172 | if (is_string($binding)) { |
||
173 | $bindings[$i] = "'$binding'"; |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return $bindings; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param $query |
||
183 | * @param $bindings |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | private function insertBindingsIntoQuery($query, $bindings) |
||
188 | { |
||
189 | if (empty($bindings)) { |
||
190 | return $query; |
||
191 | } |
||
192 | |||
193 | $query = str_replace(['%', '?'], ['%%', '%s'], $query); |
||
194 | |||
195 | return vsprintf($query, $bindings); |
||
196 | } |
||
197 | } |
||
198 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.