Skip to content

Commit 8f0232a

Browse files
committed
Revert "Remove DatabaseEloquentAppTest to see if CI passes"
This reverts commit bf2764b.
1 parent bf2764b commit 8f0232a

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Foundation\Testing\TestCase;
9+
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Schema;
11+
use Orchestra\Testbench\Concerns\CreatesApplication;
12+
13+
class DatabaseEloquentAppTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
use CreatesApplication;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
Schema::create('database_eloquent_app_test_users', function (Blueprint $table) {
23+
$table->id();
24+
$table->string('email')->unique();
25+
$table->timestamps();
26+
});
27+
}
28+
29+
public function testObserverIsCalledOnTestsWithAfterCommit()
30+
{
31+
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());
32+
33+
$user1 = DatabaseEloquentAppTestUser::create([
34+
'email' => '[email protected]',
35+
]);
36+
37+
$this->assertTrue($user1->exists);
38+
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
39+
}
40+
41+
public function testObserverCalledWithAfterCommitWhenInsideTransaction()
42+
{
43+
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());
44+
45+
$user1 = DB::transaction(fn () => DatabaseEloquentAppTestUser::create([
46+
'email' => '[email protected]',
47+
]));
48+
49+
$this->assertTrue($user1->exists);
50+
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
51+
}
52+
53+
public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepoint()
54+
{
55+
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());
56+
57+
$user1 = DatabaseEloquentAppTestUser::createOrFirst([
58+
'email' => '[email protected]',
59+
]);
60+
61+
$this->assertTrue($user1->exists);
62+
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
63+
}
64+
65+
public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepointAndInsideTransaction()
66+
{
67+
DatabaseEloquentAppTestUser::observe($observer = DatabaseEloquentAppTestUserObserver::resetting());
68+
69+
$user1 = DB::transaction(fn () => DatabaseEloquentAppTestUser::createOrFirst([
70+
'email' => '[email protected]',
71+
]));
72+
73+
$this->assertTrue($user1->exists);
74+
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
75+
}
76+
}
77+
78+
class DatabaseEloquentAppTestUser extends Model
79+
{
80+
protected $guarded = [];
81+
}
82+
83+
class DatabaseEloquentAppTestUserObserver
84+
{
85+
public static $calledTimes = 0;
86+
87+
public $afterCommit = true;
88+
89+
public static function resetting()
90+
{
91+
static::$calledTimes = 0;
92+
93+
return new static();
94+
}
95+
96+
public function created($user)
97+
{
98+
static::$calledTimes++;
99+
}
100+
}

0 commit comments

Comments
 (0)