Skip to content

Commit

Permalink
feat: Add test for custom balance model and interface implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
contactinquid committed Jul 12, 2024
1 parent 200cf14 commit d7a8374
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 48 deletions.
3 changes: 0 additions & 3 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
<?php

it('will not use debugging functions')
->expect(['dd', 'dump', 'ray'])
->each->not->toBeUsed();
69 changes: 69 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
<?php

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;

it('can test', function () {
expect(true)->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();
}
}
45 changes: 0 additions & 45 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}
23 changes: 23 additions & 0 deletions tests/create_test_user_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
};

0 comments on commit d7a8374

Please sign in to comment.