diff --git a/tests/Integration/Database/MySql/DatabaseEloquentMySqlIntegrationTest.php b/tests/Integration/Database/MySql/DatabaseEloquentMySqlIntegrationTest.php index f7eaccdb274b..2df663e3b86d 100644 --- a/tests/Integration/Database/MySql/DatabaseEloquentMySqlIntegrationTest.php +++ b/tests/Integration/Database/MySql/DatabaseEloquentMySqlIntegrationTest.php @@ -4,11 +4,15 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Illuminate\Tests\Integration\Database\WithEloquentIntegrationTests; class DatabaseEloquentMySqlIntegrationTest extends MySqlTestCase { + use WithEloquentIntegrationTests; + + protected $eloquentModelClass = DatabaseEloquentMySqlIntegrationUser::class; + protected function defineDatabaseMigrationsAfterDatabaseRefreshed() { if (! Schema::hasTable('database_eloquent_mysql_integration_users')) { @@ -25,55 +29,6 @@ protected function destroyDatabaseMigrations() { Schema::drop('database_eloquent_mysql_integration_users'); } - - public function testCreateOrFirst() - { - $user1 = DatabaseEloquentMySqlIntegrationUser::createOrFirst(['email' => 'taylorotwell@gmail.com']); - - $this->assertSame('taylorotwell@gmail.com', $user1->email); - $this->assertNull($user1->name); - - $user2 = DatabaseEloquentMySqlIntegrationUser::createOrFirst( - ['email' => 'taylorotwell@gmail.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylorotwell@gmail.com', $user2->email); - $this->assertNull($user2->name); - - $user3 = DatabaseEloquentMySqlIntegrationUser::createOrFirst( - ['email' => 'abigailotwell@gmail.com'], - ['name' => 'Abigail Otwell'] - ); - - $this->assertNotEquals($user3->id, $user1->id); - $this->assertSame('abigailotwell@gmail.com', $user3->email); - $this->assertSame('Abigail Otwell', $user3->name); - - $user4 = DatabaseEloquentMySqlIntegrationUser::createOrFirst( - ['name' => 'Dries Vints'], - ['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com'] - ); - - $this->assertSame('Nuno Maduro', $user4->name); - } - - public function testCreateOrFirstWithinTransaction() - { - $user1 = DatabaseEloquentMySqlIntegrationUser::createOrFirst(['email' => 'taylor@laravel.com']); - - DB::transaction(function () use ($user1) { - $user2 = DatabaseEloquentMySqlIntegrationUser::createOrFirst( - ['email' => 'taylor@laravel.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylor@laravel.com', $user2->email); - $this->assertNull($user2->name); - }); - } } class DatabaseEloquentMySqlIntegrationUser extends Model diff --git a/tests/Integration/Database/Postgres/DatabaseEloquentPostgresIntegrationTest.php b/tests/Integration/Database/Postgres/DatabaseEloquentPostgresIntegrationTest.php index d95dca61a0cb..2470dc6c822f 100644 --- a/tests/Integration/Database/Postgres/DatabaseEloquentPostgresIntegrationTest.php +++ b/tests/Integration/Database/Postgres/DatabaseEloquentPostgresIntegrationTest.php @@ -4,11 +4,15 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Illuminate\Tests\Integration\Database\WithEloquentIntegrationTests; class DatabaseEloquentPostgresIntegrationTest extends PostgresTestCase { + use WithEloquentIntegrationTests; + + protected $eloquentModelClass = DatabaseEloquentPostgresIntegrationUser::class; + protected function defineDatabaseMigrationsAfterDatabaseRefreshed() { if (! Schema::hasTable('database_eloquent_postgres_integration_users')) { @@ -25,55 +29,6 @@ protected function destroyDatabaseMigrations() { Schema::drop('database_eloquent_postgres_integration_users'); } - - public function testCreateOrFirst() - { - $user1 = DatabaseEloquentPostgresIntegrationUser::createOrFirst(['email' => 'taylorotwell@gmail.com']); - - $this->assertSame('taylorotwell@gmail.com', $user1->email); - $this->assertNull($user1->name); - - $user2 = DatabaseEloquentPostgresIntegrationUser::createOrFirst( - ['email' => 'taylorotwell@gmail.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylorotwell@gmail.com', $user2->email); - $this->assertNull($user2->name); - - $user3 = DatabaseEloquentPostgresIntegrationUser::createOrFirst( - ['email' => 'abigailotwell@gmail.com'], - ['name' => 'Abigail Otwell'] - ); - - $this->assertNotEquals($user3->id, $user1->id); - $this->assertSame('abigailotwell@gmail.com', $user3->email); - $this->assertSame('Abigail Otwell', $user3->name); - - $user4 = DatabaseEloquentPostgresIntegrationUser::createOrFirst( - ['name' => 'Dries Vints'], - ['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com'] - ); - - $this->assertSame('Nuno Maduro', $user4->name); - } - - public function testCreateOrFirstWithinTransaction() - { - $user1 = DatabaseEloquentPostgresIntegrationUser::create(['email' => 'taylor@laravel.com']); - - DB::transaction(function () use ($user1) { - $user2 = DatabaseEloquentPostgresIntegrationUser::createOrFirst( - ['email' => 'taylor@laravel.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylor@laravel.com', $user2->email); - $this->assertNull($user2->name); - }); - } } class DatabaseEloquentPostgresIntegrationUser extends Model diff --git a/tests/Integration/Database/SqlServer/DatabaseEloquentSqlServerIntegrationTest.php b/tests/Integration/Database/SqlServer/DatabaseEloquentSqlServerIntegrationTest.php index 9098e6608c6c..694eca76e830 100644 --- a/tests/Integration/Database/SqlServer/DatabaseEloquentSqlServerIntegrationTest.php +++ b/tests/Integration/Database/SqlServer/DatabaseEloquentSqlServerIntegrationTest.php @@ -4,11 +4,15 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Illuminate\Tests\Integration\Database\WithEloquentIntegrationTests; class DatabaseEloquentSqlServerIntegrationTest extends SqlServerTestCase { + use WithEloquentIntegrationTests; + + protected $eloquentModelClass = DatabaseEloquentSqlServerIntegrationUser::class; + protected function defineDatabaseMigrationsAfterDatabaseRefreshed() { if (! Schema::hasTable('database_eloquent_sql_server_integration_users')) { @@ -25,55 +29,6 @@ protected function destroyDatabaseMigrations() { Schema::drop('database_eloquent_sql_server_integration_users'); } - - public function testCreateOrFirst() - { - $user1 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst(['email' => 'taylorotwell@gmail.com']); - - $this->assertSame('taylorotwell@gmail.com', $user1->email); - $this->assertNull($user1->name); - - $user2 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst( - ['email' => 'taylorotwell@gmail.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylorotwell@gmail.com', $user2->email); - $this->assertNull($user2->name); - - $user3 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst( - ['email' => 'abigailotwell@gmail.com'], - ['name' => 'Abigail Otwell'] - ); - - $this->assertNotEquals($user3->id, $user1->id); - $this->assertSame('abigailotwell@gmail.com', $user3->email); - $this->assertSame('Abigail Otwell', $user3->name); - - $user4 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst( - ['name' => 'Dries Vints'], - ['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com'] - ); - - $this->assertSame('Nuno Maduro', $user4->name); - } - - public function testCreateOrFirstWithinTransaction() - { - $user1 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst(['email' => 'taylor@laravel.com']); - - DB::transaction(function () use ($user1) { - $user2 = DatabaseEloquentSqlServerIntegrationUser::createOrFirst( - ['email' => 'taylor@laravel.com'], - ['name' => 'Taylor Otwell'] - ); - - $this->assertEquals($user1->id, $user2->id); - $this->assertSame('taylor@laravel.com', $user2->email); - $this->assertNull($user2->name); - }); - } } class DatabaseEloquentSqlServerIntegrationUser extends Model diff --git a/tests/Integration/Database/WithEloquentIntegrationTests.php b/tests/Integration/Database/WithEloquentIntegrationTests.php new file mode 100644 index 000000000000..7ad66edd1557 --- /dev/null +++ b/tests/Integration/Database/WithEloquentIntegrationTests.php @@ -0,0 +1,57 @@ +eloquentModelClass::createOrFirst(['email' => 'taylorotwell@gmail.com']); + + $this->assertSame('taylorotwell@gmail.com', $user1->email); + $this->assertNull($user1->name); + + $user2 = $this->eloquentModelClass::createOrFirst( + ['email' => 'taylorotwell@gmail.com'], + ['name' => 'Taylor Otwell'] + ); + + $this->assertEquals($user1->id, $user2->id); + $this->assertSame('taylorotwell@gmail.com', $user2->email); + $this->assertNull($user2->name); + + $user3 = $this->eloquentModelClass::createOrFirst( + ['email' => 'abigailotwell@gmail.com'], + ['name' => 'Abigail Otwell'] + ); + + $this->assertNotEquals($user3->id, $user1->id); + $this->assertSame('abigailotwell@gmail.com', $user3->email); + $this->assertSame('Abigail Otwell', $user3->name); + + $user4 = $this->eloquentModelClass::createOrFirst( + ['name' => 'Dries Vints'], + ['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com'] + ); + + $this->assertSame('Nuno Maduro', $user4->name); + } + + public function testCreateOrFirstWithinTransaction() + { + $user1 = $this->eloquentModelClass::createOrFirst(['email' => 'taylor@laravel.com']); + + DB::transaction(function () use ($user1) { + $user2 = $this->eloquentModelClass::createOrFirst( + ['email' => 'taylor@laravel.com'], + ['name' => 'Taylor Otwell'] + ); + + $this->assertEquals($user1->id, $user2->id); + $this->assertSame('taylor@laravel.com', $user2->email); + $this->assertNull($user2->name); + }); + } +}