Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Santa fee #112

Merged
merged 4 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}