diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..c92edfc --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,21 @@ +{ + "name": "Laradock", + "dockerComposeFile": ["../../laradock/docker-compose.yml"], + "service": "workspace", + "workspaceFolder": "/var/www/laravel-wallet", + "extensions": [ + "dotjoshjohnson.xml", + "ryu1kn.partial-diff", + "artdiniz.quitcontrol-vscode", + "emallin.phpunit", + "eamodio.gitlens", + "onecentlin.laravel-extension-pack", + "onecentlin.php-productive-pack", + "sdras.vue-vscode-extensionpack", + "fknop.vscode-npm", + "apility.beautify-blade" + ], + "remoteUser": "laradock", + "shutdownAction": "none", + "runServices": ["mysql", "redis", "nginx"] + } diff --git a/.gitattributes b/.gitattributes old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 diff --git a/LICENSE.md b/LICENSE.md old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 259716b..c51b393 --- a/composer.json +++ b/composer.json @@ -28,8 +28,7 @@ ], "require": { "php": "^7.3|^8.0", - "illuminate/support": "^7.0|^8.0", - "laravel/legacy-factories": "^1.1" + "illuminate/support": "^7.0|^8.0" }, "require-dev": { "doctrine/dbal": "^2.9", diff --git a/config/.gitkeep b/config/.gitkeep old mode 100644 new mode 100755 diff --git a/config/config.php b/config/config.php old mode 100644 new mode 100755 diff --git a/database/migrations/2018_09_13_123456_create_wallet_tables.php b/database/migrations/2018_09_13_123456_create_wallet_tables.php old mode 100644 new mode 100755 diff --git a/database/migrations/2018_11_05_123456_update_wallet_transactions_table.php b/database/migrations/2018_11_05_123456_update_wallet_transactions_table.php old mode 100644 new mode 100755 diff --git a/database/migrations/2019_01_25_000000_add_polymorphic_relation_to_transactions_table.php b/database/migrations/2019_01_25_000000_add_polymorphic_relation_to_transactions_table.php old mode 100644 new mode 100755 diff --git a/phpunit.xml b/phpunit.xml old mode 100644 new mode 100755 diff --git a/src/Exceptions/UnacceptedTransactionException.php b/src/Exceptions/UnacceptedTransactionException.php old mode 100644 new mode 100755 diff --git a/src/Facades/WalletFacade.php b/src/Facades/WalletFacade.php old mode 100644 new mode 100755 diff --git a/src/Jobs/RecalculateWalletBalance.php b/src/Jobs/RecalculateWalletBalance.php old mode 100644 new mode 100755 diff --git a/src/Models/Transaction.php b/src/Models/Transaction.php old mode 100644 new mode 100755 index 3751bf2..b12b2cf --- a/src/Models/Transaction.php +++ b/src/Models/Transaction.php @@ -2,6 +2,7 @@ namespace MannikJ\Laravel\Wallet\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Arr; @@ -9,6 +10,7 @@ class Transaction extends Model { use SoftDeletes; + use HasFactory; protected $table = 'wallet_transactions'; diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php old mode 100644 new mode 100755 index 0cf05fb..47ca933 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -2,6 +2,7 @@ namespace MannikJ\Laravel\Wallet\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; @@ -10,6 +11,7 @@ class Wallet extends Model { use SoftDeletes; + use HasFactory; protected $attributes = [ 'balance' => 0, diff --git a/src/Observers/TransactionObserver.php b/src/Observers/TransactionObserver.php old mode 100644 new mode 100755 diff --git a/src/Observers/WalletObserver.php b/src/Observers/WalletObserver.php old mode 100644 new mode 100755 diff --git a/src/Services/Wallet.php b/src/Services/Wallet.php old mode 100644 new mode 100755 diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php old mode 100644 new mode 100755 diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php old mode 100644 new mode 100755 diff --git a/tests/Factories/TransactionFactory.php b/tests/Factories/TransactionFactory.php new file mode 100755 index 0000000..ab6aeec --- /dev/null +++ b/tests/Factories/TransactionFactory.php @@ -0,0 +1,41 @@ + WalletFactory::new(), + 'type' => $this->faker->randomElement([ + 'deposit', + 'withdraw', + ]), + 'amount' => $this->faker->randomFloat(4, 0, 10000), + ]; + } + + public function withdraw() + { + return $this->state(function (array $attributes) { + return ['type' => 'withdraw']; + }); + } + + public function deposit() + { + return $this->state(function (array $attributes) { + return ['type' => 'deposit']; + }); + } +} diff --git a/tests/Factories/UserFactory.php b/tests/Factories/UserFactory.php new file mode 100644 index 0000000..f6b10e6 --- /dev/null +++ b/tests/Factories/UserFactory.php @@ -0,0 +1,23 @@ + $this->faker->name, + 'email' => $this->faker->unique()->safeEmail, + 'email_verified_at' => now(), + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'remember_token' => Str::random(10), + ]; + } +} diff --git a/tests/Factories/WalletFactory.php b/tests/Factories/WalletFactory.php new file mode 100755 index 0000000..aa096d9 --- /dev/null +++ b/tests/Factories/WalletFactory.php @@ -0,0 +1,21 @@ + UserFactory::new(), + 'owner_type' => User::class + ]; + } +} diff --git a/tests/Models/User.php b/tests/Models/User.php old mode 100644 new mode 100755 index a51e08c..f6a8b06 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -2,10 +2,12 @@ namespace MannikJ\Laravel\Wallet\Tests\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as AuthUser; use MannikJ\Laravel\Wallet\Traits\HasWallet; class User extends AuthUser { use HasWallet; + use HasFactory; } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php old mode 100644 new mode 100755 index d1295f5..4c4cb7c --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -14,7 +14,6 @@ class TestCase extends BaseTestCase protected function setUp(): void { parent::setUp(); - $this->withFactories(__DIR__ . '/factories'); $this->loadLaravelMigrations(); $this->loadMigrationsFrom('database/migrations'); } diff --git a/tests/factories/TransactionFactory.php b/tests/factories/TransactionFactory.php deleted file mode 100644 index 240963d..0000000 --- a/tests/factories/TransactionFactory.php +++ /dev/null @@ -1,47 +0,0 @@ -define(Transaction::class, function (Faker $faker, $attributes) { -// $wallet = array_has($attributes, 'wallet_id') -// ? Wallet::findOrFail($attributes['wallet_id']) -// : (Wallet::first() ? : factory(Wallet::class)->create()); -// $type = $faker->randomElement([ -// 'deposit', -// 'withdraw', -// ]); -// $negativator = in_array($type, config('wallet.subtracting_transaction_types', [])) ? -1 : 1; -// return [ -// 'wallet_id' => $wallet->id, -// 'type' => $type, -// 'amount' => $faker->randomFloat(4, 0, 10000) * $negativator, -// 'hash' => uniqid() -// ]; -// }); - - -$factory->define(config('wallet.transaction_model'), function (Faker $faker, $attributes) { - $walletModel = config('wallet.wallet_model'); - $wallet = Arr::has($attributes, 'wallet_id') - ? $walletModel::findOrFail($attributes['wallet_id']) - : ($walletModel::first() ? : factory($walletModel)->create()); - $type = $faker->randomElement([ - 'deposit', - 'withdraw', - ]); - return [ - 'wallet_id' => $wallet->id, - 'type' => $type, - 'amount' => $faker->randomFloat(4, 0, 10000), - ]; -}); - -$factory->state(config('wallet.transaction_model'), 'withdraw', function ($faker, $attributes) { - return ['type' => 'withdraw']; -}); - -$factory->state(config('wallet.transaction_model'), 'deposit', function ($faker, $attributes) { - return ['type' => 'deposit']; -}); \ No newline at end of file diff --git a/tests/factories/UserFactory.php b/tests/factories/UserFactory.php deleted file mode 100644 index 086e4b8..0000000 --- a/tests/factories/UserFactory.php +++ /dev/null @@ -1,14 +0,0 @@ -define(User::class, function (Faker $faker) { - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => bcrypt('test'), - 'remember_token' => Str::random(10), - ]; -}); \ No newline at end of file diff --git a/tests/factories/WalletFactory.php b/tests/factories/WalletFactory.php deleted file mode 100644 index 8faaa0f..0000000 --- a/tests/factories/WalletFactory.php +++ /dev/null @@ -1,16 +0,0 @@ -define(Wallet::class, function (Faker $faker, $attributes) { - $owner = Arr::has($attributes, 'owner_id') - ? User::findOrFail($attributes['owner_id']) - : (User::first() ? : factory(User::class)->create()); - return [ - 'owner_id' => $owner->id, - 'owner_type' => get_class($owner), - ]; -}); \ No newline at end of file diff --git a/tests/unit/HasWalletTest.php b/tests/unit/HasWalletTest.php old mode 100644 new mode 100755 index 1dd6b36..c56e969 --- a/tests/unit/HasWalletTest.php +++ b/tests/unit/HasWalletTest.php @@ -5,16 +5,18 @@ use MannikJ\Laravel\Wallet\Models\Wallet; use MannikJ\Laravel\Wallet\Exceptions\UnacceptedTransactionException; use MannikJ\Laravel\Wallet\Tests\TestCase; -use MannikJ\Laravel\Wallet\Tests\Models\User; use MannikJ\Laravel\Wallet\Models\Transaction; use Illuminate\Support\Collection; +use MannikJ\Laravel\Wallet\Tests\Factories\TransactionFactory; +use MannikJ\Laravel\Wallet\Tests\Factories\UserFactory; +use MannikJ\Laravel\Wallet\Tests\Factories\WalletFactory; class HasWalletTest extends TestCase { /** @test */ public function wallet() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertInstanceOf(Wallet::class, $user->wallet); $this->assertFalse($user->wallet->exists()); $this->assertTrue(0.0 === $user->wallet->balance); @@ -23,15 +25,15 @@ public function wallet() /** @test */ public function wallet_transactions() { - $user1 = factory(User::class)->create(); - $wallet1 = factory(Wallet::class)->create(['owner_id' => $user1->id]); - $transactions1 = factory(Transaction::class, 10)->create(['wallet_id' => $wallet1->id]); + $user1 = UserFactory::new()->create(); + $wallet1 = WalletFactory::new()->create(['owner_id' => $user1->id]); + $transactions1 = TransactionFactory::new()->count(10)->create(['wallet_id' => $wallet1->id]); $this->assertInstanceOf(Collection::class, $user1->walletTransactions); $this->assertEquals(10, $user1->walletTransactions()->count()); $this->assertEmpty($wallet1->transactions->diff($user1->walletTransactions)); - $user2 = factory(User::class)->create(); - $wallet2 = factory(Wallet::class)->create(['owner_id' => $user2->id]); - $transactions2 = factory(Transaction::class, 5)->create(['wallet_id' => $wallet2->id]); + $user2 = UserFactory::new()->create(); + $wallet2 = WalletFactory::new()->create(['owner_id' => $user2->id]); + $transactions2 = TransactionFactory::new()->count(5)->create(['wallet_id' => $wallet2->id]); $this->assertInstanceOf(Collection::class, $user1->walletTransactions); $this->assertEquals(10, $user1->walletTransactions()->count()); $this->assertEmpty($wallet2->transactions->diff($user2->walletTransactions)); diff --git a/tests/unit/RecalculateWalletBalanceTest.php b/tests/unit/RecalculateWalletBalanceTest.php old mode 100644 new mode 100755 index 4e8523a..a9c98ff --- a/tests/unit/RecalculateWalletBalanceTest.php +++ b/tests/unit/RecalculateWalletBalanceTest.php @@ -10,6 +10,7 @@ use Illuminate\Support\Collection; use MannikJ\Laravel\Wallet\Jobs\RecalculateWalletBalance; use MannikJ\Laravel\Wallet\DebouncedJob; +use MannikJ\Laravel\Wallet\Tests\Factories\WalletFactory; class RecalculateWalletBalanceTest extends TestCase { @@ -17,7 +18,7 @@ class RecalculateWalletBalanceTest extends TestCase public function dispatch() { config(['auto_recalculate_balance' => true]); - $wallet = factory(Wallet::class)->create(); + $wallet = WalletFactory::new()->create(); Transaction::flushEventListeners(); $transaction = $wallet->transactions()->make(['type' => 'deposit', 'amount' => 10]); $transaction->hash = uniqid(); diff --git a/tests/unit/TransactionTest.php b/tests/unit/TransactionTest.php old mode 100644 new mode 100755 index 47e34d6..0c7e693 --- a/tests/unit/TransactionTest.php +++ b/tests/unit/TransactionTest.php @@ -8,6 +8,8 @@ use MannikJ\Laravel\Wallet\Tests\Models\User; use MannikJ\Laravel\Wallet\Models\Transaction; use Illuminate\Support\Collection; +use MannikJ\Laravel\Wallet\Tests\Factories\TransactionFactory; +use MannikJ\Laravel\Wallet\Tests\Factories\WalletFactory; class TransactionTest extends TestCase { @@ -15,15 +17,15 @@ class TransactionTest extends TestCase /** @test */ public function wallet() { - $transaction = factory(Transaction::class)->create(); + $transaction = TransactionFactory::new()->create(); $this->assertInstanceOf(Wallet::class, $transaction->wallet); } /** @test */ public function origin() { - $origin = factory(Transaction::class)->create(); - $transaction = factory(Transaction::class)->create(); + $origin = TransactionFactory::new()->create(); + $transaction = TransactionFactory::new()->create(); $transaction->origin()->associate($origin); $transaction->save(); $transaction = $transaction->fresh(); @@ -34,8 +36,8 @@ public function origin() /** @test */ public function children() { - $origin = factory(Transaction::class)->create(); - $transaction = factory(Transaction::class)->create(); + $origin = TransactionFactory::new()->create(); + $transaction = TransactionFactory::new()->create(); $origin->children()->save($transaction); $this->assertInstanceOf(Collection::class, $transaction->children); $child = $origin->children()->where('id', $transaction->id)->first(); @@ -46,8 +48,7 @@ public function children() /** @test */ public function reference() { - - $transaction = factory(Transaction::class)->create(); + $transaction = TransactionFactory::new()->create(); $this->assertNull($transaction->reference); $transaction->reference()->associate($transaction->wallet); $this->assertTrue($transaction->wallet->is($transaction->reference)); @@ -56,7 +57,7 @@ public function reference() /** @test */ public function update() { - $transaction = factory(Transaction::class)->create(['amount' => 20, 'type' => 'deposit']); + $transaction = TransactionFactory::new()->create(['amount' => 20, 'type' => 'deposit']); $this->assertEquals(20, $transaction->wallet->balance); $transaction->update(['amount' => 100]); $this->assertEquals(100, $transaction->wallet->refresh()->balance); @@ -71,7 +72,7 @@ public function update() /** @test */ public function create_converts_amount_to_absolute_value() { - $wallet = factory(Wallet::class)->create(); + $wallet = WalletFactory::new()->create(); $transaction = $wallet->transactions()->create(['type' => 'withdraw', 'amount' => -20]); $this->assertEquals(20, $transaction->getAttributes()['amount']); } @@ -79,12 +80,12 @@ public function create_converts_amount_to_absolute_value() /** @test */ public function delete_model() { - $transaction = factory(Transaction::class)->create(['amount' => 20, 'type' => 'deposit']); + $transaction = TransactionFactory::new()->create(['amount' => 20, 'type' => 'deposit']); $this->assertEquals(20, $transaction->wallet->refresh()->balance); $transaction->delete(); $this->assertTrue($transaction->trashed()); $this->assertEquals(0, $transaction->wallet->refresh()->balance); - $transaction = factory(Transaction::class)->create(['amount' => 20, 'type' => 'withdraw']); + $transaction = TransactionFactory::new()->create(['amount' => 20, 'type' => 'withdraw']); $this->assertEquals(-20, $transaction->wallet->refresh()->balance); } @@ -92,7 +93,7 @@ public function delete_model() public function replace() { $timestamp = now()->subHours(1); - $transaction = factory(Transaction::class)->create([ + $transaction = TransactionFactory::new()->create([ 'amount' => 20, 'type' => 'deposit', 'created_at' => $timestamp, @@ -111,9 +112,9 @@ public function replace() /** @test */ public function generated_hash_is_set() { - $transaction = factory(Transaction::class)->create(); + $transaction = TransactionFactory::new()->create(); $this->assertNotNull($transaction->hash); - $transactions = factory(Transaction::class, 2)->create(); + $transactions = TransactionFactory::new()->count(2)->create(); $transactions->each(function ($transaction) { $this->assertNotNull($transaction->hash); }); @@ -122,22 +123,22 @@ public function generated_hash_is_set() /** @test */ public function get_total_amount() { - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 2; $this->assertEquals($price, $transaction->getTotalAmount()); $this->assertEquals($price, $transaction->getAttributes()['total_amount']); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = -2; $this->assertEquals($price, $transaction->getTotalAmount()); $this->assertEquals($price, $transaction->getAttributes()['total_amount']); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 8; $this->assertEquals(-$price, $transaction->getTotalAmount()); - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 8; $this->assertEquals($price, $transaction->getTotalAmount()); $children->first()->delete(); @@ -148,20 +149,20 @@ public function get_total_amount() /** @test */ public function scope_select_total_amount() { - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 2; $this->assertEquals($price, $transaction->where('id', $transaction->id)->selectTotalAmount()->first()->getAttributes()['total_amount']); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = -2; $this->assertEquals($price, $transaction->where('id', $transaction->id)->selectTotalAmount()->first()->getAttributes()['total_amount']); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 8; $this->assertEquals(-$price, $transaction->where('id', $transaction->id)->selectTotalAmount()->first()->getAttributes()['total_amount']); - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 8; $this->assertEquals($price, $transaction->where('id', $transaction->id)->selectTotalAmount()->first()->getAttributes()['total_amount']); $children->first()->delete(); @@ -172,24 +173,24 @@ public function scope_select_total_amount() /** @test */ public function get_total_amount_attribute() { - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 2; $this->assertEquals($price, $transaction->getTotalAmountAttribute()); $this->assertEquals($price, $transaction->total_amount); $this->assertEquals(4, Transaction::count()); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = -2; $this->assertEquals($price, $transaction->getTotalAmountAttribute()); $this->assertEquals($price, $transaction->total_amount); - $transaction = factory(Transaction::class)->states('withdraw')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('withdraw')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->withdraw()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->withdraw()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = -8; $this->assertEquals($price, $transaction->getTotalAmountAttribute()); $this->assertEquals($price, $transaction->total_amount); - $transaction = factory(Transaction::class)->states('deposit')->create(['amount' => '5']); - $children = factory(Transaction::class, 3)->states('deposit')->create(['amount' => '1', 'origin_id' => $transaction->id]); + $transaction = TransactionFactory::new()->deposit()->create(['amount' => '5']); + $children = TransactionFactory::new()->count(3)->deposit()->create(['amount' => '1', 'origin_id' => $transaction->id]); $price = 8; $this->assertEquals($price, $transaction->getTotalAmountAttribute()); $this->assertEquals($price, $transaction->total_amount); diff --git a/tests/unit/WalletTest.php b/tests/unit/WalletTest.php old mode 100644 new mode 100755 index 716f76a..f381578 --- a/tests/unit/WalletTest.php +++ b/tests/unit/WalletTest.php @@ -9,6 +9,9 @@ use MannikJ\Laravel\Wallet\Models\Transaction; use Illuminate\Support\Collection; use MannikJ\Laravel\Wallet\Jobs\RecalculateWalletBalance; +use MannikJ\Laravel\Wallet\Tests\Factories\TransactionFactory; +use MannikJ\Laravel\Wallet\Tests\Factories\UserFactory; +use MannikJ\Laravel\Wallet\Tests\Factories\WalletFactory; class WalletTest extends TestCase { @@ -16,14 +19,14 @@ class WalletTest extends TestCase /** @test */ public function owner() { - $wallet = factory(Wallet::class)->create(); + $wallet = WalletFactory::new()->create(); $this->assertInstanceOf(User::class, $wallet->owner); } /** @test */ public function delete_and_restore_wallet() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $transaction = $user->wallet->deposit(10); $wallet = $user->wallet; $this->assertEquals(1, $wallet->id); @@ -46,7 +49,7 @@ public function delete_and_restore_wallet() /** @test */ public function deposit() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists()); $transaction = $user->wallet->deposit(10); $this->assertTrue($user->wallet->exists()); @@ -68,7 +71,7 @@ public function deposit() /** @test */ public function deposit_negative_amount() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists); $transaction = $user->wallet->failDeposit(-30); $this->assertTrue($transaction->trashed()); @@ -80,7 +83,7 @@ public function deposit_negative_amount() /** @test */ public function fail_deposit() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists); $transaction = $user->wallet->failDeposit(10000); $this->assertTrue($transaction->trashed()); @@ -92,7 +95,7 @@ public function fail_deposit() /** @test */ public function force_withdraw() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists); $transaction = $user->wallet->forceWithdraw(10000); $this->assertTrue($transaction->exists); @@ -105,7 +108,7 @@ public function force_withdraw() /** @test */ public function can_withdraw() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists); $this->assertFalse($user->wallet->canWithdraw()); $this->assertFalse($user->wallet->canWithdraw(1)); @@ -122,7 +125,7 @@ public function can_withdraw() /** @test */ public function withdraw() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertFalse($user->wallet->exists); $this->expectException(UnacceptedTransactionException::class); $user->wallet->withdraw(10); @@ -137,7 +140,7 @@ public function withdraw() /** @test */ function set_balance() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); $this->assertEquals(0, $user->balance); $offsetTransaction = $user->wallet->setBalance(0); $this->assertEquals(null, $offsetTransaction); @@ -161,9 +164,19 @@ function set_balance() /** @test */ function actual_balance() { - $user = factory(User::class)->create(); - $transactions = factory(Transaction::class, 10)->create(['amount' => 50, 'type' => 'deposit']); - $transactions = factory(Transaction::class, 10)->create(['amount' => 25, 'type' => 'withdraw']);# + $wallet = WalletFactory::new()->create(); + $user = $wallet->owner; + $transactions = TransactionFactory::new()->count(10)->create([ + 'wallet_id' => $wallet->id, + 'amount' => 50, + 'type' => 'deposit' + ]); + $transactions = TransactionFactory::new()->count(10)->create([ + 'wallet_id' => $wallet->id, + 'amount' => 25, + 'type' => 'withdraw' + ]); + // dd($transactions->first()); $expectedBalance = 10 * 50 - 10 * 25; $this->assertEquals($expectedBalance, $user->balance); $this->assertEquals($expectedBalance, $user->wallet->balance); @@ -174,7 +187,7 @@ function actual_balance() /** @test */ function balance_change_doesnt_trigger_recalculation() { - $wallet = factory(Wallet::class)->create(); + $wallet = WalletFactory::new()->create(); $this->doesntExpectJobs(RecalculateWalletBalance::class); $wallet->balance = 10; $wallet->save(); @@ -183,23 +196,23 @@ function balance_change_doesnt_trigger_recalculation() /** @test */ function balance_change_triggers_recalculation_if_activated() { - $wallet = factory(Wallet::class)->create(); + $wallet = WalletFactory::new()->create(); config(['auto_recalculate_balance' => true]); $this->expectsJobs(RecalculateWalletBalance::class); $wallet->balance = -10; $wallet->save(); - } /** @test */ function recalculation_performance() { - $user = factory(User::class)->create(); + $user = UserFactory::new()->create(); Transaction::flushEventListeners(); $numbers = [1, 10, 100, 1000, 10000]; $result = collect($numbers)->mapWithKeys(function ($number) use ($user) { $alreadyExist = Transaction::count(); - $transactions = factory(Transaction::class, $number - $alreadyExist)->create(['hash' => uniqid()]); + $transactions = TransactionFactory::new()->count($number - $alreadyExist) + ->create(['hash' => uniqid()]); $start = microtime(true); $actualBalance = $user->wallet->actualBalance(); return [$number => microtime(true) - $start]; @@ -207,5 +220,4 @@ function recalculation_performance() $this->assertTrue(true); \Log::info($result); } - -} \ No newline at end of file +}