Skip to content

Commit

Permalink
Revert "Remove DatabaseEloquentAppTest to see if CI passes"
Browse files Browse the repository at this point in the history
This reverts commit bf2764b.
  • Loading branch information
tonysm committed Sep 20, 2023
1 parent bf2764b commit 8f0232a
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/Database/DatabaseEloquentAppTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Concerns\CreatesApplication;

class DatabaseEloquentAppTest extends TestCase
{
use RefreshDatabase;
use CreatesApplication;

protected function setUp(): void
{
parent::setUp();

Schema::create('database_eloquent_app_test_users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamps();
});
}

public function testObserverIsCalledOnTestsWithAfterCommit()
{
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());

$user1 = DatabaseEloquentAppTestUser::create([
'email' => '[email protected]',
]);

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverCalledWithAfterCommitWhenInsideTransaction()
{
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());

$user1 = DB::transaction(fn () => DatabaseEloquentAppTestUser::create([
'email' => '[email protected]',
]));

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepoint()
{
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());

$user1 = DatabaseEloquentAppTestUser::createOrFirst([
'email' => '[email protected]',
]);

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepointAndInsideTransaction()
{
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());

$user1 = DB::transaction(fn () => DatabaseEloquentAppTestUser::createOrFirst([
'email' => '[email protected]',
]));

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}
}

class DatabaseEloquentAppTestUser extends Model
{
protected $guarded = [];
}

class DatabaseEloquentAppTestUserObserver
{
public static $calledTimes = 0;

public $afterCommit = true;

public static function resetting()
{
static::$calledTimes = 0;

return new static();
}

public function created($user)
{
static::$calledTimes++;
}
}

0 comments on commit 8f0232a

Please sign in to comment.