Skip to content

Commit a3e8014

Browse files
author
Babichev Maxim
committed
fix unit cases and patch bug
1 parent 76d3b94 commit a3e8014

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

src/Interfaces/Discount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface Discount extends Product
88
* @param Customer $customer
99
* @return int
1010
*/
11-
public function getDiscountProduct(Customer $customer): int;
11+
public function getPersonalDiscount(Customer $customer): int;
1212
}

src/Services/CommonService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Bavix\Wallet\Objects\Bring;
1313
use Bavix\Wallet\Objects\Operation;
1414
use Bavix\Wallet\Traits\HasWallet;
15+
use function max;
1516
use function app;
1617
use function compact;
1718

@@ -47,7 +48,7 @@ public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $met
4748
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($from, $to, $amount, $meta, $status) {
4849
$from = app(WalletService::class)->getWallet($from);
4950
$discount = app(WalletService::class)->discount($from, $to);
50-
$amount -= $discount;
51+
$amount = max(0, $amount - $discount);
5152

5253
$fee = app(WalletService::class)->fee($to, $amount);
5354
$withdraw = $this->forceWithdraw($from, $amount + $fee, $meta);

src/Services/WalletService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class WalletService
2525
public function discount(Wallet $customer, Wallet $product): int
2626
{
2727
if ($customer instanceof Customer && $product instanceof Discount) {
28-
return $product->getDiscountProduct($customer);
28+
return $product->getPersonalDiscount($customer);
2929
}
3030

3131
// without discount

tests/DiscountTest.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testPay(): void
3535

3636
$this->assertEquals(
3737
$buyer->balance,
38-
$product->getDiscountProduct($buyer)
38+
$product->getPersonalDiscount($buyer)
3939
);
4040

4141
/**
@@ -71,10 +71,10 @@ public function testRefund(): void
7171
{
7272
/**
7373
* @var Buyer $buyer
74-
* @var Item $product
74+
* @var ItemDiscount $product
7575
*/
7676
$buyer = factory(Buyer::class)->create();
77-
$product = factory(Item::class)->create([
77+
$product = factory(ItemDiscount::class)->create([
7878
'quantity' => 1,
7979
]);
8080

@@ -98,8 +98,12 @@ public function testRefund(): void
9898

9999
$transfer = $buyer->pay($product);
100100
$this->assertNotNull($transfer);
101-
$this->assertEquals($buyer->balance, 0);
102-
$this->assertEquals($product->balance, $product->getAmountProduct($buyer));
101+
$this->assertEquals($buyer->balance, $product->getPersonalDiscount($buyer));
102+
$this->assertEquals(
103+
$product->balance,
104+
$product->getAmountProduct($buyer) - $product->getPersonalDiscount($buyer)
105+
);
106+
103107
$this->assertEquals($transfer->status, Transfer::STATUS_PAID);
104108

105109
$this->assertTrue($buyer->refund($product));
@@ -117,10 +121,10 @@ public function testForceRefund(): void
117121
{
118122
/**
119123
* @var Buyer $buyer
120-
* @var Item $product
124+
* @var ItemDiscount $product
121125
*/
122126
$buyer = factory(Buyer::class)->create();
123-
$product = factory(Item::class)->create([
127+
$product = factory(ItemDiscount::class)->create([
124128
'quantity' => 1,
125129
]);
126130

@@ -130,16 +134,23 @@ public function testForceRefund(): void
130134
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
131135

132136
$buyer->pay($product);
133-
$this->assertEquals($buyer->balance, 0);
134-
$this->assertEquals($product->balance, $product->getAmountProduct($buyer));
137+
$this->assertEquals($buyer->balance, $product->getPersonalDiscount($buyer));
138+
139+
$this->assertEquals(
140+
$product->balance,
141+
$product->getAmountProduct($buyer) - $product->getPersonalDiscount($buyer)
142+
);
135143

136144
$product->withdraw($product->balance);
137145
$this->assertEquals($product->balance, 0);
138146

139147
$this->assertFalse($buyer->safeRefund($product));
140148
$this->assertTrue($buyer->forceRefund($product));
141149

142-
$this->assertEquals($product->balance, -$product->getAmountProduct($buyer));
150+
$this->assertEquals(
151+
$product->balance, -($product->getAmountProduct($buyer) - $product->getPersonalDiscount($buyer))
152+
);
153+
143154
$this->assertEquals($buyer->balance, $product->getAmountProduct($buyer));
144155
$product->deposit(-$product->balance);
145156
$buyer->withdraw($buyer->balance);
@@ -157,10 +168,10 @@ public function testOutOfStock(): void
157168

158169
/**
159170
* @var Buyer $buyer
160-
* @var Item $product
171+
* @var ItemDiscount $product
161172
*/
162173
$buyer = factory(Buyer::class)->create();
163-
$product = factory(Item::class)->create([
174+
$product = factory(ItemDiscount::class)->create([
164175
'quantity' => 1,
165176
]);
166177

@@ -176,17 +187,19 @@ public function testForcePay(): void
176187
{
177188
/**
178189
* @var Buyer $buyer
179-
* @var Item $product
190+
* @var ItemDiscount $product
180191
*/
181192
$buyer = factory(Buyer::class)->create();
182-
$product = factory(Item::class)->create([
193+
$product = factory(ItemDiscount::class)->create([
183194
'quantity' => 1,
184195
]);
185196

186197
$this->assertEquals($buyer->balance, 0);
187198
$buyer->forcePay($product);
188199

189-
$this->assertEquals($buyer->balance, -$product->getAmountProduct($buyer));
200+
$this->assertEquals(
201+
$buyer->balance, -($product->getAmountProduct($buyer) - $product->getPersonalDiscount($buyer))
202+
);
190203

191204
$buyer->deposit(-$buyer->balance);
192205
$this->assertEquals($buyer->balance, 0);
@@ -199,10 +212,10 @@ public function testPayFree(): void
199212
{
200213
/**
201214
* @var Buyer $buyer
202-
* @var Item $product
215+
* @var ItemDiscount $product
203216
*/
204217
$buyer = factory(Buyer::class)->create();
205-
$product = factory(Item::class)->create([
218+
$product = factory(ItemDiscount::class)->create([
206219
'quantity' => 1,
207220
]);
208221

@@ -224,10 +237,10 @@ public function testFreePay(): void
224237
{
225238
/**
226239
* @var Buyer $buyer
227-
* @var Item $product
240+
* @var ItemDiscount $product
228241
*/
229242
$buyer = factory(Buyer::class)->create();
230-
$product = factory(Item::class)->create([
243+
$product = factory(ItemDiscount::class)->create([
231244
'quantity' => 1,
232245
]);
233246

@@ -255,10 +268,10 @@ public function testPayFreeOutOfStock(): void
255268

256269
/**
257270
* @var Buyer $buyer
258-
* @var Item $product
271+
* @var ItemDiscount $product
259272
*/
260273
$buyer = factory(Buyer::class)->create();
261-
$product = factory(Item::class)->create([
274+
$product = factory(ItemDiscount::class)->create([
262275
'quantity' => 1,
263276
]);
264277

tests/Models/ItemDiscount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getTable(): string
2222
* @param Customer $customer
2323
* @return int
2424
*/
25-
public function getDiscountProduct(Customer $customer): int
25+
public function getPersonalDiscount(Customer $customer): int
2626
{
2727
$wallet = app(WalletService::class)
2828
->getWallet($customer);

0 commit comments

Comments
 (0)