-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove DatabaseEloquentAppTest to see if CI passes"
This reverts commit bf2764b.
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} |