Skip to content

Commit ab999e4

Browse files
authored
Merge pull request #440 from bavix/develop
#438 UUID Duplicate entry fix
2 parents f46fbf4 + aca8d12 commit ab999e4

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

changelog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [8.0.3] - 2022-02-19
10+
### Fixed
11+
- Fixed "UUID Duplicate entry" bug on eager loading. #438 @DanielSpravtsev
12+
- Use predefined PHP float epsilon (phpunit) sebastianbergmann/phpunit#4874
13+
914
## [8.0.2] - 2022-02-12
1015
### Fixed
1116
- Added keys to service provider
@@ -784,7 +789,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
784789
- Exceptions: AmountInvalid, BalanceIsEmpty.
785790
- Models: Transfer, Transaction.
786791

787-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/8.0.2...develop
792+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/8.0.3...develop
793+
[8.0.3]: https://github.com/bavix/laravel-wallet/compare/8.0.2...8.0.3
788794
[8.0.2]: https://github.com/bavix/laravel-wallet/compare/8.0.1...8.0.2
789795
[8.0.1]: https://github.com/bavix/laravel-wallet/compare/8.0.0...8.0.1
790796
[8.0.0]: https://github.com/bavix/laravel-wallet/compare/7.3.2...8.0.0

src/Models/Wallet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
1515
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1616
use Bavix\Wallet\Internal\Service\MathServiceInterface;
17+
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1718
use Bavix\Wallet\Services\AtomicServiceInterface;
1819
use Bavix\Wallet\Services\RegulatorServiceInterface;
1920
use Bavix\Wallet\Traits\CanConfirm;
@@ -167,4 +168,9 @@ public function getCurrencyAttribute(): string
167168
{
168169
return $this->meta['currency'] ?? Str::upper($this->slug);
169170
}
171+
172+
protected function initializeMorphOneWallet(): void
173+
{
174+
$this->uuid = app(UuidFactoryServiceInterface::class)->uuid4();
175+
}
170176
}

src/Traits/MorphOneWallet.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Bavix\Wallet\Traits;
66

7-
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
87
use Bavix\Wallet\Models\Wallet as WalletModel;
98
use Bavix\Wallet\Services\CastServiceInterface;
109
use Illuminate\Database\Eloquent\Relations\MorphOne;
@@ -31,7 +30,6 @@ public function wallet(): MorphOne
3130
'name' => config('wallet.wallet.default.name', 'Default Wallet'),
3231
'slug' => config('wallet.wallet.default.slug', 'default'),
3332
'meta' => config('wallet.wallet.default.meta', []),
34-
'uuid' => app(UuidFactoryServiceInterface::class)->uuid4(),
3533
'balance' => 0,
3634
]))
3735
;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Units\Domain;
6+
7+
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
8+
use Bavix\Wallet\Test\Infra\Models\Buyer;
9+
use Bavix\Wallet\Test\Infra\TestCase;
10+
11+
/**
12+
* @internal
13+
*/
14+
class EagerLoadingTest extends TestCase
15+
{
16+
public function testUuidDuplicate(): void
17+
{
18+
$buyerTimes = BuyerFactory::times(10)->create();
19+
20+
/** @var Buyer[] $buyers */
21+
$buyers = Buyer::with('wallet')
22+
->whereIn('id', $buyerTimes->pluck('id')->toArray())
23+
->paginate(10)
24+
;
25+
26+
$uuids = [];
27+
$balances = [];
28+
foreach ($buyers as $buyer) {
29+
$uuids[] = $buyer->wallet->uuid;
30+
$balances[] = $buyer->wallet->balanceInt;
31+
}
32+
33+
self::assertCount(10, array_unique($uuids));
34+
self::assertCount(1, array_unique($balances));
35+
}
36+
}

tests/Units/Domain/MultiWalletTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,21 @@ public function testGetWallet(): void
415415
$secondWallet = $user->getWallet('test');
416416
self::assertSame($secondWallet->getKey(), $firstWallet->getKey());
417417

418+
$uuid = app(UuidFactoryServiceInterface::class)->uuid4();
418419
$test2 = $user->wallets()->create([
419420
'name' => 'Test2',
420-
'uuid' => app(UuidFactoryServiceInterface::class)->uuid4(),
421+
'uuid' => $uuid,
421422
]);
422423

424+
self::assertNotNull($test2->refresh());
425+
self::assertSame($uuid, $test2->uuid);
423426
self::assertSame(
424427
$test2->getKey(),
425428
$user->getWallet('test2')->getKey()
426429
);
427430

431+
self::assertNotNull($user->wallets()->where('uuid', $uuid)->first());
432+
428433
// check default wallet
429434
self::assertSame(
430435
$user->balance,
@@ -443,9 +448,17 @@ public function testGetWalletOptimize(): void
443448

444449
$user->load('wallets'); // optimize
445450

451+
$ids = [];
452+
$uuids = [];
446453
foreach ($names as $name) {
447-
self::assertSame($name, $user->getWallet($name)->name);
454+
$wallet = $user->getWallet($name);
455+
self::assertSame($name, $wallet->name);
456+
$uuids[] = $wallet->uuid;
457+
$ids[] = $wallet->getKey();
448458
}
459+
460+
self::assertCount(count($names), array_unique($uuids));
461+
self::assertCount(count($names), array_unique($ids));
449462
}
450463

451464
public function testPay(): void

tests/Units/Domain/WalletFloatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function testMantissa(): void
196196
self::assertSame($transaction->type, Transaction::TYPE_WITHDRAW);
197197

198198
self::assertSame($user->balanceInt, 1_000_000 - 255672);
199-
self::assertSame((float) $user->balanceFloat, 10000.00 - 2556.72);
199+
self::assertSame((float) $user->balanceFloat, 7443.28);
200200

201201
$transaction = $user->depositFloat(2556.72 * 2);
202202
self::assertSame($transaction->amountInt, 255672 * 2);

tests/Units/Service/MathTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public function testDiv(): void
143143
self::assertSame(-0.2, (float) $provider->div(-1, 5));
144144

145145
// float
146-
self::assertSame(0.24223602484472, (float) $provider->div(1.17, 4.83));
147-
self::assertSame(-0.26519337016574, (float) $provider->div(-1.44, 5.43));
146+
self::assertSame(0.24223602484472, (float) $provider->div(1.17, 4.83, 14));
147+
self::assertSame(-0.26519337016574, (float) $provider->div(-1.44, 5.43, 14));
148148

149149
self::assertSame(
150150
0,

0 commit comments

Comments
 (0)