Skip to content

3.0-Dev #38

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

Merged
merged 4 commits into from
May 16, 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.7",
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7.5|^8.1"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 6 additions & 6 deletions src/Interfaces/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ interface Customer extends Wallet
* @return Transfer
* @throws
*/
public function pay(Product $product, bool $force = false): Transfer;
public function pay(Product $product, bool $force = null): Transfer;

/**
* @param Product $product
* @param bool $force
* @return null|Transfer
* @throws
*/
public function safePay(Product $product, bool $force = false): ?Transfer;
public function safePay(Product $product, bool $force = null): ?Transfer;

/**
* @param Product $product
Expand All @@ -35,7 +35,7 @@ public function forcePay(Product $product): Transfer;
* @param bool $gifts
* @return null|Transfer
*/
public function paid(Product $product, bool $gifts = false): ?Transfer;
public function paid(Product $product, bool $gifts = null): ?Transfer;

/**
* @param Product $product
Expand All @@ -44,21 +44,21 @@ public function paid(Product $product, bool $gifts = false): ?Transfer;
* @return bool
* @throws
*/
public function refund(Product $product, bool $force = false, bool $gifts = false): bool;
public function refund(Product $product, bool $force = null, bool $gifts = null): bool;

/**
* @param Product $product
* @param bool $force
* @param bool $gifts
* @return bool
*/
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool;
public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool;

/**
* @param Product $product
* @param bool $gifts
* @return bool
*/
public function forceRefund(Product $product, bool $gifts = false): bool;
public function forceRefund(Product $product, bool $gifts = null): bool;

}
2 changes: 1 addition & 1 deletion src/Interfaces/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Product extends Wallet
*
* @return bool
*/
public function canBuy(Customer $customer, bool $force = false): bool;
public function canBuy(Customer $customer, bool $force = null): bool;

