Skip to content

Commit c754e06

Browse files
author
Babichev Maxim
committed
fix bug
1 parent 60eaf37 commit c754e06

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"require": {
2626
"php": "^7.1|^8.0",
27+
"ext-pdo": "*",
2728
"illuminate/database": "^5.5|^6.0",
2829
"nesbot/carbon": "^1.20|^2.0",
2930
"doctrine/dbal": "^2.8",

src/Services/CommonService.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,14 @@ public function addBalance(Wallet $wallet, int $amount): bool
225225
$balance = app(Storable::class)
226226
->incBalance($wallet, $amount);
227227

228-
$result = $wallet->update(compact('balance'));
228+
try {
229+
$result = $wallet->update(compact('balance'));
230+
} catch (\Throwable $throwable) {
231+
app(Storable::class)
232+
->setBalance($wallet, $wallet->getAvailableBalance());
233+
234+
throw $throwable;
235+
}
229236

230237
if (!$result) {
231238
app(Storable::class)

tests/BalanceTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Bavix\Wallet\Interfaces\Storable;
66
use Bavix\Wallet\Models\Wallet;
7-
use Bavix\Wallet\Objects\Cart;
87
use Bavix\Wallet\Services\CommonService;
98
use Bavix\Wallet\Services\ProxyService;
109
use Bavix\Wallet\Services\WalletService;
1110
use Bavix\Wallet\Test\Models\Buyer;
1211
use Bavix\Wallet\Test\Models\UserMulti;
1312
use Illuminate\Support\Facades\DB;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PDOException;
1415
use function app;
1516

1617
class BalanceTest extends TestCase
@@ -148,7 +149,7 @@ public function testFailUpdate(): void
148149
$this->assertTrue($wallet->exists);
149150

150151
/**
151-
* @var Wallet $mockWallet
152+
* @var Wallet|MockObject $mockWallet
152153
*/
153154
$mockWallet = $this->createMock(\get_class($wallet));
154155
$mockWallet->method('update')->willReturn(false);
@@ -161,6 +162,36 @@ public function testFailUpdate(): void
161162
$this->assertEquals(0, app(Storable::class)->getBalance($wallet));
162163
}
163164

165+
/**
166+
* @return void
167+
* @throws
168+
*/
169+
public function testThrowUpdate(): void
170+
{
171+
$this->expectException(PDOException::class);
172+
173+
/**
174+
* @var Buyer $buyer
175+
*/
176+
$buyer = factory(Buyer::class)->create();
177+
$this->assertFalse($buyer->relationLoaded('wallet'));
178+
$wallet = $buyer->wallet;
179+
180+
$this->assertFalse($wallet->exists);
181+
$this->assertEquals($wallet->balance, 0);
182+
$this->assertTrue($wallet->exists);
183+
184+
/**
185+
* @var Wallet|MockObject $mockWallet
186+
*/
187+
$mockWallet = $this->createMock(\get_class($wallet));
188+
$mockWallet->method('update')->willThrowException(new PDOException());
189+
$mockWallet->method('getKey')->willReturn($wallet->getKey());
190+
191+
app(CommonService::class)
192+
->addBalance($mockWallet, 100);
193+
}
194+
164195
/**
165196
* @throws
166197
*/

0 commit comments

Comments
 (0)