From b32fd737369d62cc640a08e6bb288563c932908e Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Thu, 11 Jul 2024 22:39:42 -0600 Subject: [PATCH 1/7] Adding new configuration --- config/balance.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/config/balance.php b/config/balance.php index a06c470..c86f88e 100644 --- a/config/balance.php +++ b/config/balance.php @@ -1,9 +1,17 @@ 'balances' + 'table' => 'balances', + + /** + * Default model to use. + * Change this if you have extended the model. + */ + 'model' => Balance::class, ]; From 0d8c6e1b2502d6caf13bfec35f715bd6d288e08c Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Thu, 11 Jul 2024 22:39:53 -0600 Subject: [PATCH 2/7] Adding new interface --- src/Concerns/BalanceInterface.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/Concerns/BalanceInterface.php diff --git a/src/Concerns/BalanceInterface.php b/src/Concerns/BalanceInterface.php new file mode 100644 index 0000000..654de07 --- /dev/null +++ b/src/Concerns/BalanceInterface.php @@ -0,0 +1,12 @@ + Date: Thu, 11 Jul 2024 22:40:39 -0600 Subject: [PATCH 3/7] Default model implements from new interface --- src/Models/Balance.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Models/Balance.php b/src/Models/Balance.php index 914a4e9..02d7714 100644 --- a/src/Models/Balance.php +++ b/src/Models/Balance.php @@ -2,12 +2,13 @@ namespace Geow\Balance\Models; +use Geow\Balance\Concerns\BalanceInterface; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Support\Number; -class Balance extends Model +class Balance extends Model implements BalanceInterface { protected $guarded = []; From 5c7abd526787ab5f48c4c02b4db46d48339c7040 Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Thu, 11 Jul 2024 22:41:22 -0600 Subject: [PATCH 4/7] Trait to use inteface --- src/Traits/HasBalance.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Traits/HasBalance.php b/src/Traits/HasBalance.php index 7369e01..251ebab 100644 --- a/src/Traits/HasBalance.php +++ b/src/Traits/HasBalance.php @@ -2,8 +2,9 @@ namespace Geow\Balance\Traits; +use Geow\Balance\Concerns\BalanceInterface; use Illuminate\Database\Eloquent\Casts\Attribute; -use Geow\Balance\Models\Balance; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Support\Number; @@ -13,7 +14,7 @@ trait HasBalance public function credits(): MorphMany { - return $this->morphMany(Balance::class, 'balanceable'); + return $this->morphMany(config('balance.model'), 'balanceable'); } public function withCurrency(string $currency): self @@ -37,17 +38,17 @@ protected function creditCurrency(): Attribute ); } - public function increaseCredit(int $amount, ?string $reason = null): Balance + public function increaseCredit(int $amount, ?string $reason = null): BalanceInterface { return $this->createCredit($amount, $reason); } - public function decreaseCredit(int $amount, ?string $reason = null): Balance + public function decreaseCredit(int $amount, ?string $reason = null): BalanceInterface { return $this->createCredit(-1 * abs($amount), $reason); } - public function setCredit(int $amount, ?string $reason = null): Balance + public function setCredit(int $amount, ?string $reason = null): BalanceInterface { return $this->createCredit($amount, $reason); } @@ -62,7 +63,7 @@ public function hasCredit(): bool return $this->credit > 0; } - protected function createCredit(int $amount, ?string $reason = null): Balance + protected function createCredit(int $amount, ?string $reason = null): Model|BalanceInterface { return $this->credits()->create([ 'amount' => $amount, From 200cf14458828fcaad7467d9e99c7b58bd24538e Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Thu, 11 Jul 2024 22:41:44 -0600 Subject: [PATCH 5/7] Adding test to cover new functionality --- tests/TestCase.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index afb7550..21c26b3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,12 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; use Geow\Balance\BalanceServiceProvider; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Geow\Balance\Concerns\BalanceInterface; +use Geow\Balance\Traits\HasBalance; +use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Support\Number; class TestCase extends Orchestra { @@ -33,4 +39,43 @@ public function getEnvironmentSetUp($app) $migration->up(); */ } + + // Add a test that ensures I can use any model configured in the config('balance.model') and implements the BalanceInterface + public function testIcanUseAnyModelConfiguredInTheConfigBalanceModelAndImplementsTheBalanceInterface() + { + config()->set('balance.model', CustomBalance::class); + $model = config('balance.model'); + + $user = new User(); + $user->setCredit(2000); + $this->assertEquals(2000, $user->credit); + $user->increaseCredit(1000); + $this->assertEquals(3000, $user->credit); + $user->decreaseCredit(500); + $this->assertEquals(2500, $user->credit); + + $this->assertEquals('$25.00', $user->creditCurrency); + } +} + +// Create a model that uses the HasBalance trait and test that it can increase, decrease, set and reset the balance +class User extends Model +{ + use HasBalance; +} + + +class CustomBalance extends Model implements BalanceInterface +{ + protected function amountCurrency(): Attribute + { + return Attribute::make( + get: fn () => Number::currency($this->amount / 100, 'MXN'), + ); + } + + public function balanceable(): MorphTo + { + return $this->morphTo(); + } } From d7a8374f3e68888560ee0d4d37d87962012b9b98 Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Thu, 11 Jul 2024 23:36:59 -0600 Subject: [PATCH 6/7] feat: Add test for custom balance model and interface implementation --- tests/ArchTest.php | 3 -- tests/ExampleTest.php | 69 +++++++++++++++++++++++++++ tests/TestCase.php | 45 ----------------- tests/create_test_user_table.php.stub | 23 +++++++++ 4 files changed, 92 insertions(+), 48 deletions(-) create mode 100644 tests/create_test_user_table.php.stub diff --git a/tests/ArchTest.php b/tests/ArchTest.php index ccc19b2..a4abe2d 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -1,5 +1,2 @@ expect(['dd', 'dump', 'ray']) - ->each->not->toBeUsed(); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 5d36321..10425a1 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,5 +1,74 @@ toBeTrue(); }); + +it ('can use any model configured in the config balance.model and implements the BalanceInterface', function () { + config()->set('database.default', 'testing'); + + // Run the test user migration + $migration = include __DIR__.'/create_test_user_table.php.stub'; + $migration->up(); + + // Run the balance migration + $migration = include __DIR__.'/../database/migrations/create_balances_table.php.stub'; + $migration->up(); + + config()->set('balance.model', CustomBalance::class); + + $user = User::create([ + 'name' => 'John Doe', + 'email' => 'john@doe.com', + ]); + $user->setCredit(2000); + $this->assertEquals(2000, $user->credit); + $user->increaseCredit(1000); + $this->assertEquals(3000, $user->credit); + $user->decreaseCredit(500); + $this->assertEquals(2500, $user->credit); + + $this->assertEquals('Amount in custom format: 25', $user->creditCurrency); +}); + + +// Create a model that uses the HasBalance trait and test that it can increase, decrease, set and reset the balance +class User extends Model +{ + use HasBalance; + + protected $fillable = ['name', 'email']; +} + + +class CustomBalance extends Model implements BalanceInterface +{ + protected $guarded = []; + + public function __construct(array $attributes = []) + { + parent::__construct($attributes); + + $this->guarded[] = $this->primaryKey; + $this->table = config('balance.table'); + } + + protected function amountCurrency(): Attribute + { + $amount = $this->amount / 100; + return Attribute::make( + get: fn () => "Amount in custom format: $amount", + ); + } + + public function balanceable(): MorphTo + { + return $this->morphTo(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 21c26b3..afb7550 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,12 +5,6 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; use Geow\Balance\BalanceServiceProvider; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\MorphTo; -use Geow\Balance\Concerns\BalanceInterface; -use Geow\Balance\Traits\HasBalance; -use Illuminate\Database\Eloquent\Casts\Attribute; -use Illuminate\Support\Number; class TestCase extends Orchestra { @@ -39,43 +33,4 @@ public function getEnvironmentSetUp($app) $migration->up(); */ } - - // Add a test that ensures I can use any model configured in the config('balance.model') and implements the BalanceInterface - public function testIcanUseAnyModelConfiguredInTheConfigBalanceModelAndImplementsTheBalanceInterface() - { - config()->set('balance.model', CustomBalance::class); - $model = config('balance.model'); - - $user = new User(); - $user->setCredit(2000); - $this->assertEquals(2000, $user->credit); - $user->increaseCredit(1000); - $this->assertEquals(3000, $user->credit); - $user->decreaseCredit(500); - $this->assertEquals(2500, $user->credit); - - $this->assertEquals('$25.00', $user->creditCurrency); - } -} - -// Create a model that uses the HasBalance trait and test that it can increase, decrease, set and reset the balance -class User extends Model -{ - use HasBalance; -} - - -class CustomBalance extends Model implements BalanceInterface -{ - protected function amountCurrency(): Attribute - { - return Attribute::make( - get: fn () => Number::currency($this->amount / 100, 'MXN'), - ); - } - - public function balanceable(): MorphTo - { - return $this->morphTo(); - } } diff --git a/tests/create_test_user_table.php.stub b/tests/create_test_user_table.php.stub new file mode 100644 index 0000000..27e6089 --- /dev/null +++ b/tests/create_test_user_table.php.stub @@ -0,0 +1,23 @@ +id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamps(); + }); + } +}; From 78a1eec1d18c4e95dc887bb351ec3b1669bd8f09 Mon Sep 17 00:00:00 2001 From: Luis Gonzalez Date: Fri, 12 Jul 2024 00:30:13 -0600 Subject: [PATCH 7/7] ArchTest and ExampleTest --- tests/ArchTest.php | 3 +++ tests/ExampleTest.php | 21 ++++++++++++++++----- tests/create_balances_custom_table.php.stub | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 tests/create_balances_custom_table.php.stub diff --git a/tests/ArchTest.php b/tests/ArchTest.php index a4abe2d..ccc19b2 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -1,2 +1,5 @@ expect(['dd', 'dump', 'ray']) + ->each->not->toBeUsed(); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 10425a1..f4fdff6 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -5,6 +5,7 @@ use Geow\Balance\Concerns\BalanceInterface; use Geow\Balance\Traits\HasBalance; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Support\Facades\Number; it('can test', function () { expect(true)->toBeTrue(); @@ -18,9 +19,10 @@ $migration->up(); // Run the balance migration - $migration = include __DIR__.'/../database/migrations/create_balances_table.php.stub'; + $migration = include __DIR__.'/create_balances_custom_table.php.stub'; $migration->up(); + config()->set('balance.table', 'balances_custom'); config()->set('balance.model', CustomBalance::class); $user = User::create([ @@ -34,7 +36,7 @@ $user->decreaseCredit(500); $this->assertEquals(2500, $user->credit); - $this->assertEquals('Amount in custom format: 25', $user->creditCurrency); + $this->assertEquals('$25.00', $user->creditCurrency); }); @@ -51,6 +53,16 @@ class CustomBalance extends Model implements BalanceInterface { protected $guarded = []; + // before saving the model ensure the custom column is the name of the model that originated the balance + protected static function boot() + { + parent::boot(); + + static::saving(function ($model) { + $model->custom = get_class($model->balanceable); + }); + } + public function __construct(array $attributes = []) { parent::__construct($attributes); @@ -58,12 +70,11 @@ public function __construct(array $attributes = []) $this->guarded[] = $this->primaryKey; $this->table = config('balance.table'); } - + protected function amountCurrency(): Attribute { - $amount = $this->amount / 100; return Attribute::make( - get: fn () => "Amount in custom format: $amount", + get: fn () => Number::currency($this->amount / 100), ); } diff --git a/tests/create_balances_custom_table.php.stub b/tests/create_balances_custom_table.php.stub new file mode 100644 index 0000000..3594495 --- /dev/null +++ b/tests/create_balances_custom_table.php.stub @@ -0,0 +1,20 @@ +id(); + $table->morphs('balanceable'); + $table->integer('amount'); + $table->text('reason')->nullable(); + $table->text('custom')->nullable(); + $table->timestamps(); + }); + } +};