/**
* @return int
Expand Down
18 changes: 9 additions & 9 deletions src/Traits/CanPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function payFree(Product $product): Transfer
* @param bool $force
* @return Transfer|null
*/
public function safePay(Product $product, bool $force = false): ?Transfer
public function safePay(Product $product, bool $force = null): ?Transfer
{
try {
return $this->pay($product, $force);
Expand All @@ -52,7 +52,7 @@ public function safePay(Product $product, bool $force = false): ?Transfer
* @return Transfer
* @throws
*/
public function pay(Product $product, bool $force = false): Transfer
public function pay(Product $product, bool $force = null): Transfer
{
if (!$product->canBuy($this, $force)) {
throw new ProductEnded(trans('wallet::errors.product_stock'));
Expand Down Expand Up @@ -91,7 +91,7 @@ public function forcePay(Product $product): Transfer
* @param bool $gifts
* @return bool
*/
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool
public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool
{
try {
return $this->refund($product, $force, $gifts);
Expand All @@ -107,7 +107,7 @@ public function safeRefund(Product $product, bool $force = false, bool $gifts =
* @return bool
* @throws
*/
public function refund(Product $product, bool $force = false, bool $gifts = false): bool
public function refund(Product $product, bool $force = null, bool $gifts = null): bool
{
$transfer = $this->paid($product, $gifts);

Expand Down Expand Up @@ -145,7 +145,7 @@ public function refund(Product $product, bool $force = false, bool $gifts = fals
* @param bool $gifts
* @return null|Transfer
*/
public function paid(Product $product, bool $gifts = false): ?Transfer
public function paid(Product $product, bool $gifts = null): ?Transfer
{
$status = [Transfer::STATUS_PAID];
if ($gifts) {
Expand All @@ -156,7 +156,7 @@ public function paid(Product $product, bool $gifts = false): ?Transfer
* @var Model $product
* @var Transfer $query
*/
$query = $this->holderTransfers();
$query = $this->transfers();
return $query
->where('to_type', $product->getMorphClass())
->where('to_id', $product->getKey())
Expand All @@ -171,7 +171,7 @@ public function paid(Product $product, bool $gifts = false): ?Transfer
* @return bool
* @throws
*/
public function forceRefund(Product $product, bool $gifts = false): bool
public function forceRefund(Product $product, bool $gifts = null): bool
{
return $this->refund($product, true, $gifts);
}
Expand All @@ -181,7 +181,7 @@ public function forceRefund(Product $product, bool $gifts = false): bool
* @param bool $force
* @return bool
*/
public function safeRefundGift(Product $product, bool $force = false): bool
public function safeRefundGift(Product $product, bool $force = null): bool
{
try {
return $this->refundGift($product, $force);
Expand All @@ -195,7 +195,7 @@ public function safeRefundGift(Product $product, bool $force = false): bool
* @param bool $force
* @return bool
*/
public function refundGift(Product $product, bool $force = false): bool
public function refundGift(Product $product, bool $force = null): bool
{
return $this->refund($product, $force, true);
}
Expand Down
1 change: 0 additions & 1 deletion src/Traits/CanPayFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ trait CanPayFloat
CanPay::assemble insteadof HasWalletFloat;
CanPay::change insteadof HasWalletFloat;
CanPay::transactions insteadof HasWalletFloat;
CanPay::holderTransfers insteadof HasWalletFloat;
CanPay::transfers insteadof HasWalletFloat;
CanPay::wallet insteadof HasWalletFloat;
CanPay::getBalanceAttribute insteadof HasWalletFloat;
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasGift.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait HasGift
* @param bool $force
* @return Transfer|null
*/
public function safeGift(Wallet $to, Product $product, bool $force = false): ?Transfer
public function safeGift(Wallet $to, Product $product, bool $force = null): ?Transfer
{
try {
return $this->gift($to, $product, $force);
Expand All @@ -44,7 +44,7 @@ public function safeGift(Wallet $to, Product $product, bool $force = false): ?Tr
* @param bool $force
* @return Transfer
*/
public function gift(Wallet $to, Product $product, bool $force = false): Transfer
public function gift(Wallet $to, Product $product, bool $force = null): Transfer
{
/**
* Who's giving? Let's call him Santa Claus
Expand Down
26 changes: 7 additions & 19 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $
'status' => $status,
'deposit_id' => $deposit->getKey(),
'withdraw_id' => $withdraw->getKey(),
'from_type' => $this->getMorphClass(),
'from_id' => $this->getKey(),
'from_type' => ($this instanceof WalletModel ? $this : $this->wallet)->getMorphClass(),
'from_id' => ($this instanceof WalletModel ? $this : $this->wallet)->getKey(),
'to_type' => $wallet->getMorphClass(),
'to_id' => $wallet->getKey(),
'fee' => \abs($withdraw->amount) - \abs($deposit->amount),
Expand Down Expand Up @@ -296,22 +296,6 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null,
});
}

/**
* holder transfers
*
* @return MorphMany
* @deprecated since laravel-wallet 2.5
* @see transfers
*/
public function holderTransfers(): MorphMany
{
if ($this instanceof WalletModel) {
return $this->holder->transfers();
}

return $this->transfers();
}

/**
* the transfer table is used to confirm the payment
* this method receives all transfers
Expand All @@ -320,6 +304,10 @@ public function holderTransfers(): MorphMany
*/
public function transfers(): MorphMany
{
if (!($this instanceof WalletModel)) {
return $this->wallet->transfers();
}

return $this->morphMany(config('wallet.transfer.model'), 'from');
}

Expand All @@ -331,7 +319,7 @@ public function transfers(): MorphMany
*/
public function wallet(): MorphOne
{
return ($this instanceof WalletModel ? $this->holder : $this)
return $this
->morphOne(config('wallet.wallet.model'), 'holder')
->where('slug', config('wallet.wallet.default.slug'))
->withDefault([
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait HasWallets
/**
* @var bool
*/
private $_loadedWallets = false;
private $_loadedWallets;

/**
* Get wallet by slug
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Item extends Model implements Product
*
* @return bool
*/
public function canBuy(Customer $customer, bool $force = false): bool
public function canBuy(Customer $customer, bool $force = null): bool
{
$result = $this->quantity > 0;

Expand Down
19 changes: 14 additions & 5 deletions tests/MultiWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Bavix\Wallet\Test;

use Bavix\Wallet\Exceptions\AmountInvalid;
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Test\Models\UserMulti;
use Illuminate\Database\QueryException;

class MultiWalletTest extends TestCase
{
Expand Down Expand Up @@ -45,10 +48,11 @@ public function testDeposit(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\AmountInvalid
*/
public function testInvalidDeposit(): void
{
$this->expectException(AmountInvalid::class);

/**
* @var UserMulti $user
*/
Expand All @@ -62,10 +66,11 @@ public function testInvalidDeposit(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
*/
public function testWithdraw(): void
{
$this->expectException(BalanceIsEmpty::class);

/**
* @var UserMulti $user
*/
Expand Down Expand Up @@ -93,10 +98,11 @@ public function testWithdraw(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
*/
public function testInvalidWithdraw(): void
{
$this->expectException(BalanceIsEmpty::class);

/**
* @var UserMulti $user
*/
Expand Down Expand Up @@ -199,10 +205,11 @@ public function testTransferYourself(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
*/
public function testBalanceIsEmpty(): void
{
$this->expectException(BalanceIsEmpty::class);

/**
* @var UserMulti $user
*/
Expand Down Expand Up @@ -241,10 +248,12 @@ public function testConfirmed(): void
}

/**
* @expectedException \Illuminate\Database\QueryException
* @return void
*/
public function testWalletUnique(): void
{
$this->expectException(QueryException::class);

/**
* @var UserMulti $user
*/
Expand Down
9 changes: 6 additions & 3 deletions tests/ProductTest.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\ProductEnded;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Test\Models\Buyer;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function testPay(): void
$this->assertEquals($buyer->getKey(), $withdraw->payable->getKey());
$this->assertEquals($product->getKey(), $deposit->payable->getKey());

$this->assertInstanceOf(Buyer::class, $transfer->from);
$this->assertInstanceOf(Buyer::class, $transfer->from->holder);
$this->assertInstanceOf(Item::class, $transfer->to);

$this->assertEquals($buyer->getKey(), $transfer->from->getKey());
Expand Down Expand Up @@ -133,10 +134,11 @@ public function testForceRefund(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\ProductEnded
*/
public function testOutOfStock(): void
{
$this->expectException(ProductEnded::class);

/**
* @var Buyer $buyer
* @var Item $product
Expand Down Expand Up @@ -204,10 +206,11 @@ public function testPayFree(): void

/**
* @return void
* @expectedException \Bavix\Wallet\Exceptions\ProductEnded
*/
public function testPayFreeOutOfStock(): void
{
$this->expectException(ProductEnded::class);

/**
* @var Buyer $buyer
* @var Item $product
Expand Down
Loading