Skip to content

Commit

Permalink
Add TransactionEventModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
r-kujawa committed May 16, 2024
1 parent 38f9a34 commit 9f87a35
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 11 deletions.
21 changes: 11 additions & 10 deletions database/factories/TransactionEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Payavel\Checkout\CheckoutStatus;
use Payavel\Checkout\Models\Dispute;
use Payavel\Checkout\Models\Refund;
use Payavel\Orchestration\Support\ServiceConfig;

class TransactionEventFactory extends Factory
{
Expand Down Expand Up @@ -37,26 +38,26 @@ public function definition()
*/
public function configure()
{
$this->model = ServiceConfig::get('checkout', 'models.' . $this->model, $this->model);

return $this->afterMaking(function (TransactionEvent $transactionEvent) {
if (is_null($transactionEvent->payment_id)) {
$transactionEvent->payment_id = Payment::factory()->create()->id;
}

if (is_null($transactionEvent->transactionable_id)) {
$transactionEvent->transactionable_id = $transactionEvent->payment_id;
$transactionEvent->transactionable_type = Payment::class;
}

if (is_null($transactionEvent->amount)) {
$transactionEvent->amount = $transactionEvent->transactionable->amount;
$transactionEvent->amount = $transactionEvent->transactionable->amount ?? $transactionEvent->payment->amount;
}

if (is_null($transactionEvent->status_code)) {
$transactionEvent->status_code = [
Payment::class => CheckoutStatus::AUTHORIZED,
Refund::class => CheckoutStatus::REFUNDED,
Dispute::class => CheckoutStatus::CHARGEBACK,
][$transactionEvent->transactionable_type];
ServiceConfig::get('checkout', 'models.' . Payment::class, Payment::class) => CheckoutStatus::AUTHORIZED,
ServiceConfig::get('checkout', 'models.' . Refund::class, Refund::class) => CheckoutStatus::REFUNDED,
ServiceConfig::get('checkout', 'models.' . Dispute::class, Dispute::class) => CheckoutStatus::CHARGEBACK,
][
ServiceConfig::get('checkout', 'models.' . $transactionEvent->transactionable_type, $transactionEvent->transactionable_type) ??
ServiceConfig::get('checkout', 'models.' . Payment::class, Payment::class)
];
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function up()
Schema::create('transaction_events', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('payment_id');
$table->morphs('transactionable');
$table->nullableMorphs('transactionable');
$table->string('reference');
$table->unsignedInteger('status_code');
$table->unsignedInteger('amount');
Expand Down
11 changes: 11 additions & 0 deletions src/CheckoutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Payavel\Checkout;

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\ServiceProvider;
use Payavel\Checkout\Console\Commands\CheckoutInstall;
use Payavel\Checkout\Console\Commands\CheckoutProvider;
use Payavel\Checkout\Models\Dispute;
use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Models\Refund;
use Payavel\Orchestration\Support\ServiceConfig;

class CheckoutServiceProvider extends ServiceProvider
{
Expand All @@ -19,6 +24,12 @@ public function boot()
$this->registerCommands();

$this->registerMigrations();

Relation::morphMap([
Payment::class => fn () => ServiceConfig::get('checkout', 'models.' . Payment::class, Payment::class),
Refund::class => fn () => ServiceConfig::get('checkout', 'models.' . Refund::class, Refund::class),
Dispute::class => fn () => ServiceConfig::get('checkout', 'models.' . Dispute::class, Dispute::class),
]);
}

public function register()
Expand Down
22 changes: 22 additions & 0 deletions tests/Models/TestDispute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Payavel\Checkout\Tests\Models;

use Payavel\Checkout\Models\Dispute;

class TestDispute extends Dispute
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'disputes';

/**
* Check if model is overridden for testing purposes.
*
* @var bool
*/
public bool $overridden = true;
}
23 changes: 23 additions & 0 deletions tests/Models/TestRefund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Payavel\Checkout\Tests\Models;

use Payavel\Checkout\Models\Refund;
use PHPUnit\Framework\Attributes\Test;

class TestRefund extends Refund
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'refunds';

/**
* Check if model is overridden for testing purposes.
*
* @var bool
*/
public bool $overridden = true;
}
50 changes: 50 additions & 0 deletions tests/Unit/TransactionEventModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Payavel\Checkout\Tests\Unit;

use Payavel\Checkout\Models\Dispute;
use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Models\Refund;
use Payavel\Checkout\Models\TransactionEvent;
use Payavel\Checkout\Tests\Models\TestDispute;
use Payavel\Checkout\Tests\Models\TestPayment;
use Payavel\Checkout\Tests\Models\TestRefund;
use Payavel\Checkout\Tests\TestCase;
use Payavel\Orchestration\Support\ServiceConfig;
use PHPUnit\Framework\Attributes\Test;

class TransactionEventModelTest extends TestCase
{
#[Test]
public function retrieve_transaction_event_payment()
{
$transactionEventWithPayment = TransactionEvent::factory()->create();
$this->assertInstanceOf(Payment::class, $transactionEventWithPayment->payment);

ServiceConfig::set('checkout', 'models.' . Payment::class, TestPayment::class);
$transactionEventWithOverriddenPayment = TransactionEvent::factory()->create();
$this->assertInstanceOf(TestPayment::class, $transactionEventWithOverriddenPayment->payment);
}

#[Test]
public function retrieve_transaction_event_transactionable()
{
$transactionEvent = TransactionEvent::factory()->create();
$this->assertNull($transactionEvent->transactionable);

$transactionables = [
Payment::class => TestPayment::class,
Refund::class => TestRefund::class,
Dispute::class => TestDispute::class,
];

$randomTransactionable = $this->faker->randomElement(array_keys($transactionables));
$transactionEventWithTransactionable = TransactionEvent::factory()->for($randomTransactionable::factory(), 'transactionable')->create();
$this->assertInstanceOf($randomTransactionable, $transactionEventWithTransactionable->transactionable);

$randomTransactionable = $this->faker->randomElement(array_keys($transactionables));
ServiceConfig::set('checkout', 'models.' . $randomTransactionable, $transactionables[$randomTransactionable]);
$transactionEventWithOverriddenTransactionable = TransactionEvent::factory()->for($transactionables[$randomTransactionable]::factory(), 'transactionable')->create();
$this->assertInstanceOf($transactionables[$randomTransactionable], $transactionEventWithOverriddenTransactionable->transactionable);
}
}

0 comments on commit 9f87a35

Please sign in to comment.