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

[10.x] Fix createOrFirst on transactions #48144

Merged
merged 13 commits into from
Aug 23, 2023
Prev Previous commit
Next Next commit
Moves the eloquent contract tests to a trait
  • Loading branch information
tonysm committed Aug 23, 2023
commit a7e57c84ef8036e3dfb052d073943b1e9ad6ceb0
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);
});
}
}