Skip to content

Added ability to override type #131

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 1 commit into from
Dec 15, 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ laravel-wallet - Easy work with virtual wallet.
* **Package**: laravel-wallet
* **Version**: [![Latest Stable Version](https://poser.pugx.org/bavix/laravel-wallet/v/stable)](https://packagist.org/packages/bavix/laravel-wallet)
* **PHP Version**: 7.2+
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.0`
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.x`
* **[Composer](https://getcomposer.org/):** `composer require bavix/laravel-wallet`

### Upgrade Guide
Expand Down
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [4.1.0] - 2019-12-15
### Added
- Added ability to override type

## [4.0.1] - 2019-11-30
### Fixed
- Encountered error: "You are not the owner of the wallet" #129 @arjayosma
Expand Down Expand Up @@ -435,7 +439,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.0.1...HEAD
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.1.0...HEAD
[4.1.0]: https://github.com/bavix/laravel-wallet/compare/4.0.1...4.1.0
[4.0.1]: https://github.com/bavix/laravel-wallet/compare/4.0.0...4.0.1
[4.0.0]: https://github.com/bavix/laravel-wallet/compare/3.3.0...4.0.0
[3.3.0]: https://github.com/bavix/laravel-wallet/compare/3.2.1...3.3.0
Expand Down
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'transaction' => [
'table' => 'transactions',
'model' => Transaction::class,
'casts' => [],
],

/**
Expand All @@ -86,6 +87,7 @@
'transfer' => [
'table' => 'transfers',
'model' => Transfer::class,
'casts' => [],
],

/**
Expand All @@ -94,6 +96,7 @@
'wallet' => [
'table' => 'wallets',
'model' => Wallet::class,
'casts' => [],
'default' => [
'name' => 'Default Wallet',
'slug' => 'default',
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class Transaction extends Model
'meta' => 'json'
];

/**
* @inheritDoc
*/
public function getCasts(): array
{
$this->casts = array_merge(
$this->casts,
config('wallet.transaction.casts', [])
);

return parent::getCasts();
}

/**
* @return string
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ class Transfer extends Model
'fee' => 'int',
];

/**
* @inheritDoc
*/
public function getCasts(): array
{
$this->casts = array_merge(
$this->casts,
config('wallet.transfer.casts', [])
);

return parent::getCasts();
}

/**
* @return string
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan
'decimal_places' => 'int',
];

/**
* @inheritDoc
*/
public function getCasts(): array
{
$this->casts = array_merge(
$this->casts,
config('wallet.wallet.casts', [])
);

return parent::getCasts();
}

/**
* @return string
*/
Expand Down
77 changes: 77 additions & 0 deletions tests/CastsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Bavix\Wallet\Test;

use Bavix\Wallet\Test\Models\Buyer;
use Bavix\Wallet\Test\Models\User;

class CastsTest extends TestCase
{

/**
* @return void
*/
public function testModelWallet(): void
{
/**
* @var Buyer $buyer
*/
$buyer = factory(Buyer::class)->create();
$this->assertEquals($buyer->balance, 0);

$this->assertIsInt($buyer->wallet->getKey());
$this->assertEquals($buyer->wallet->getCasts()['id'], 'int');

config(['wallet.wallet.casts.id' => 'string']);
$this->assertIsString($buyer->wallet->getKey());
$this->assertEquals($buyer->wallet->getCasts()['id'], 'string');
}

/**
* @return void
*/
public function testModelTransfer(): void
{
/**
* @var Buyer $buyer
* @var User $user
*/
$buyer = factory(Buyer::class)->create();
$user = factory(User::class)->create();
$this->assertEquals($buyer->balance, 0);
$this->assertEquals($user->balance, 0);

$deposit = $user->deposit(1000);
$this->assertEquals($user->balance, $deposit->amount);

$transfer = $user->transfer($buyer, 700);

$this->assertIsInt($transfer->getKey());
$this->assertEquals($transfer->getCasts()['id'], 'int');

config(['wallet.transfer.casts.id' => 'string']);
$this->assertIsString($transfer->getKey());
$this->assertEquals($transfer->getCasts()['id'], 'string');
}

/**
* @return void
*/
public function testModelTransaction(): void
{
/**
* @var Buyer $buyer
*/
$buyer = factory(Buyer::class)->create();
$this->assertEquals($buyer->balance, 0);
$deposit = $buyer->deposit(1);

$this->assertIsInt($deposit->getKey());
$this->assertEquals($deposit->getCasts()['id'], 'int');

config(['wallet.transaction.casts.id' => 'string']);
$this->assertIsString($deposit->getKey());
$this->assertEquals($deposit->getCasts()['id'], 'string');
}

}