generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable database transactions feature
- Loading branch information
1 parent
d3cf794
commit 0f6196e
Showing
5 changed files
with
145 additions
and
30 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
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
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,89 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use RichanFongdasen\Turso\Tests\Fixtures\Models\User; | ||
|
||
beforeEach(function () { | ||
migrateTables('users', 'posts'); | ||
|
||
$this->user = User::factory()->create(); | ||
}); | ||
|
||
afterEach(function () { | ||
Schema::dropAllTables(); | ||
}); | ||
|
||
test('it can rollback the transaction', function () { | ||
$this->user->name = 'John Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::count())->toBe(1); | ||
|
||
DB::transaction(function () { | ||
$this->user->name = 'Jane Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::first()->name)->toBe('Jane Doe'); | ||
|
||
DB::rollBack(); | ||
}); | ||
|
||
expect(User::count())->toBe(1); | ||
expect(User::first()->name)->toBe('John Doe'); | ||
})->group('DatabaseTransactionsTest', 'FeatureTest'); | ||
|
||
test('it can rollback the transaction by manually using the transactions', function () { | ||
$this->user->name = 'John Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::count())->toBe(1); | ||
|
||
DB::beginTransaction(); | ||
|
||
$this->user->name = 'Jane Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::first()->name)->toBe('Jane Doe'); | ||
|
||
DB::rollBack(); | ||
|
||
expect(User::count())->toBe(1); | ||
expect(User::first()->name)->toBe('John Doe'); | ||
})->group('DatabaseTransactionsTest', 'FeatureTest'); | ||
|
||
test('it can commit the transaction', function () { | ||
$this->user->name = 'John Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::count())->toBe(1); | ||
|
||
DB::transaction(function () { | ||
$this->user->name = 'Jane Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::first()->name)->toBe('Jane Doe'); | ||
}); | ||
|
||
expect(User::count())->toBe(1); | ||
expect(User::first()->name)->toBe('Jane Doe'); | ||
})->group('DatabaseTransactionsTest', 'FeatureTest'); | ||
|
||
test('it can commit the transaction by manually using the transactions', function () { | ||
$this->user->name = 'John Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::count())->toBe(1); | ||
|
||
DB::beginTransaction(); | ||
|
||
$this->user->name = 'Jane Doe'; | ||
$this->user->save(); | ||
|
||
expect(User::first()->name)->toBe('Jane Doe'); | ||
|
||
DB::commit(); | ||
|
||
expect(User::count())->toBe(1); | ||
expect(User::first()->name)->toBe('Jane Doe'); | ||
})->group('DatabaseTransactionsTest', 'FeatureTest'); |
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
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