From 93437add4c8333b47bc43c88209fc0b9626182bd Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Sat, 30 Dec 2023 23:31:18 +0300 Subject: [PATCH] fix default wallet --- database/2023_12_30_204610_soft_delete.php | 7 ----- src/Traits/MorphOneWallet.php | 1 + tests/Units/Domain/SoftDeleteTest.php | 30 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/database/2023_12_30_204610_soft_delete.php b/database/2023_12_30_204610_soft_delete.php index 552794894..3b84c9e58 100644 --- a/database/2023_12_30_204610_soft_delete.php +++ b/database/2023_12_30_204610_soft_delete.php @@ -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(); @@ -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) { diff --git a/src/Traits/MorphOneWallet.php b/src/Traits/MorphOneWallet.php index f78184b1c..1b27dbd60 100644 --- a/src/Traits/MorphOneWallet.php +++ b/src/Traits/MorphOneWallet.php @@ -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); diff --git a/tests/Units/Domain/SoftDeleteTest.php b/tests/Units/Domain/SoftDeleteTest.php index 414a1b406..816c552bb 100644 --- a/tests/Units/Domain/SoftDeleteTest.php +++ b/tests/Units/Domain/SoftDeleteTest.php @@ -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); @@ -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);