Skip to content

Commit

Permalink
Merge branch 'master' of github.com:MannikJ/laravel-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
MannikJ committed Aug 6, 2021
2 parents eed874c + 8f8363a commit de0499f
Show file tree
Hide file tree
Showing 35 changed files with 195 additions and 146 deletions.
21 changes: 21 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"]
}
Empty file modified .gitattributes
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified LICENSE.md
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
3 changes: 1 addition & 2 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Empty file modified config/.gitkeep
100644 → 100755
Empty file.
Empty file modified config/config.php
100644 → 100755
Empty file.
Empty file modified database/migrations/2018_09_13_123456_create_wallet_tables.php
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file modified phpunit.xml
100644 → 100755
Empty file.
Empty file modified src/Exceptions/UnacceptedTransactionException.php
100644 → 100755
Empty file.
Empty file modified src/Facades/WalletFacade.php
100644 → 100755
Empty file.
Empty file modified src/Jobs/RecalculateWalletBalance.php
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions src/Models/Transaction.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;

class Transaction extends Model
{
use SoftDeletes;
use HasFactory;

protected $table = 'wallet_transactions';

Expand Down
2 changes: 2 additions & 0 deletions src/Models/Wallet.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +11,7 @@
class Wallet extends Model
{
use SoftDeletes;
use HasFactory;

protected $attributes = [
'balance' => 0,
Expand Down
Empty file modified src/Observers/TransactionObserver.php
100644 → 100755
Empty file.
Empty file modified src/Observers/WalletObserver.php
100644 → 100755
Empty file.
Empty file modified src/Services/Wallet.php
100644 → 100755
Empty file.
Empty file modified src/Traits/HasWallet.php
100644 → 100755
Empty file.
Empty file modified src/WalletServiceProvider.php
100644 → 100755
Empty file.
41 changes: 41 additions & 0 deletions tests/Factories/TransactionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace MannikJ\Laravel\Wallet\Tests\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;
use MannikJ\Laravel\Wallet\Models\Transaction;
use MannikJ\Laravel\Wallet\Models\Wallet;

class TransactionFactory extends Factory
{

protected $model = Transaction::class;


public function definition()
{
return [
'wallet_id' => 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'];
});
}
}
23 changes: 23 additions & 0 deletions tests/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MannikJ\Laravel\Wallet\Tests\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use MannikJ\Laravel\Wallet\Tests\Models\User;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
protected $model = User::class;

public function definition()
{
return [
'name' => $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),
];
}
}
21 changes: 21 additions & 0 deletions tests/Factories/WalletFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MannikJ\Laravel\Wallet\Tests\Factories;

use MannikJ\Laravel\Wallet\Models\Wallet;
use MannikJ\Laravel\Wallet\Tests\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class WalletFactory extends Factory
{

protected $model = Wallet::class;

public function definition()
{
return [
'owner_id' => UserFactory::new(),
'owner_type' => User::class
];
}
}
2 changes: 2 additions & 0 deletions tests/Models/User.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion tests/TestCase.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TestCase extends BaseTestCase
protected function setUp(): void
{
parent::setUp();
$this->withFactories(__DIR__ . '/factories');
$this->loadLaravelMigrations();
$this->loadMigrationsFrom('database/migrations');
}
Expand Down
47 changes: 0 additions & 47 deletions tests/factories/TransactionFactory.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/factories/UserFactory.php

This file was deleted.

16 changes: 0 additions & 16 deletions tests/factories/WalletFactory.php

This file was deleted.

18 changes: 10 additions & 8 deletions tests/unit/HasWalletTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/RecalculateWalletBalanceTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
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
{
/** @test */
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();
Expand Down
Loading

0 comments on commit de0499f

Please sign in to comment.