Skip to content

Commit

Permalink
Fix PaymentModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
r-kujawa committed Jun 7, 2024
1 parent 6812cc8 commit e2566b6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion database/factories/TransactionEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function configure()

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

if (is_null($transactionEvent->amount)) {
Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/Database/PaymentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ class PaymentModelTest extends TestPaymentModel
#[Test]
public function retrieve_payment_provider()
{
$paymentWithProvider = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$paymentWithProvider = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(Provider::class, $paymentWithProvider->provider);

ServiceConfig::set('checkout', 'models.' . Provider::class, TestProvider::class);
$paymentWithOverriddenProvider = Payment::factory()->create();
$paymentWithOverriddenProvider = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(TestProvider::class, $paymentWithOverriddenProvider->provider);
}

#[Test]
public function retrieve_payment_account()
{
$paymentWithAccount = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$paymentWithAccount = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(Account::class, $paymentWithAccount->account);

ServiceConfig::set('checkout', 'models.' . Account::class, TestAccount::class);
$paymentWithOverriddenAccount = Payment::factory()->create();
$paymentWithOverriddenAccount = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(TestAccount::class, $paymentWithOverriddenAccount->account);
}
}
59 changes: 40 additions & 19 deletions tests/Unit/TestPaymentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
use Payavel\Checkout\Models\PaymentInstrument;
use Payavel\Checkout\Models\PaymentRail;
use Payavel\Checkout\Models\TransactionEvent;
use Payavel\Checkout\Tests\Models\TestAccount;
use Payavel\Checkout\Models\Wallet;
use Payavel\Checkout\Tests\Models\TestPaymentInstrument;
use Payavel\Checkout\Tests\Models\TestPaymentRail;
use Payavel\Checkout\Tests\Models\TestProvider;
use Payavel\Checkout\Tests\Models\TestTransactionEvent;
use Payavel\Checkout\Tests\TestCase;
use Payavel\Orchestration\Contracts\Accountable;
use Payavel\Orchestration\Contracts\Providable;
use Payavel\Orchestration\Models\Account;
use Payavel\Orchestration\Models\Provider;
use Payavel\Orchestration\Support\ServiceConfig;
use Payavel\Orchestration\Tests\Contracts\CreatesServiceables;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -26,77 +23,101 @@ abstract class TestPaymentModel extends TestCase implements CreatesServiceables
#[Test]
public function retrieve_payment_rail()
{
$paymentWithRail = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$paymentWithRail = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(PaymentRail::class, $paymentWithRail->rail);

ServiceConfig::set('checkout', 'models.' . PaymentRail::class, TestPaymentRail::class);
$paymentWithOverriddenRail = Payment::factory()->create();
$paymentWithOverriddenRail = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(TestPaymentRail::class, $paymentWithOverriddenRail->rail);
}

#[Test]
public function retrieve_payment_instrument()
{
$payment = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$payment = Payment::factory()->create($usingServiceables);
$this->assertNull($payment->instrument);

$paymentWithInstrument = Payment::factory()->forInstrument()->create();
$paymentWithInstrument = Payment::factory()->for(PaymentInstrument::factory()->for(Wallet::factory()->create($usingServiceables))->create(), 'instrument')->create($usingServiceables);
$this->assertInstanceOf(PaymentInstrument::class, $paymentWithInstrument->instrument);

ServiceConfig::set('checkout', 'models.' . PaymentInstrument::class, TestPaymentInstrument::class);
$paymentWithOverriddenInstrument = Payment::factory()->forInstrument()->create();
$paymentWithOverriddenInstrument = Payment::factory()->for(PaymentInstrument::factory()->for(Wallet::factory()->create($usingServiceables))->create(), 'instrument')->create($usingServiceables);
$this->assertInstanceOf(TestPaymentInstrument::class, $paymentWithOverriddenInstrument->instrument);
}

#[Test]
public function retrieve_payment_events()
{
$payment = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$payment = Payment::factory()->create($usingServiceables);
$this->assertEmpty($payment->events);

$paymentWith2Events = Payment::factory()->hasEvents(2, ['status_code' => CheckoutStatus::AUTHORIZED])->create();
$paymentWith2Events = Payment::factory()->hasEvents(2, ['status_code' => CheckoutStatus::AUTHORIZED])->create($usingServiceables);
$this->assertCount(2, $paymentWith2Events->events);
$this->assertContainsOnlyInstancesOf(TransactionEvent::class, $paymentWith2Events->events);

ServiceConfig::set('checkout', 'models.' . TransactionEvent::class, TestTransactionEvent::class);
$paymentWith3OverriddenEvents = Payment::factory()->hasEvents(3, ['status_code' => CheckoutStatus::AUTHORIZED])->create();
$paymentWith3OverriddenEvents = Payment::factory()->hasEvents(3, ['status_code' => CheckoutStatus::AUTHORIZED])->create($usingServiceables);
$this->assertCount(3, $paymentWith3OverriddenEvents->events);
$this->assertContainsOnlyInstancesOf(TestTransactionEvent::class, $paymentWith3OverriddenEvents->events);
}

#[Test]
public function retrieve_payment_transaction_events()
{
$payment = Payment::factory()->create();
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
];

$payment = Payment::factory()->create($usingServiceables);
$this->assertEmpty($payment->transactionEvents);

$paymentWith2TransactionEvents = Payment::factory()->hasTransactionEvents(2)->create();
$paymentWith2TransactionEvents = Payment::factory()->hasTransactionEvents(2)->create($usingServiceables);
$this->assertCount(2, $paymentWith2TransactionEvents->transactionEvents);
$this->assertContainsOnlyInstancesOf(TransactionEvent::class, $paymentWith2TransactionEvents->transactionEvents);

ServiceConfig::set('checkout', 'models.' . TransactionEvent::class, TestTransactionEvent::class);
$paymentWith3OverriddenTransactionEvents = Payment::factory()->hasTransactionEvents(3)->create();
$paymentWith3OverriddenTransactionEvents = Payment::factory()->hasTransactionEvents(3)->create($usingServiceables);
$this->assertCount(3, $paymentWith3OverriddenTransactionEvents->transactionEvents);
$this->assertContainsOnlyInstancesOf(TestTransactionEvent::class, $paymentWith3OverriddenTransactionEvents->transactionEvents);
}

#[Test]
public function retrieve_payment_providable()
{
$payment = Payment::factory()->create([
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
]);
];

$payment = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(Providable::class, $payment->getProvider());
}

#[Test]
public function retrieve_payment_accountable()
{
$payment = Payment::factory()->create([
$usingServiceables = [
'provider_id' => $this->createProvider($this->checkoutService)->getId(),
'account_id' => $this->createAccount($this->checkoutService)->getId(),
]);
];

$payment = Payment::factory()->create($usingServiceables);
$this->assertInstanceOf(Accountable::class, $payment->getAccount());
}
}

0 comments on commit e2566b6

Please sign in to comment.