Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] MySQL Builds #39415

Merged
merged 27 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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();
Copy link
Member Author

@driesvints driesvints Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we're not running an in-memory database anymore we'll need to wipe the database each time after every test. I want to refactor this to a RefreshDatabase trait later on but for now this is okay.

}
}
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In one test we're storing the name as a value so this needs to be a string. SQLite accepted this but it's in fact an incorrect column type for these tests.

$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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch to an accessor instead of relying on SQLite's U date format. We only need to verify that the custom pivot class is properly hydrated so no need to use $dateFormat specifically.


$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',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use universal date formats instead of SQLite specific timestamps.

],
]);

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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor based pagination relies on an orderBy clause when selecting specific columns.


$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',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date fields in most engines need a full date to be inserted.

'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