Skip to content

Commit

Permalink
[8.x] MySQL Builds (#39415)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
driesvints and taylorotwell authored Nov 2, 2021
1 parent b3705b9 commit 50f5079
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 97 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/databases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: databases

on: [push, pull_request]

jobs:
mysql_57:
runs-on: ubuntu-20.04

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: forge
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
fail-fast: true

name: MySQL 5.7

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql
tools: composer:v2
coverage: none

- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit tests/Integration/Database --verbose
env:
DB_CONNECTION: mysql
DB_USERNAME: root

mysql_8:
runs-on: ubuntu-20.04

services:
mysql:
image: mysql:8
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: forge
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
fail-fast: true

name: MySQL 8.0

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql
tools: composer:v2
coverage: none

- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit tests/Integration/Database --verbose
env:
DB_CONNECTION: mysql
DB_USERNAME: root
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class DatabaseEmulatePreparesMySqlConnectionTest extends DatabaseMySqlConnection
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');
$app['config']->set('database.default', 'mysql');
parent::getEnvironmentSetUp($app);

$app['config']->set('database.connections.mysql.options', [
PDO::ATTR_EMULATE_PREPARES => true,
]);
Expand Down
9 changes: 8 additions & 1 deletion tests/Integration/Database/DatabaseMySqlConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
* @requires extension pdo_mysql
* @requires OS Linux|Darwin
*/
class DatabaseMySqlConnectionTest extends DatabaseMySqlTestCase
class DatabaseMySqlConnectionTest extends DatabaseTestCase
{
const TABLE = 'player';
const FLOAT_COL = 'float_col';
const JSON_COL = 'json_col';
const FLOAT_VAL = 0.2;

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('database.default', 'mysql');
}

protected function setUp(): void
{
parent::setUp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
* @requires extension pdo_mysql
* @requires OS Linux|Darwin
*/
class DatabaseSchemaBuilderAlterTableWithEnumTest extends DatabaseMySqlTestCase
class DatabaseMySqlSchemaBuilderAlterTableWithEnumTest extends DatabaseTestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('database.default', 'mysql');
}

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -58,7 +65,12 @@ public function testGetAllTablesAndColumnListing()

$tableProperties = array_values((array) $tables[0]);
$this->assertEquals(['users', 'BASE TABLE'], $tableProperties);
$this->assertEquals(['id', 'name', 'age', 'color'], Schema::getColumnListing('users'));

$columns = Schema::getColumnListing('users');

foreach (['id', 'name', 'age', 'color'] as $column) {
$this->assertContains($column, $columns);
}

Schema::create('posts', function (Blueprint $table) {
$table->integer('id');
Expand Down
23 changes: 0 additions & 23 deletions tests/Integration/Database/DatabaseMySqlTestCase.php

This file was deleted.

17 changes: 14 additions & 3 deletions tests/Integration/Database/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@

use Orchestra\Testbench\TestCase;

class DatabaseTestCase extends TestCase
abstract class DatabaseTestCase extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

if (! env('DB_CONNECTION')) {
$app['config']->set('database.default', 'testbench');
}
}

protected function tearDown(): void
{
if ($this->app['config']->get('database.default') !== 'testbench') {
$this->artisan('db:wipe', ['--drop-views' => true]);
}

parent::tearDown();
}
}
18 changes: 11 additions & 7 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function setUp(): void

Schema::create('posts_tags', function (Blueprint $table) {
$table->integer('post_id');
$table->integer('tag_id');
$table->string('tag_id');
$table->string('flag')->default('')->nullable();
$table->timestamps();
});
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testCustomPivotClass()
$post->tagsWithCustomPivot()->attach($tag->id);

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

$this->assertInstanceOf(PostTagPivot::class, $post->tagsWithCustomPivotClass[0]->pivot);
$this->assertSame('posts_tags', $post->tagsWithCustomPivotClass()->getTable());
Expand Down Expand Up @@ -213,8 +213,8 @@ public function testCustomPivotClassUpdatesTimestamps()
DB::table('posts_tags')->insert([
[
'post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty',
'created_at' => '1507630210',
'updated_at' => '1507630210',
'created_at' => '2017-10-10 10:10:10',
'updated_at' => '2017-10-10 10:10:10',
],
]);

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

Expand Down Expand Up @@ -1068,7 +1068,11 @@ class UserPostPivot extends Pivot
class PostTagPivot extends Pivot
{
protected $table = 'posts_tags';
protected $dateFormat = 'U';

public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->format('U');
}
}

class TagWithGlobalScope extends Model
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentCursorPaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function testPaginationWithDistinctColumnsAndSelect()
TestPost::create(['title' => 'Goodbye world']);
}

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

$this->assertEquals(2, $query->get()->count());
$this->assertEquals(2, $query->count());
Expand Down
10 changes: 4 additions & 6 deletions tests/Integration/Database/EloquentModelDateCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ public function testDatesFormattedAttributeBindings()
$bindings = $query->bindings;
});

$user = TestModel1::create([
'date_field' => '2019-10',
TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
]);

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

public function testDatesFormattedArrayAndJson()
{
$user = TestModel1::create([
'date_field' => '2019-10',
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
Expand All @@ -78,7 +78,6 @@ public function testDatesFormattedArrayAndJson()

public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
{
/** @var TestModel1 */
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
Expand All @@ -99,7 +98,6 @@ public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()

public function testCustomDateCastsAreComparedAsDatesForStringValues()
{
/** @var TestModel1 */
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
Expand Down
9 changes: 0 additions & 9 deletions tests/Integration/Database/EloquentPrunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,6 @@ public function testPruneWithCustomPruneMethod()
$this->assertFalse((bool) PrunableWithCustomPruneMethodTestModel::orderBy('id', 'desc')->first()->pruned);
$this->assertEquals(5000, PrunableWithCustomPruneMethodTestModel::count());
}

public function tearDown(): void
{
parent::tearDown();

Container::setInstance(null);

m::close();
}
}

class PrunableTestModel extends Model
Expand Down
Loading

0 comments on commit 50f5079

Please sign in to comment.