From 896e3a8eccb68e67be5a036ff1e35b10b3d6a1e5 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 28 Dec 2023 11:04:18 +0300 Subject: [PATCH 1/3] add getDynamicSlug --- src/Traits/MorphOneWallet.php | 7 +++- tests/Infra/Factories/UserDynamicFactory.php | 27 +++++++++++++++ tests/Infra/Models/UserDynamic.php | 35 ++++++++++++++++++++ tests/Units/Domain/WalletTest.php | 34 +++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/Infra/Factories/UserDynamicFactory.php create mode 100644 tests/Infra/Models/UserDynamic.php diff --git a/src/Traits/MorphOneWallet.php b/src/Traits/MorphOneWallet.php index c286e8c93..f3bdef5a1 100644 --- a/src/Traits/MorphOneWallet.php +++ b/src/Traits/MorphOneWallet.php @@ -34,9 +34,14 @@ public function wallet(): MorphOne ->where('slug', config('wallet.wallet.default.slug', 'default')) ->withDefault(static function (WalletModel $wallet, object $holder) use ($castService) { $model = $castService->getModel($holder); + + $slug = method_exists($model, 'getDynamicSlug') + ? $model->getDynamicSlug() + : config('wallet.wallet.default.slug', 'default'); + $wallet->forceFill(array_merge(config('wallet.wallet.creating', []), [ 'name' => config('wallet.wallet.default.name', 'Default Wallet'), - 'slug' => config('wallet.wallet.default.slug', 'default'), + 'slug' => $slug, 'meta' => config('wallet.wallet.default.meta', []), 'balance' => 0, ])); diff --git a/tests/Infra/Factories/UserDynamicFactory.php b/tests/Infra/Factories/UserDynamicFactory.php new file mode 100644 index 000000000..10c0a48be --- /dev/null +++ b/tests/Infra/Factories/UserDynamicFactory.php @@ -0,0 +1,27 @@ + + */ +final class UserDynamicFactory extends Factory +{ + protected $model = UserDynamic::class; + + public function definition(): array + { + return [ + 'name' => fake() + ->name, + 'email' => fake() + ->unique() + ->safeEmail, + ]; + } +} diff --git a/tests/Infra/Models/UserDynamic.php b/tests/Infra/Models/UserDynamic.php new file mode 100644 index 000000000..5589bbc41 --- /dev/null +++ b/tests/Infra/Models/UserDynamic.php @@ -0,0 +1,35 @@ +email; + } +} diff --git a/tests/Units/Domain/WalletTest.php b/tests/Units/Domain/WalletTest.php index 3a5e004f2..c30684751 100644 --- a/tests/Units/Domain/WalletTest.php +++ b/tests/Units/Domain/WalletTest.php @@ -12,8 +12,11 @@ use Bavix\Wallet\Internal\Service\DatabaseServiceInterface; use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Services\RegulatorServiceInterface; +use Bavix\Wallet\Test\Infra\Factories\UserDynamicFactory; use Bavix\Wallet\Test\Infra\Factories\UserFactory; use Bavix\Wallet\Test\Infra\Models\User; +use Bavix\Wallet\Test\Infra\Models\UserDynamic; +use Bavix\Wallet\Test\Infra\PackageModels\Wallet; use Bavix\Wallet\Test\Infra\TestCase; use Illuminate\Database\Eloquent\Collection; use RuntimeException; @@ -280,6 +283,37 @@ public function testRecalculate(): void self::assertSame(0, $user->balanceInt); } + public function testDefaultWalletCustomize(): void + { + /** @var User $user */ + $user = UserFactory::new()->create(); + + self::assertFalse($user->wallet->exists); + + $user->wallet->meta = [ + 'internal' => 1, + ]; + + self::assertNotNull($user->deposit(100)); + self::assertSame(100, $user->balanceInt); + + $wallet = Wallet::query()->find($user->wallet->getKey()); + + self::assertNotNull($wallet); + self::assertSame(1, $wallet->meta['internal'] ?? 0); + } + + public function testGetDynamicSlug(): void + { + /** @var UserDynamic $user */ + $user = UserDynamicFactory::new()->create(); + + self::assertNotNull($user->deposit(100)); + self::assertSame(100, $user->balanceInt); + + self::assertSame('default-' . $user->email, $user->wallet->slug); + } + public function testCrash(): void { /** @var User $user */ From 428d3ab1a9f2c8d8066a49d31655e3871b749f23 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 28 Dec 2023 11:11:34 +0300 Subject: [PATCH 2/3] update changelog.md --- changelog.md | 68 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index ac2147f2f..b6130ded1 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [10.1.0] - 2023-12-28 +### Bump +* build(deps-dev): update laravel/cashier requirement from ^14.12 to ^15.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/817 +* build(deps): bump axios from 1.6.2 to 1.6.3 by @dependabot in https://github.com/bavix/laravel-wallet/pull/821 + +### Added +* add wallet getDynamicSlug by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/822 + +## [10.0.2] - 2023-12-14 +### Bump +* Bump bavix/.github from 0.0.5 to 0.0.7 by @dependabot in https://github.com/bavix/laravel-wallet/pull/784 +* docs: accessing float amount by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/786 +* docs: changing wallet decimal_places by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/785 +* remove driftingly/rector-laravel by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/787 +* Bump postcss from 8.4.19 to 8.4.31 by @dependabot in https://github.com/bavix/laravel-wallet/pull/788 +* Bump webpack from 5.88.2 to 5.89.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/790 +* Bump size-limit from 9.0.0 to 10.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/792 +* Bump bavix/.github from 0.0.7 to 0.0.8 by @dependabot in https://github.com/bavix/laravel-wallet/pull/793 +* Bump axios from 1.5.1 to 1.6.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/795 +* Bump size-limit from 10.0.1 to 10.0.2 by @dependabot in https://github.com/bavix/laravel-wallet/pull/797 +* build(deps): bump actions/checkout from 3 to 4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/799 +* build(deps): bump axios from 1.6.0 to 1.6.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/801 +* build(deps): bump size-limit from 10.0.2 to 11.0.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/802 +* build(deps): bump axios from 1.6.1 to 1.6.2 by @dependabot in https://github.com/bavix/laravel-wallet/pull/803 +* build(deps): bump bavix/.github from 0.0.8 to 0.0.9 by @dependabot in https://github.com/bavix/laravel-wallet/pull/808 +* build(deps): bump size-limit from 11.0.0 to 11.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/810 +* build(deps): bump actions/stale from 8 to 9 by @dependabot in https://github.com/bavix/laravel-wallet/pull/811 +* build(deps): bump bavix/.github from 0.0.9 to 0.1.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/812 +* build(deps): bump JetBrains/qodana-action from 2023.2 to 2023.3 by @dependabot in https://github.com/bavix/laravel-wallet/pull/813 + +### Removed +* remove cozyhouse by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/794 + +### Added +* add support database cache by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/815 +* Qodana by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/798 +* add support php 8.3 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/796 + ## [10.0.1] - 2023-09-30 ### Bump * Bump semver from 7.3.8 to 7.5.4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/726 @@ -15,11 +53,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Updated laravel rector set by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/733 * Update driftingly/rector-laravel requirement from ^0.21 to ^0.22 by @dependabot in https://github.com/bavix/laravel-wallet/pull/735 * Create npm workflow by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/736 -* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/737 -* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/739 -* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/740 -* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/741 -* Lumen documentation update by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/743 * [Snyk] Upgrade webpack-cli from 5.0.0 to 5.1.4 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/747 * [Snyk] Upgrade axios from 1.3.4 to 1.4.0 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/748 * [Snyk] Upgrade size-limit from 8.2.4 to 8.2.6 by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/750 @@ -31,18 +64,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Bump size-limit from 8.2.6 to 9.0.0 by @dependabot in https://github.com/bavix/laravel-wallet/pull/762 * Bump actions/stale from 6 to 8 by @dependabot in https://github.com/bavix/laravel-wallet/pull/761 * Bump actions/checkout from 3 to 4 by @dependabot in https://github.com/bavix/laravel-wallet/pull/760 -* docs: update wording to sound grammatically correct by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/766 -* docs: customize name and slug of default wallet by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/765 -* docs: fixed grammatical error by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/764 * Bump uuid from 9.0.0 to 9.0.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/768 * checking the Wallet class extensibility by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/769 +* Update driftingly/rector-laravel requirement from ^0.24 to ^0.25 by @dependabot in https://github.com/bavix/laravel-wallet/pull/779 +* Update driftingly/rector-laravel requirement from ^0.25 to ^0.26 by @dependabot in https://github.com/bavix/laravel-wallet/pull/780 +* Bump axios from 1.5.0 to 1.5.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/781 + +### Docs * docs: Create configuration.md by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/771 * docs: add configuration file to sidebar by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/772 * docs: move configuration details to separate file by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/773 * docs: update for clarity by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/774 -* Update driftingly/rector-laravel requirement from ^0.24 to ^0.25 by @dependabot in https://github.com/bavix/laravel-wallet/pull/779 -* Update driftingly/rector-laravel requirement from ^0.25 to ^0.26 by @dependabot in https://github.com/bavix/laravel-wallet/pull/780 -* Bump axios from 1.5.0 to 1.5.1 by @dependabot in https://github.com/bavix/laravel-wallet/pull/781 +* docs: update wording to sound grammatically correct by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/766 +* docs: customize name and slug of default wallet by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/765 +* docs: fixed grammatical error by @justinkekeocha in https://github.com/bavix/laravel-wallet/pull/764 +* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/737 +* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/739 +* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/740 +* Update docs.yaml by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/741 +* Lumen documentation update by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/743 + +### Added * Add semgrep by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/783 ## [10.0.0] - 2023-07-08 @@ -1047,7 +1089,9 @@ The operation is now executed in the transaction and updates the new `refund` fi - Exceptions: AmountInvalid, BalanceIsEmpty. - Models: Transfer, Transaction. -[Unreleased]: https://github.com/bavix/laravel-wallet/compare/10.0.1...develop +[Unreleased]: https://github.com/bavix/laravel-wallet/compare/10.1.0...develop +[10.1.0]: https://github.com/bavix/laravel-wallet/compare/10.0.2...10.1.0 +[10.0.2]: https://github.com/bavix/laravel-wallet/compare/10.0.1...10.0.2 [10.0.1]: https://github.com/bavix/laravel-wallet/compare/10.0.0...10.0.1 [10.0.0]: https://github.com/bavix/laravel-wallet/compare/9.6.2...10.0.0 [9.6.2]: https://github.com/bavix/laravel-wallet/compare/9.6.1...9.6.2 From ed81f3f7978f44b7f051929ecff079dd236a3084 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Thu, 28 Dec 2023 11:13:53 +0300 Subject: [PATCH 3/3] rename getDynamicSlug => getDynamicDefaultSlug --- changelog.md | 2 +- src/Traits/MorphOneWallet.php | 4 ++-- tests/Infra/Models/UserDynamic.php | 2 +- tests/Units/Domain/WalletTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index b6130ded1..f2700ed7f 100644 --- a/changelog.md +++ b/changelog.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * build(deps): bump axios from 1.6.2 to 1.6.3 by @dependabot in https://github.com/bavix/laravel-wallet/pull/821 ### Added -* add wallet getDynamicSlug by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/822 +* add wallet getDynamicDefaultSlug by @rez1dent3 in https://github.com/bavix/laravel-wallet/pull/822 ## [10.0.2] - 2023-12-14 ### Bump diff --git a/src/Traits/MorphOneWallet.php b/src/Traits/MorphOneWallet.php index f3bdef5a1..f78184b1c 100644 --- a/src/Traits/MorphOneWallet.php +++ b/src/Traits/MorphOneWallet.php @@ -35,8 +35,8 @@ public function wallet(): MorphOne ->withDefault(static function (WalletModel $wallet, object $holder) use ($castService) { $model = $castService->getModel($holder); - $slug = method_exists($model, 'getDynamicSlug') - ? $model->getDynamicSlug() + $slug = method_exists($model, 'getDynamicDefaultSlug') + ? $model->getDynamicDefaultSlug() : config('wallet.wallet.default.slug', 'default'); $wallet->forceFill(array_merge(config('wallet.wallet.creating', []), [ diff --git a/tests/Infra/Models/UserDynamic.php b/tests/Infra/Models/UserDynamic.php index 5589bbc41..af81cff8b 100644 --- a/tests/Infra/Models/UserDynamic.php +++ b/tests/Infra/Models/UserDynamic.php @@ -28,7 +28,7 @@ public function getTable(): string return 'users'; } - public function getDynamicSlug(): string + public function getDynamicDefaultSlug(): string { return 'default-' . $this->email; } diff --git a/tests/Units/Domain/WalletTest.php b/tests/Units/Domain/WalletTest.php index c30684751..b6f4590b1 100644 --- a/tests/Units/Domain/WalletTest.php +++ b/tests/Units/Domain/WalletTest.php @@ -303,7 +303,7 @@ public function testDefaultWalletCustomize(): void self::assertSame(1, $wallet->meta['internal'] ?? 0); } - public function testGetDynamicSlug(): void + public function testgetDynamicDefaultSlug(): void { /** @var UserDynamic $user */ $user = UserDynamicFactory::new()->create();