Skip to content

Commit

Permalink
Moves the eloquent contract tests to a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Aug 23, 2023
1 parent d4dd95b commit a7e57c8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/Integration/Database/WithEloquentIntegrationTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Support\Facades\DB;

trait WithEloquentIntegrationTests
{
public function testCreateOrFirst()
{
$user1 = $this->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);
});
}
}

0 comments on commit a7e57c8

Please sign in to comment.