Skip to content

Commit 32a3cae

Browse files
authored
Merge pull request #131 from bavix/develop
Added ability to override type
2 parents c67fb30 + 73469d2 commit 32a3cae

File tree

7 files changed

+126
-2
lines changed

7 files changed

+126
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ laravel-wallet - Easy work with virtual wallet.
2424
* **Package**: laravel-wallet
2525
* **Version**: [![Latest Stable Version](https://poser.pugx.org/bavix/laravel-wallet/v/stable)](https://packagist.org/packages/bavix/laravel-wallet)
2626
* **PHP Version**: 7.2+
27-
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.0`
27+
* **Laravel Version**: `5.5`, `5.6`, `5.7`, `5.8`, `6.x`
2828
* **[Composer](https://getcomposer.org/):** `composer require bavix/laravel-wallet`
2929

3030
### Upgrade Guide

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [4.1.0] - 2019-12-15
10+
### Added
11+
- Added ability to override type
12+
913
## [4.0.1] - 2019-11-30
1014
### Fixed
1115
- Encountered error: "You are not the owner of the wallet" #129 @arjayosma
@@ -435,7 +439,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
435439
- Exceptions: AmountInvalid, BalanceIsEmpty.
436440
- Models: Transfer, Transaction.
437441

438-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.0.1...HEAD
442+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.1.0...HEAD
443+
[4.1.0]: https://github.com/bavix/laravel-wallet/compare/4.0.1...4.1.0
439444
[4.0.1]: https://github.com/bavix/laravel-wallet/compare/4.0.0...4.0.1
440445
[4.0.0]: https://github.com/bavix/laravel-wallet/compare/3.3.0...4.0.0
441446
[3.3.0]: https://github.com/bavix/laravel-wallet/compare/3.2.1...3.3.0

config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'transaction' => [
7979
'table' => 'transactions',
8080
'model' => Transaction::class,
81+
'casts' => [],
8182
],
8283

8384
/**
@@ -86,6 +87,7 @@
8687
'transfer' => [
8788
'table' => 'transfers',
8889
'model' => Transfer::class,
90+
'casts' => [],
8991
],
9092

9193
/**
@@ -94,6 +96,7 @@
9496
'wallet' => [
9597
'table' => 'wallets',
9698
'model' => Wallet::class,
99+
'casts' => [],
97100
'default' => [
98101
'name' => 'Default Wallet',
99102
'slug' => 'default',

src/Models/Transaction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ class Transaction extends Model
5353
'meta' => 'json'
5454
];
5555

56+
/**
57+
* @inheritDoc
58+
*/
59+
public function getCasts(): array
60+
{
61+
$this->casts = array_merge(
62+
$this->casts,
63+
config('wallet.transaction.casts', [])
64+
);
65+
66+
return parent::getCasts();
67+
}
68+
5669
/**
5770
* @return string
5871
*/

src/Models/Transfer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ class Transfer extends Model
5959
'fee' => 'int',
6060
];
6161

62+
/**
63+
* @inheritDoc
64+
*/
65+
public function getCasts(): array
66+
{
67+
$this->casts = array_merge(
68+
$this->casts,
69+
config('wallet.transfer.casts', [])
70+
);
71+
72+
return parent::getCasts();
73+
}
74+
6275
/**
6376
* @return string
6477
*/

src/Models/Wallet.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan
5858
'decimal_places' => 'int',
5959
];
6060

61+
/**
62+
* @inheritDoc
63+
*/
64+
public function getCasts(): array
65+
{
66+
$this->casts = array_merge(
67+
$this->casts,
68+
config('wallet.wallet.casts', [])
69+
);
70+
71+
return parent::getCasts();
72+
}
73+
6174
/**
6275
* @return string
6376
*/

tests/CastsTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Test\Models\Buyer;
6+
use Bavix\Wallet\Test\Models\User;
7+
8+
class CastsTest extends TestCase
9+
{
10+
11+
/**
12+
* @return void
13+
*/
14+
public function testModelWallet(): void
15+
{
16+
/**
17+
* @var Buyer $buyer
18+
*/
19+
$buyer = factory(Buyer::class)->create();
20+
$this->assertEquals($buyer->balance, 0);
21+
22+
$this->assertIsInt($buyer->wallet->getKey());
23+
$this->assertEquals($buyer->wallet->getCasts()['id'], 'int');
24+
25+
config(['wallet.wallet.casts.id' => 'string']);
26+
$this->assertIsString($buyer->wallet->getKey());
27+
$this->assertEquals($buyer->wallet->getCasts()['id'], 'string');
28+
}
29+
30+
/**
31+
* @return void
32+
*/
33+
public function testModelTransfer(): void
34+
{
35+
/**
36+
* @var Buyer $buyer
37+
* @var User $user
38+
*/
39+
$buyer = factory(Buyer::class)->create();
40+
$user = factory(User::class)->create();
41+
$this->assertEquals($buyer->balance, 0);
42+
$this->assertEquals($user->balance, 0);
43+
44+
$deposit = $user->deposit(1000);
45+
$this->assertEquals($user->balance, $deposit->amount);
46+
47+
$transfer = $user->transfer($buyer, 700);
48+
49+
$this->assertIsInt($transfer->getKey());
50+
$this->assertEquals($transfer->getCasts()['id'], 'int');
51+
52+
config(['wallet.transfer.casts.id' => 'string']);
53+
$this->assertIsString($transfer->getKey());
54+
$this->assertEquals($transfer->getCasts()['id'], 'string');
55+
}
56+
57+
/**
58+
* @return void
59+
*/
60+
public function testModelTransaction(): void
61+
{
62+
/**
63+
* @var Buyer $buyer
64+
*/
65+
$buyer = factory(Buyer::class)->create();
66+
$this->assertEquals($buyer->balance, 0);
67+
$deposit = $buyer->deposit(1);
68+
69+
$this->assertIsInt($deposit->getKey());
70+
$this->assertEquals($deposit->getCasts()['id'], 'int');
71+
72+
config(['wallet.transaction.casts.id' => 'string']);
73+
$this->assertIsString($deposit->getKey());
74+
$this->assertEquals($deposit->getCasts()['id'], 'string');
75+
}
76+
77+
}

0 commit comments

Comments
 (0)