Skip to content

Commit 6142983

Browse files
author
Babichev Maxim
committed
return throw message & add unit-test's
1 parent 1bd202c commit 6142983

File tree

3 files changed

+256
-2
lines changed

3 files changed

+256
-2
lines changed

src/Services/CommonService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ public function verifyWithdraw(Wallet $wallet, int $amount, bool $allowZero = nu
149149
}
150150

151151
if (!$wallet->canWithdraw($amount, $allowZero)) {
152-
// throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
153-
throw new InsufficientFunds("{$wallet->balance}, $amount");
152+
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
154153
}
155154
}
156155

tests/GiftDiscountTaxTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Models\Transfer;
6+
use Bavix\Wallet\Services\WalletService;
7+
use Bavix\Wallet\Test\Models\Buyer;
8+
use Bavix\Wallet\Test\Models\ItemDiscountTax;
9+
10+
class GiftDiscountTaxTest extends TestCase
11+
{
12+
13+
/**
14+
* @return void
15+
*/
16+
public function testGift(): void
17+
{
18+
/**
19+
* @var Buyer $first
20+
* @var Buyer $second
21+
* @var ItemDiscountTax $product
22+
*/
23+
[$first, $second] = factory(Buyer::class, 2)->create();
24+
$product = factory(ItemDiscountTax::class)->create([
25+
'quantity' => 1,
26+
]);
27+
28+
$this->assertEquals($first->balance, 0);
29+
$this->assertEquals($second->balance, 0);
30+
31+
$fee = app(WalletService::class)->fee(
32+
$product,
33+
$product->getAmountProduct($first) - $product->getPersonalDiscount($first)
34+
);
35+
36+
$first->deposit($product->getAmountProduct($first) + $fee);
37+
$this->assertEquals(
38+
$first->balance,
39+
$product->getAmountProduct($first) + $fee
40+
);
41+
42+
$transfer = $first->wallet->gift($second, $product);
43+
$this->assertEquals($first->balance, $product->getPersonalDiscount($first));
44+
$this->assertEquals($second->balance, 0);
45+
$this->assertNull($first->paid($product, true));
46+
$this->assertNotNull($second->paid($product, true));
47+
$this->assertNull($second->wallet->paid($product));
48+
$this->assertNotNull($second->wallet->paid($product, true));
49+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
50+
}
51+
52+
/**
53+
* @return void
54+
*/
55+
public function testRefund(): void
56+
{
57+
/**
58+
* @var Buyer $first
59+
* @var Buyer $second
60+
* @var ItemDiscountTax $product
61+
*/
62+
[$first, $second] = factory(Buyer::class, 2)->create();
63+
$product = factory(ItemDiscountTax::class)->create([
64+
'quantity' => 1,
65+
]);
66+
67+
$this->assertEquals($first->balance, 0);
68+
$this->assertEquals($second->balance, 0);
69+
70+
$fee = app(WalletService::class)->fee(
71+
$product,
72+
$product->getAmountProduct($first) - $product->getPersonalDiscount($first)
73+
);
74+
75+
$first->deposit($product->getAmountProduct($first) + $fee);
76+
$this->assertEquals($first->balance, $product->getAmountProduct($first) + $fee);
77+
78+
$transfer = $first->wallet->gift($second, $product);
79+
$this->assertEquals($first->balance, $product->getPersonalDiscount($first));
80+
$this->assertEquals($second->balance, 0);
81+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
82+
83+
$first->withdraw($product->getPersonalDiscount($first));
84+
$this->assertEquals($first->balance, 0);
85+
86+
$this->assertFalse($second->wallet->safeRefund($product));
87+
$this->assertTrue($second->wallet->refundGift($product));
88+
89+
$this->assertEquals(
90+
$first->balance,
91+
$product->getAmountProduct($first) - $product->getPersonalDiscount($first)
92+
);
93+
94+
$first->withdraw($first->balance);
95+
$this->assertEquals($first->balance, 0);
96+
$this->assertEquals($second->balance, 0);
97+
98+
$this->assertNull($second->wallet->safeGift($first, $product));
99+
100+
$secondFee = app(WalletService::class)->fee(
101+
$product,
102+
$product->getAmountProduct($second) - $product->getPersonalDiscount($second)
103+
);
104+
105+
$transfer = $second->wallet->forceGift($first, $product);
106+
$this->assertNotNull($transfer);
107+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
108+
109+
$this->assertEquals(
110+
$second->balance,
111+
-(($product->getAmountProduct($second) + $secondFee) - $product->getPersonalDiscount($second))
112+
);
113+
114+
$second->deposit(-$second->balance);
115+
$this->assertEquals($second->balance, 0);
116+
$this->assertEquals($first->balance, 0);
117+
118+
$product->withdraw($product->balance);
119+
$this->assertEquals($product->balance, 0);
120+
121+
$this->assertFalse($first->safeRefundGift($product));
122+
$this->assertTrue($first->forceRefundGift($product));
123+
124+
$this->assertEquals($second->balance, -$product->balance);
125+
126+
$this->assertEquals(
127+
$product->balance,
128+
-($product->getAmountProduct($second) - $product->getPersonalDiscount($second))
129+
);
130+
131+
$this->assertEquals(
132+
$second->balance,
133+
$product->getAmountProduct($second) - $product->getPersonalDiscount($second)
134+
);
135+
136+
$second->withdraw($second->balance);
137+
$this->assertEquals($second->balance, 0);
138+
}
139+
140+
}

