Skip to content

Commit cf9dc16

Browse files
author
Babichev Maxim
committed
#111 santa fix
1 parent 60e2c6c commit cf9dc16

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Lock settings for highload projects
3030
*/
3131
'lock' => [
32+
'cache' => 'memcached',
3233
'enabled' => false,
3334
'seconds' => 1,
3435
],

src/Services/LockService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ protected function bindTo($self, \Closure $closure): \Closure
5959
protected function cache(): ?Store
6060
{
6161
try {
62-
return Cache::getStore();
62+
return Cache::store(config('wallet.lock.cache'))
63+
->getStore();
6364
} catch (\Throwable $throwable) {
6465
return null;
6566
}

src/Traits/HasGift.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
7575
* Santa pays taxes
7676
*/
7777
if (!$force) {
78-
$commonService->verifyWithdraw($santa, $amount);
78+
$commonService->verifyWithdraw($santa, $amount + $fee);
7979
}
8080

8181
$withdraw = $commonService->forceWithdraw($santa, $amount + $fee, $meta);
82-
8382
$deposit = $commonService->deposit($product, $amount, $meta);
8483

8584
$from = app(WalletService::class)

tests/TaxTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,53 @@ public function testPay(): void
5353
$this->assertEquals($buyer->balance, 0);
5454
}
5555

56+
/**
57+
* @return void
58+
*/
59+
public function testGift(): void
60+
{
61+
/**
62+
* @var Buyer $santa
63+
* @var Buyer $child
64+
* @var ItemTax $product
65+
*/
66+
[$santa, $child] = factory(Buyer::class, 2)->create();
67+
$product = factory(ItemTax::class)->create([
68+
'quantity' => 1,
69+
]);
70+
71+
$fee = (int)($product->price * $product->getFeePercent() / 100);
72+
$balance = $product->price + $fee;
73+
74+
$this->assertEquals($santa->balance, 0);
75+
$this->assertEquals($child->balance, 0);
76+
$santa->deposit($balance);
77+
78+
$this->assertNotEquals($santa->balance, 0);
79+
$this->assertEquals($child->balance, 0);
80+
$transfer = $santa->wallet->gift($child, $product);
81+
$this->assertNotNull($transfer);
82+
83+
/**
84+
* @var Transaction $withdraw
85+
* @var Transaction $deposit
86+
*/
87+
$withdraw = $transfer->withdraw;
88+
$deposit = $transfer->deposit;
89+
90+
$this->assertEquals($withdraw->amount, -$balance);
91+
$this->assertEquals($deposit->amount, $product->getAmountProduct());
92+
$this->assertNotEquals($deposit->amount, $withdraw->amount);
93+
$this->assertEquals($transfer->fee, $fee);
94+
95+
$this->assertFalse($santa->safeRefundGift($product));
96+
$this->assertTrue($child->refundGift($product));
97+
$this->assertEquals($santa->balance, $deposit->amount);
98+
$this->assertEquals($child->balance, 0);
99+
$this->assertEquals($product->balance, 0);
100+
101+
$santa->withdraw($santa->balance);
102+
$this->assertEquals($santa->balance, 0);
103+
}
104+
56105
}

0 commit comments

Comments
 (0)