Skip to content

Commit

Permalink
Merge pull request #828 from bavix/11.x-soft-delete
Browse files Browse the repository at this point in the history
fix default wallet
  • Loading branch information
rez1dent3 authored Dec 30, 2023
2 parents bf9148b + 93437ad commit e22269e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
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) {

Check warning on line 15 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Blueprint'

Check warning on line 15 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Schema'
$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) {

Check warning on line 18 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Blueprint'

Check warning on line 18 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Schema'
$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) {

Check warning on line 28 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Schema'

Check warning on line 28 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Blueprint'
$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) {

Check warning on line 31 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Schema'

Check warning on line 31 in database/2023_12_30_204610_soft_delete.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'Blueprint'
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

0 comments on commit e22269e

Please sign in to comment.