Skip to content

Commit

Permalink
Merge pull request #112 from bavix/santa-fee
Browse files Browse the repository at this point in the history
Santa fee
  • Loading branch information
rez1dent3 committed Sep 28, 2019
2 parents 96a8445 + a4af301 commit 3bcef98
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Lock settings for highload projects
*/
'lock' => [
'cache' => 'memcached',
'enabled' => false,
'seconds' => 1,
],
Expand Down
3 changes: 2 additions & 1 deletion src/Services/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ protected function bindTo($self, \Closure $closure): \Closure
protected function cache(): ?Store
{
try {
return Cache::getStore();
return Cache::store(config('wallet.lock.cache'))
->getStore();
} catch (\Throwable $throwable) {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Traits/HasGift.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
* Santa pays taxes
*/
if (!$force) {
$commonService->verifyWithdraw($santa, $amount);
$commonService->verifyWithdraw($santa, $amount + $fee);
}

$withdraw = $commonService->forceWithdraw($santa, $amount + $fee, $meta);

$deposit = $commonService->deposit($product, $amount, $meta);

$from = app(WalletService::class)
Expand Down
79 changes: 79 additions & 0 deletions tests/TaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bavix\Wallet\Test;

use Bavix\Wallet\Exceptions\InsufficientFunds;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Test\Models\Buyer;
use Bavix\Wallet\Test\Models\ItemTax;
Expand Down Expand Up @@ -53,4 +54,82 @@ public function testPay(): void
$this->assertEquals($buyer->balance, 0);
}

/**
* @return void
*/
public function testGift(): void
{
/**
* @var Buyer $santa
* @var Buyer $child
* @var ItemTax $product
*/
[$santa, $child] = factory(Buyer::class, 2)->create();
$product = factory(ItemTax::class)->create([
'quantity' => 1,
]);

$fee = (int)($product->price * $product->getFeePercent() / 100);
$balance = $product->price + $fee;

$this->assertEquals($santa->balance, 0);
$this->assertEquals($child->balance, 0);
$santa->deposit($balance);

$this->assertNotEquals($santa->balance, 0);
$this->assertEquals($child->balance, 0);
$transfer = $santa->wallet->gift($child, $product);
$this->assertNotNull($transfer);

/**
* @var Transaction $withdraw
* @var Transaction $deposit
*/
$withdraw = $transfer->withdraw;
$deposit = $transfer->deposit;

$this->assertEquals($withdraw->amount, -$balance);
$this->assertEquals($deposit->amount, $product->getAmountProduct());
$this->assertNotEquals($deposit->amount, $withdraw->amount);
$this->assertEquals($transfer->fee, $fee);

$this->assertFalse($santa->safeRefundGift($product));
$this->assertTrue($child->refundGift($product));
$this->assertEquals($santa->balance, $deposit->amount);
$this->assertEquals($child->balance, 0);
$this->assertEquals($product->balance, 0);

$santa->withdraw($santa->balance);
$this->assertEquals($santa->balance, 0);
}

/**
* @return void
*/
public function testGiftFail(): void
{
$this->expectException(InsufficientFunds::class);

/**
* @var Buyer $santa
* @var Buyer $child
* @var ItemTax $product
*/
[$santa, $child] = factory(Buyer::class, 2)->create();
$product = factory(ItemTax::class)->create([
'price' => 200,
'quantity' => 1,
]);

$this->assertEquals($santa->balance, 0);
$this->assertEquals($child->balance, 0);
$santa->deposit($product->getAmountProduct());

$this->assertNotEquals($santa->balance, 0);
$this->assertEquals($child->balance, 0);
$santa->wallet->gift($child, $product);

$this->assertEquals($santa->balance, 0);
}

}

0 comments on commit 3bcef98

Please sign in to comment.