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

fix default wallet #828

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions database/2023_12_30_204610_soft_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
public function up(): void
{
Schema::table((new Wallet())->getTable(), static function (Blueprint $table) {
$table->dropUnique(['holder_type', 'holder_id', 'slug']);

$table->softDeletesTz();

$table->unique(['holder_type', 'holder_id', 'slug', 'deleted_at']);
});
Schema::table((new Transfer())->getTable(), static function (Blueprint $table) {
$table->softDeletesTz();
Expand All @@ -30,9 +26,6 @@ public function up(): void
public function down(): void
{
Schema::table((new Wallet())->getTable(), static function (Blueprint $table) {
$table->dropUnique(['holder_type', 'holder_id', 'slug', 'deleted_at']);
$table->unique(['holder_type', 'holder_id', 'slug']);

$table->dropSoftDeletes();
});
Schema::table((new Transfer())->getTable(), static function (Blueprint $table) {
Expand Down
1 change: 1 addition & 0 deletions src/Traits/MorphOneWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function wallet(): MorphOne
return $castService
->getHolder($this)
->morphOne($related, 'holder')
->withTrashed()
->where('slug', config('wallet.wallet.default.slug', 'default'))
->withDefault(static function (WalletModel $wallet, object $holder) use ($castService) {
$model = $castService->getModel($holder);
Expand Down
30 changes: 28 additions & 2 deletions tests/Units/Domain/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ public function testDefaultWalletSoftDelete(): void

$buyer->deposit(2);

self::assertSame($buyer->wallet->getKey(), $oldWallet->getKey());

self::assertSame(3, $oldWallet->balanceInt);
self::assertSame(3, $buyer->balanceInt);
}

public function testDefaultWalletForceDelete(): void
{
/** @var Buyer $buyer */
$buyer = BuyerFactory::new()->create();
self::assertFalse($buyer->relationLoaded('wallet'));
self::assertFalse($buyer->wallet->exists);

$buyer->deposit(1);

$oldWallet = $buyer->wallet;

self::assertTrue($buyer->wallet->exists);
self::assertTrue($buyer->wallet->forceDelete());
self::assertFalse($buyer->wallet->exists);

/** @var Buyer $buyer */
$buyer = Buyer::query()->find($buyer->getKey());

$buyer->deposit(2);

self::assertNotSame($buyer->wallet->getKey(), $oldWallet->getKey());

self::assertSame(1, $oldWallet->balanceInt);
Expand Down Expand Up @@ -72,8 +98,8 @@ public function testTransferDelete(): void
$transfer = $user1->forceTransfer($user2, 100);

self::assertNotNull($transfer);
self::assertSame(100, $transfer->deposit->amount);
self::assertSame(-100, $transfer->withdraw->amount);
self::assertSame(100, $transfer->deposit->amountInt);
self::assertSame(-100, $transfer->withdraw->amountInt);

self::assertSame(-100, $user1->balanceInt);
self::assertSame(100, $user2->balanceInt);
Expand Down