Skip to content

Commit d3cf380

Browse files
committed
patch calculateBalance + unit's
1 parent 69f31cc commit d3cf380

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Models/Transaction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Transaction extends Model
2626
protected $fillable = [
2727
'payable_type',
2828
'payable_id',
29+
'wallet_id',
2930
'uuid',
3031
'type',
3132
'amount',

src/Models/Wallet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function setNameAttribute(string $name): void
6868
*/
6969
public function calculateBalance(): bool
7070
{
71-
$balance = $this->transactions()
71+
$balance = $this->holder->transactions()
7272
->where('wallet_id', $this->getKey())
7373
->where('confirmed', true)
7474
->sum('amount');

tests/WalletTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,27 @@ public function testConfirmed(): void
176176
$this->assertEquals($user->balance, 0);
177177
}
178178

179+
/**
180+
* @return void
181+
*/
182+
public function testRecalculate(): void
183+
{
184+
/**
185+
* @var User $user
186+
*/
187+
$user = factory(User::class)->create();
188+
$this->assertEquals($user->balance, 0);
189+
190+
$user->deposit(100, null, false);
191+
$this->assertEquals($user->balance, 0);
192+
193+
$user->transactions()->update(['confirmed' => true]);
194+
$this->assertEquals($user->balance, 0);
195+
196+
$user->wallet->calculateBalance();
197+
$this->assertEquals($user->balance, 100);
198+
199+
$user->withdraw($user->balance);
200+
}
201+
179202
}

0 commit comments

Comments
 (0)