tests/GiftDiscountTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Models\Transfer;
6+
use Bavix\Wallet\Test\Models\Buyer;
7+
use Bavix\Wallet\Test\Models\ItemDiscount;
8+
9+
class GiftDiscountTest extends TestCase
10+
{
11+
12+
/**
13+
* @return void
14+
*/
15+
public function testGift(): void
16+
{
17+
/**
18+
* @var Buyer $first
19+
* @var Buyer $second
20+
* @var ItemDiscount $product
21+
*/
22+
[$first, $second] = factory(Buyer::class, 2)->create();
23+
$product = factory(ItemDiscount::class)->create([
24+
'quantity' => 1,
25+
]);
26+
27+
$this->assertEquals($first->balance, 0);
28+
$this->assertEquals($second->balance, 0);
29+
30+
$first->deposit($product->getAmountProduct($first) - $product->getPersonalDiscount($first));
31+
$this->assertEquals(
32+
$first->balance,
33+
$product->getAmountProduct($first) - $product->getPersonalDiscount($first)
34+
);
35+
36+
$transfer = $first->wallet->gift($second, $product);
37+
$this->assertEquals($first->balance, 0);
38+
$this->assertEquals($second->balance, 0);
39+
$this->assertNull($first->paid($product, true));
40+
$this->assertNotNull($second->paid($product, true));
41+
$this->assertNull($second->wallet->paid($product));
42+
$this->assertNotNull($second->wallet->paid($product, true));
43+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
44+
}
45+
46+
/**
47+
* @return void
48+
*/
49+
public function testRefund(): void
50+
{
51+
/**
52+
* @var Buyer $first
53+
* @var Buyer $second
54+
* @var ItemDiscount $product
55+
*/
56+
[$first, $second] = factory(Buyer::class, 2)->create();
57+
$product = factory(ItemDiscount::class)->create([
58+
'quantity' => 1,
59+
]);
60+
61+
$this->assertEquals($first->balance, 0);
62+
$this->assertEquals($second->balance, 0);
63+
64+
$first->deposit($product->getAmountProduct($first));
65+
$this->assertEquals($first->balance, $product->getAmountProduct($first));
66+
67+
$transfer = $first->wallet->gift($second, $product);
68+
$this->assertGreaterThan(0, $first->balance);
69+
$this->assertEquals($first->balance, $product->getPersonalDiscount($first));
70+
$this->assertEquals($second->balance, 0);
71+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
72+
73+
$this->assertFalse($second->wallet->safeRefund($product));
74+
$this->assertTrue($second->wallet->refundGift($product));
75+
76+
$this->assertEquals($first->balance, $product->getAmountProduct($first));
77+
$this->assertEquals($second->balance, 0);
78+
79+
$this->assertNull($second->wallet->safeGift($first, $product));
80+
81+
$transfer = $second->wallet->forceGift($first, $product);
82+
$this->assertNotNull($transfer);
83+
$this->assertEquals($transfer->status, Transfer::STATUS_GIFT);
84+
85+
$this->assertEquals(
86+
$second->balance,
87+
-($product->getAmountProduct($second) - $product->getPersonalDiscount($second))
88+
);
89+
90+
$second->deposit(-$second->balance);
91+
$this->assertEquals($second->balance, 0);
92+
93+
$first->withdraw($product->getAmountProduct($first));
94+
$this->assertEquals($first->balance, 0);
95+
96+
$product->withdraw($product->balance);
97+
$this->assertEquals($product->balance, 0);
98+
99+
$this->assertFalse($first->safeRefundGift($product));
100+
$this->assertTrue($first->forceRefundGift($product));
101+
$this->assertEquals(
102+
$product->balance,
103+
-($product->getAmountProduct($second) - $product->getPersonalDiscount($second))
104+
);
105+
106+
$this->assertEquals(
107+
$second->balance,
108+
$product->getAmountProduct($second) - $product->getPersonalDiscount($second)
109+
);
110+
111+
$second->withdraw($second->balance);
112+
$this->assertEquals($second->balance, 0);
113+
}
114+
115+
}

0 commit comments

Comments
 (0)