Skip to content

Commit

Permalink
Add PaymentRailTest
Browse files Browse the repository at this point in the history
  • Loading branch information
r-kujawa committed May 8, 2024
1 parent f7724b3 commit 513e468
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/Models/TestPayment.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\Payment;

class TestPayment extends Payment
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'payments';

/**
* Check if model is overridden for testing purposes.
*
* @var bool
*/
public bool $overridden = true;
}
22 changes: 22 additions & 0 deletions tests/Models/TestPaymentType.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\PaymentType;

class TestPaymentType extends PaymentType
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'payment_types';

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

namespace Payavel\Checkout\Tests\Unit\Models;

use Payavel\Checkout\Models\Payment;
use Payavel\Checkout\Models\PaymentRail;
use Payavel\Checkout\Models\PaymentType;
use Payavel\Checkout\Tests\Models\TestPayment;
use Payavel\Checkout\Tests\Models\TestPaymentType;
use Payavel\Checkout\Tests\TestCase;
use Payavel\Orchestration\Support\ServiceConfig;
use PHPUnit\Framework\Attributes\Test;

class PaymentRailTest extends TestCase
{
#[Test]
public function payment_rail_generates_id_before_committing()
{
$parentPaymentType = PaymentType::factory()->create();
$paymentType = PaymentType::factory()->create();

$paymentRail = PaymentRail::create([
'parent_type_id' => $parentPaymentType->id,
'type_id' => $paymentType->id,
]);

$this->assertEquals("{$parentPaymentType->id}:{$paymentType->id}", $paymentRail->id);
}

#[Test]
public function retrieve_payment_rail_parent_type()
{
$paymentRail = PaymentRail::factory()->create();
$this->assertInstanceOf(PaymentType::class, $paymentRail->parentType);

ServiceConfig::set('checkout', 'models.' . PaymentType::class, TestPaymentType::class);
$paymentRailWithOverriddenParentType = PaymentRail::factory()->create();
$this->assertInstanceOf(TestPaymentType::class, $paymentRailWithOverriddenParentType->parentType);
}

#[Test]
public function retrieve_payment_rail_type()
{
$paymentRail = PaymentRail::factory()->create();
$this->assertInstanceOf(PaymentType::class, $paymentRail->type);

ServiceConfig::set('checkout', 'models.' . PaymentType::class, TestPaymentType::class);
$paymentRailWithOverriddenType = PaymentRail::factory()->create();
$this->assertInstanceOf(TestPaymentType::class, $paymentRailWithOverriddenType->type);
}

#[Test]
public function retrieve_payment_rail_payments()
{
$paymentRail = PaymentRail::factory()->create();
$this->assertEmpty($paymentRail->payments);

$paymentRailWith2Payments = PaymentRail::factory()->hasPayments(2)->create();
$this->assertCount(2, $paymentRailWith2Payments->payments);
$this->assertContainsOnlyInstancesOf(Payment::class, $paymentRailWith2Payments->payments);

ServiceConfig::set('checkout', 'models.' . Payment::class, TestPayment::class);
$paymentRailWith3OverriddenPayments = PaymentRail::factory()->hasPayments(3)->create();
$this->assertCount(3, $paymentRailWith3OverriddenPayments->payments);
$this->assertContainsOnlyInstancesOf(TestPayment::class, $paymentRailWith3OverriddenPayments->payments);
}
}

0 comments on commit 513e468

Please sign in to comment.