Skip to content

Commit 3bcef98

Browse files
authored
Merge pull request #112 from bavix/santa-fee
Santa fee
2 parents 96a8445 + a4af301 commit 3bcef98

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bavix\Wallet\Test;
44

5+
use Bavix\Wallet\Exceptions\InsufficientFunds;
56
use Bavix\Wallet\Models\Transaction;
67
use Bavix\Wallet\Test\Models\Buyer;
78
use Bavix\Wallet\Test\Models\ItemTax;
@@ -53,4 +54,82 @@ public function testPay(): void
5354
$this->assertEquals($buyer->balance, 0);
5455
}
5556

57+
/**
58+
* @return void
59+
*/
60+
public function testGift(): void
61+
{
62+
/**
63+
* @var Buyer $santa
64+
* @var Buyer $child
65+
* @var ItemTax $product
66+
*/
67+
[$santa, $child] = factory(Buyer::class, 2)->create();
68+
$product = factory(ItemTax::class)->create([
69+
'quantity' => 1,
70+
]);
71+
72+
$fee = (int)($product->price * $product->getFeePercent() / 100);
73+
$balance = $product->price + $fee;
74+
75+
$this->assertEquals($santa->balance, 0);
76+
$this->assertEquals($child->balance, 0);
77+
$santa->deposit($balance);
78+
79+
$this->assertNotEquals($santa->balance, 0);
80+
$this->assertEquals($child->balance, 0);
81+
$transfer = $santa->wallet->gift($child, $product);
82+
$this->assertNotNull($transfer);
83+
84+
/**
85+
* @var Transaction $withdraw
86+
* @var Transaction $deposit
87+
*/
88+
$withdraw = $transfer->withdraw;
89+
$deposit = $transfer->deposit;
90+
91+
$this->assertEquals($withdraw->amount, -$balance);
92+
$this->assertEquals($deposit->amount, $product->getAmountProduct());
93+
$this->assertNotEquals($deposit->amount, $withdraw->amount);
94+
$this->assertEquals($transfer->fee, $fee);
95+
96+
$this->assertFalse($santa->safeRefundGift($product));
97+
$this->assertTrue($child->refundGift($product));
98+
$this->assertEquals($santa->balance, $deposit->amount);
99+
$this->assertEquals($child->balance, 0);
100+
$this->assertEquals($product->balance, 0);
101+
102+
$santa->withdraw($santa->balance);
103+
$this->assertEquals($santa->balance, 0);
104+
}
105+
106+
/**
107+
* @return void
108+
*/
109+
public function testGiftFail(): void
110+
{
111+
$this->expectException(InsufficientFunds::class);
112+
113+
/**
114+
* @var Buyer $santa
115+
* @var Buyer $child
116+
* @var ItemTax $product
117+
*/
118+
[$santa, $child] = factory(Buyer::class, 2)->create();
119+
$product = factory(ItemTax::class)->create([
120+
'price' => 200,
121+
'quantity' => 1,
122+
]);
123+
124+
$this->assertEquals($santa->balance, 0);
125+
$this->assertEquals($child->balance, 0);
126+
$santa->deposit($product->getAmountProduct());
127+
128+
$this->assertNotEquals($santa->balance, 0);
129+
$this->assertEquals($child->balance, 0);
130+
$santa->wallet->gift($child, $product);
131+
132+
$this->assertEquals($santa->balance, 0);
133+
}
134+
56135
}

0 commit comments

Comments
 (0)