Skip to content

Commit 50f5079

Browse files
[8.x] MySQL Builds (#39415)
* Add MySQL build * wip * Disable other builds for now * Update DatabaseTestCase.php * wip * Delete workflow for now * Try root user * Try root password * Try port * wip * refactor * Refactor to remove query * Do not test against specific queries * Refactor test * Fix some more tests * re-add base tests * Apply fixes from StyleCI * Fix query * wip * Use PHP 8.1 * wip * Fix column type * Test on MySQL 8.0 * Add orderBy clauses * meh * wip * wip Co-authored-by: Taylor Otwell <[email protected]>
1 parent b3705b9 commit 50f5079

17 files changed

+217
-97
lines changed

.github/workflows/databases.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: databases
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
mysql_57:
7+
runs-on: ubuntu-20.04
8+
9+
services:
10+
mysql:
11+
image: mysql:5.7
12+
env:
13+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
14+
MYSQL_DATABASE: forge
15+
ports:
16+
- 3306:3306
17+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
18+
19+
strategy:
20+
fail-fast: true
21+
22+
name: MySQL 5.7
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v2
27+
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: 8.1
32+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql
33+
tools: composer:v2
34+
coverage: none
35+
36+
- name: Install dependencies
37+
uses: nick-invision/retry@v1
38+
with:
39+
timeout_minutes: 5
40+
max_attempts: 5
41+
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress
42+
43+
- name: Execute tests
44+
run: vendor/bin/phpunit tests/Integration/Database --verbose
45+
env:
46+
DB_CONNECTION: mysql
47+
DB_USERNAME: root
48+
49+
mysql_8:
50+
runs-on: ubuntu-20.04
51+
52+
services:
53+
mysql:
54+
image: mysql:8
55+
env:
56+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
57+
MYSQL_DATABASE: forge
58+
ports:
59+
- 3306:3306
60+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
61+
62+
strategy:
63+
fail-fast: true
64+
65+
name: MySQL 8.0
66+
67+
steps:
68+
- name: Checkout code
69+
uses: actions/checkout@v2
70+
71+
- name: Setup PHP
72+
uses: shivammathur/setup-php@v2
73+
with:
74+
php-version: 8.1
75+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql
76+
tools: composer:v2
77+
coverage: none
78+
79+
- name: Install dependencies
80+
uses: nick-invision/retry@v1
81+
with:
82+
timeout_minutes: 5
83+
max_attempts: 5
84+
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress
85+
86+
- name: Execute tests
87+
run: vendor/bin/phpunit tests/Integration/Database --verbose
88+
env:
89+
DB_CONNECTION: mysql
90+
DB_USERNAME: root

tests/Integration/Database/DatabaseEmulatePreparesMySqlConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class DatabaseEmulatePreparesMySqlConnectionTest extends DatabaseMySqlConnection
1212
{
1313
protected function getEnvironmentSetUp($app)
1414
{
15-
$app['config']->set('app.debug', 'true');
16-
$app['config']->set('database.default', 'mysql');
15+
parent::getEnvironmentSetUp($app);
16+
1717
$app['config']->set('database.connections.mysql.options', [
1818
PDO::ATTR_EMULATE_PREPARES => true,
1919
]);

tests/Integration/Database/DatabaseMySqlConnectionTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
* @requires extension pdo_mysql
1111
* @requires OS Linux|Darwin
1212
*/
13-
class DatabaseMySqlConnectionTest extends DatabaseMySqlTestCase
13+
class DatabaseMySqlConnectionTest extends DatabaseTestCase
1414
{
1515
const TABLE = 'player';
1616
const FLOAT_COL = 'float_col';
1717
const JSON_COL = 'json_col';
1818
const FLOAT_VAL = 0.2;
1919

20+
protected function getEnvironmentSetUp($app)
21+
{
22+
parent::getEnvironmentSetUp($app);
23+
24+
$app['config']->set('database.default', 'mysql');
25+
}
26+
2027
protected function setUp(): void
2128
{
2229
parent::setUp();

tests/Integration/Database/DatabaseSchemaBuilderAlterTableWithEnumTest.php renamed to tests/Integration/Database/DatabaseMySqlSchemaBuilderAlterTableWithEnumTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
* @requires extension pdo_mysql
1111
* @requires OS Linux|Darwin
1212
*/
13-
class DatabaseSchemaBuilderAlterTableWithEnumTest extends DatabaseMySqlTestCase
13+
class DatabaseMySqlSchemaBuilderAlterTableWithEnumTest extends DatabaseTestCase
1414
{
15+
protected function getEnvironmentSetUp($app)
16+
{
17+
parent::getEnvironmentSetUp($app);
18+
19+
$app['config']->set('database.default', 'mysql');
20+
}
21+
1522
protected function setUp(): void
1623
{
1724
parent::setUp();
@@ -58,7 +65,12 @@ public function testGetAllTablesAndColumnListing()
5865

5966
$tableProperties = array_values((array) $tables[0]);
6067
$this->assertEquals(['users', 'BASE TABLE'], $tableProperties);
61-
$this->assertEquals(['id', 'name', 'age', 'color'], Schema::getColumnListing('users'));
68+
69+
$columns = Schema::getColumnListing('users');
70+
71+
foreach (['id', 'name', 'age', 'color'] as $column) {
72+
$this->assertContains($column, $columns);
73+
}
6274

6375
Schema::create('posts', function (Blueprint $table) {
6476
$table->integer('id');

tests/Integration/Database/DatabaseMySqlTestCase.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/Integration/Database/DatabaseTestCase.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@
44

55
use Orchestra\Testbench\TestCase;
66

7-
class DatabaseTestCase extends TestCase
7+
abstract class DatabaseTestCase extends TestCase
88
{
99
protected function getEnvironmentSetUp($app)
1010
{
1111
$app['config']->set('app.debug', 'true');
1212

13-
$app['config']->set('database.default', 'testbench');
14-
1513
$app['config']->set('database.connections.testbench', [
1614
'driver' => 'sqlite',
1715
'database' => ':memory:',
1816
'prefix' => '',
1917
]);
18+
19+
if (! env('DB_CONNECTION')) {
20+
$app['config']->set('database.default', 'testbench');
21+
}
22+
}
23+
24+
protected function tearDown(): void
25+
{
26+
if ($this->app['config']->get('database.default') !== 'testbench') {
27+
$this->artisan('db:wipe', ['--drop-views' => true]);
28+
}
29+
30+
parent::tearDown();
2031
}
2132
}

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function setUp(): void
4848

4949
Schema::create('posts_tags', function (Blueprint $table) {
5050
$table->integer('post_id');
51-
$table->integer('tag_id');
51+
$table->string('tag_id');
5252
$table->string('flag')->default('')->nullable();
5353
$table->timestamps();
5454
});
@@ -132,7 +132,7 @@ public function testCustomPivotClass()
132132
$post->tagsWithCustomPivot()->attach($tag->id);
133133

134134
$this->assertInstanceOf(PostTagPivot::class, $post->tagsWithCustomPivot[0]->pivot);
135-
$this->assertEquals('1507630210', $post->tagsWithCustomPivot[0]->pivot->getAttributes()['created_at']);
135+
$this->assertEquals('1507630210', $post->tagsWithCustomPivot[0]->pivot->created_at);
136136

137137
$this->assertInstanceOf(PostTagPivot::class, $post->tagsWithCustomPivotClass[0]->pivot);
138138
$this->assertSame('posts_tags', $post->tagsWithCustomPivotClass()->getTable());
@@ -213,8 +213,8 @@ public function testCustomPivotClassUpdatesTimestamps()
213213
DB::table('posts_tags')->insert([
214214
[
215215
'post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty',
216-
'created_at' => '1507630210',
217-
'updated_at' => '1507630210',
216+
'created_at' => '2017-10-10 10:10:10',
217+
'updated_at' => '2017-10-10 10:10:10',
218218
],
219219
]);
220220

@@ -226,8 +226,8 @@ public function testCustomPivotClassUpdatesTimestamps()
226226
);
227227
foreach ($post->tagsWithCustomExtraPivot as $tag) {
228228
$this->assertSame('exclude', $tag->pivot->flag);
229-
$this->assertEquals('1507630210', $tag->pivot->getAttributes()['created_at']);
230-
$this->assertEquals('1507630220', $tag->pivot->getAttributes()['updated_at']); // +10 seconds
229+
$this->assertEquals('2017-10-10 10:10:10', $tag->pivot->getAttributes()['created_at']);
230+
$this->assertEquals('2017-10-10 10:10:20', $tag->pivot->getAttributes()['updated_at']); // +10 seconds
231231
}
232232
}
233233

@@ -1068,7 +1068,11 @@ class UserPostPivot extends Pivot
10681068
class PostTagPivot extends Pivot
10691069
{
10701070
protected $table = 'posts_tags';
1071-
protected $dateFormat = 'U';
1071+
1072+
public function getCreatedAtAttribute($value)
1073+
{
1074+
return Carbon::parse($value)->format('U');
1075+
}
10721076
}
10731077

10741078
class TagWithGlobalScope extends Model

tests/Integration/Database/EloquentCursorPaginateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function testPaginationWithDistinctColumnsAndSelect()
180180
TestPost::create(['title' => 'Goodbye world']);
181181
}
182182

183-
$query = TestPost::query()->distinct('title')->select('title');
183+
$query = TestPost::query()->orderBy('title')->distinct('title')->select('title');
184184

185185
$this->assertEquals(2, $query->get()->count());
186186
$this->assertEquals(2, $query->count());

tests/Integration/Database/EloquentModelDateCastingTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ public function testDatesFormattedAttributeBindings()
4545
$bindings = $query->bindings;
4646
});
4747

48-
$user = TestModel1::create([
49-
'date_field' => '2019-10',
48+
TestModel1::create([
49+
'date_field' => '2019-10-01',
5050
'datetime_field' => '2019-10-01 10:15:20',
5151
'immutable_date_field' => '2019-10-01',
5252
'immutable_datetime_field' => '2019-10-01 10:15',
5353
]);
5454

55-
$this->assertSame(['2019-10', '2019-10-01 10:15:20', '2019-10-01', '2019-10-01 10:15'], $bindings);
55+
$this->assertSame(['2019-10-01', '2019-10-01 10:15:20', '2019-10-01', '2019-10-01 10:15'], $bindings);
5656
}
5757

5858
public function testDatesFormattedArrayAndJson()
5959
{
6060
$user = TestModel1::create([
61-
'date_field' => '2019-10',
61+
'date_field' => '2019-10-01',
6262
'datetime_field' => '2019-10-01 10:15:20',
6363
'immutable_date_field' => '2019-10-01',
6464
'immutable_datetime_field' => '2019-10-01 10:15',
@@ -78,7 +78,6 @@ public function testDatesFormattedArrayAndJson()
7878

7979
public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
8080
{
81-
/** @var TestModel1 */
8281
$user = TestModel1::create([
8382
'date_field' => '2019-10-01',
8483
'datetime_field' => '2019-10-01 10:15:20',
@@ -99,7 +98,6 @@ public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
9998

10099
public function testCustomDateCastsAreComparedAsDatesForStringValues()
101100
{
102-
/** @var TestModel1 */
103101
$user = TestModel1::create([
104102
'date_field' => '2019-10-01',
105103
'datetime_field' => '2019-10-01 10:15:20',

tests/Integration/Database/EloquentPrunableTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ public function testPruneWithCustomPruneMethod()
111111
$this->assertFalse((bool) PrunableWithCustomPruneMethodTestModel::orderBy('id', 'desc')->first()->pruned);
112112
$this->assertEquals(5000, PrunableWithCustomPruneMethodTestModel::count());
113113
}
114-
115-
public function tearDown(): void
116-
{
117-
parent::tearDown();
118-
119-
Container::setInstance(null);
120-
121-
m::close();
122-
}
123114
}
124115

125116
class PrunableTestModel extends Model

0 commit comments

Comments
 (0)