Skip to content

Commit

Permalink
Reformat PaymentModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
r-kujawa committed Jun 5, 2024
1 parent 05fbe32 commit f82ecdd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
4 changes: 2 additions & 2 deletions database/factories/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function makeSureServiceablesExistInDatabase(Payment $payment)
{
if (is_null($payment->provider_id)) {
$provider = ! is_null($payment->instrument_id)
? $payment->instrument->provider
? $payment->instrument->wallet->provider
: Provider::whereHas(
'accounts',
fn ($query) => $query->where('accounts.id', $payment->account_id)
Expand All @@ -81,7 +81,7 @@ protected function makeSureServiceablesExistInDatabase(Payment $payment)

if (is_null($payment->account_id)) {
$account = ! is_null($payment->instrument_id)
? $payment->instrument->account
? $payment->instrument->wallet->account
: Account::whereHas(
'providers',
fn ($query) => $query->where('providers.id', $payment->provider_id)
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Config/PaymentModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Payavel\Checkout\Tests\Unit\Config;

use Payavel\Checkout\Tests\Unit\TestPaymentModel;
use Payavel\Orchestration\Tests\Traits\CreatesConfigServiceables;
use Payavel\Orchestration\Tests\Traits\SetsConfigDriver;

class PaymentModelTest extends TestPaymentModel
{
use CreatesConfigServiceables;
use SetsConfigDriver;
}
42 changes: 42 additions & 0 deletions tests/Unit/Database/PaymentModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Payavel\Checkout\Tests\Unit\Database;

use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Tests\Models\TestAccount;
use Payavel\Checkout\Tests\Models\TestProvider;
use Payavel\Checkout\Tests\Unit\TestPaymentModel;
use Payavel\Orchestration\Models\Account;
use Payavel\Orchestration\Models\Provider;
use Payavel\Orchestration\Support\ServiceConfig;
use Payavel\Orchestration\Tests\Traits\CreatesDatabaseServiceables;
use Payavel\Orchestration\Tests\Traits\SetsDatabaseDriver;
use PHPUnit\Framework\Attributes\Test;

class PaymentModelTest extends TestPaymentModel
{
use CreatesDatabaseServiceables;
use SetsDatabaseDriver;

#[Test]
public function retrieve_payment_provider()
{
$paymentWithProvider = Payment::factory()->create();
$this->assertInstanceOf(Provider::class, $paymentWithProvider->provider);

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

#[Test]
public function retrieve_payment_account()
{
$paymentWithAccount = Payment::factory()->create();
$this->assertInstanceOf(Account::class, $paymentWithAccount->account);

ServiceConfig::set('checkout', 'models.' . Account::class, TestAccount::class);
$paymentWithOverriddenAccount = Payment::factory()->create();
$this->assertInstanceOf(TestAccount::class, $paymentWithOverriddenAccount->account);
}
}
47 changes: 24 additions & 23 deletions tests/Unit/PaymentModelTest.php → tests/Unit/TestPaymentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,16 @@
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;

class PaymentModelTest extends TestCase
abstract class TestPaymentModel extends TestCase implements CreatesServiceables
{
#[Test]
public function retrieve_payment_provider()
{
$paymentWithProvider = Payment::factory()->create();
$this->assertInstanceOf(Provider::class, $paymentWithProvider->provider);

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

#[Test]
public function retrieve_payment_account()
{
$paymentWithAccount = Payment::factory()->create();
$this->assertInstanceOf(Account::class, $paymentWithAccount->account);

ServiceConfig::set('checkout', 'models.' . Account::class, TestAccount::class);
$paymentWithOverriddenAccount = Payment::factory()->create();
$this->assertInstanceOf(TestAccount::class, $paymentWithOverriddenAccount->account);
}

#[Test]
public function retrieve_payment_rail()
{
Expand Down Expand Up @@ -98,4 +79,24 @@ public function retrieve_payment_transaction_events()
$this->assertCount(3, $paymentWith3OverriddenTransactionEvents->transactionEvents);
$this->assertContainsOnlyInstancesOf(TestTransactionEvent::class, $paymentWith3OverriddenTransactionEvents->transactionEvents);
}

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

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

0 comments on commit f82ecdd

Please sign in to comment.