Skip to content

Commit 3865c76

Browse files
committed
bug fixed; up coverage
1 parent d3cf380 commit 3865c76

File tree

8 files changed

+203
-2
lines changed

8 files changed

+203
-2
lines changed

src/Models/Wallet.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ public function getTable(): string
6060
public function setNameAttribute(string $name): void
6161
{
6262
$this->attributes['name'] = $name;
63-
$this->attributes['slug'] = Str::slug($name);
63+
64+
/**
65+
* Must be updated only if the model does not exist
66+
* or the slug is empty
67+
*/
68+
if (!$this->exists && !\array_key_exists('slug', $this->attributes)) {
69+
$this->attributes['slug'] = Str::slug($name);
70+
}
6471
}
6572

6673
/**

src/Traits/HasWallet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function deposit(int $amount, ?array $meta = null, bool $confirmed = true
8484
*/
8585
public function withdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
8686
{
87-
if (!$this->balance) {
87+
if ($amount && !$this->balance) {
8888
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty'));
8989
}
9090

tests/Models/ItemTax.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test\Models;
4+
5+
use Bavix\Wallet\Interfaces\Taxing;
6+
7+
class ItemTax extends Item implements Taxing
8+
{
9+
10+
/**
11+
* @return string
12+
*/
13+
public function getTable(): string
14+
{
15+
return 'items';
16+
}
17+
18+
/**
19+
* Specify the percentage of the amount.
20+
* For example, the product costs $100, the equivalent of 15%.
21+
* That's $115.
22+
*
23+
* Minimum 0; Maximum 100
24+
* Example: return 7.5; // 7.5%
25+
*
26+
* @return float
27+
*/
28+
public function getFeePercent(): float
29+
{
30+
return 7.5;
31+
}
32+
33+
}

tests/MultiWalletTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,32 @@ public function testWalletUnique(): void
251251
]);
252252
}
253253

254+
/**
255+
* @return void
256+
*/
257+
public function testGetWallet(): void
258+
{
259+
/**
260+
* @var UserMulti $user
261+
*/
262+
$user = factory(UserMulti::class)->create();
263+
264+
$firstWallet = $user->createWallet([
265+
'name' => 'My Test',
266+
'slug' => 'test',
267+
]);
268+
269+
$secondWallet = $user->getWallet('test');
270+
$this->assertEquals($secondWallet->getKey(), $firstWallet->getKey());
271+
272+
$test2 = $user->wallets()->create([
273+
'name' => 'Test2'
274+
]);
275+
276+
$this->assertEquals(
277+
$test2->getKey(),
278+
$user->getWallet('test2')->getKey()
279+
);
280+
}
281+
254282
}

tests/ProductTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,48 @@ public function testForcePay(): void
168168
$this->assertEquals($buyer->balance, 0);
169169
}
170170

171+
/**
172+
* @return void
173+
*/
174+
public function testPayFree(): void
175+
{
176+
/**
177+
* @var Buyer $buyer
178+
* @var Item $product
179+
*/
180+
$buyer = factory(Buyer::class)->create();
181+
$product = factory(Item::class)->create([
182+
'quantity' => 1,
183+
]);
184+
185+
$this->assertEquals($buyer->balance, 0);
186+
187+
$buyer->payFree($product);
188+
$this->assertEquals($buyer->balance, 0);
189+
$this->assertEquals($product->balance, 0);
190+
191+
$buyer->refund($product);
192+
$this->assertEquals($buyer->balance, 0);
193+
$this->assertEquals($product->balance, 0);
194+
}
195+
196+
/**
197+
* @return void
198+
* @expectedException \Bavix\Wallet\Exceptions\ProductEnded
199+
*/
200+
public function testPayFreeOutOfStock(): void
201+
{
202+
/**
203+
* @var Buyer $buyer
204+
* @var Item $product
205+
*/
206+
$buyer = factory(Buyer::class)->create();
207+
$product = factory(Item::class)->create([
208+
'quantity' => 1,
209+
]);
210+
211+
$this->assertNotNull($buyer->payFree($product));
212+
$buyer->payFree($product);
213+
}
214+
171215
}

tests/TaxTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Models\Transaction;
6+
use Bavix\Wallet\Test\Models\Buyer;
7+
use Bavix\Wallet\Test\Models\ItemTax;
8+
9+
class TaxTest extends TestCase
10+
{
11+
12+
/**
13+
* @return void
14+
*/
15+
public function testPay(): void
16+
{
17+
/**
18+
* @var Buyer $buyer
19+
* @var ItemTax $product
20+
*/
21+
$buyer = factory(Buyer::class)->create();
22+
$product = factory(ItemTax::class)->create([
23+
'quantity' => 1,
24+
]);
25+
26+
$balance = (int) ($product->price +
27+
$product->price * $product->getFeePercent() / 100);
28+
29+
$this->assertEquals($buyer->balance, 0);
30+
$buyer->deposit($balance);
31+
32+
$this->assertNotEquals($buyer->balance, 0);
33+
$transfer = $buyer->pay($product);
34+
$this->assertNotNull($transfer);
35+
36+
/**
37+
* @var Transaction $withdraw
38+
* @var Transaction $deposit
39+
*/
40+
$withdraw = $transfer->withdraw;
41+
$deposit = $transfer->deposit;
42+
43+
$this->assertEquals($withdraw->amount, -$balance);
44+
$this->assertEquals($deposit->amount, $product->getAmountProduct());
45+
$this->assertNotEquals($deposit->amount, $withdraw->amount);
46+
47+
$buyer->refund($product);
48+
$this->assertEquals($buyer->balance, $deposit->amount);
49+
$this->assertEquals($product->balance, 0);
50+
51+
$buyer->withdraw($buyer->balance);
52+
$this->assertEquals($buyer->balance, 0);
53+
}
54+
55+
}

tests/WalletTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public function testInvalidWithdraw(): void
7474
$user->withdraw(-1);
7575
}
7676

77+
/**
78+
* @return void
79+
* @expectedException \Bavix\Wallet\Exceptions\InsufficientFunds
80+
*/
81+
public function testInsufficientFundsWithdraw(): void
82+
{
83+
$user = factory(User::class)->create();
84+
$user->deposit(1);
85+
$user->withdraw(2);
86+
}
87+
7788
/**
7889
* @return void
7990
*/

tests/factories/ItemTaxFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use Bavix\Wallet\Test\Models\ItemTax;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Model Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
| This directory should contain each of the model factory definitions for
12+
| your application. Factories provide a convenient way to generate new
13+
| model instances for testing / seeding your application's database.
14+
|
15+
*/
16+
17+
$factory->define(ItemTax::class, function(Faker $faker) {
18+
return [
19+
'name' => $faker->domainName,
20+
'price' => \random_int(1, 100),
21+
'quantity' => \random_int(0, 10),
22+
];
23+
});

0 commit comments

Comments
 (0)