Skip to content

Commit

Permalink
cleaned up Paystack unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rukykf committed Jan 15, 2020
1 parent 74aab64 commit a6d4be2
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions tests/unit/PaymentProviders/PaystackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ class PaystackTest extends TestCase

public function setUp(): void
{
parent::setUp();
$this->paystack = new Paystack('public', 'secret', 'testing');
$this->paystack->setHttpClient(MockHttpClient::getHttpClient());
}

public function testInitializePayment()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulInitializePaymentResponse(),
MockPaystackApiResponse::getSuccessfulInitializePaymentResponse()
]));

//it successfully adapts provided request params to the paystack api
$this->paystack->initializePayment([
'customer_email' => 'customer@example.com', //paystack expects email not customer_email as the param
Expand All @@ -39,7 +42,7 @@ public function testInitializePayment()
$this->assertEquals('Bearer secret', $sent_request->getHeader('authorization')[0]);
$this->assertEquals('/transaction/initialize', $sent_request->getUri()->getPath());

MockHttpClient::appendResponsesToMockHttpClient([new Response(200)]);

$this->paystack->initializePayment([
'customer_email' => 'customer@example.com',
'amount' => 3000
Expand Down Expand Up @@ -122,13 +125,12 @@ public function testGetPaymentReference()

public function testIsPaymentValid()
{
$responses = [
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulVerifyPaymentResponse(),
MockPaystackApiResponse::getSuccessfulVerifyPaymentResponse(),
MockPaystackApiResponse::getFailedVerifyPaymentResponse(),
MockPaystackApiResponse::getFailedVerifyPaymentResponse(),
];
$this->paystack->setHttpClient(MockHttpClient::getHttpClient($responses));
]));

//Without Exceptions
$this->paystack->disablePaymentExceptions();
Expand Down Expand Up @@ -200,12 +202,12 @@ public function testChargeAuth()
public function testGetPaymentAuthorizationCode()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulVerifyPaymentResponse()
MockPaystackApiResponse::getSuccessfulVerifyPaymentResponse(),
new Response(404)
]));
$this->assertTrue($this->paystack->isPaymentValid('mock_reference', 5000));
$this->assertEquals('mock_authorization_code', $this->paystack->getPaymentAuthorizationCode());

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$this->paystack->disablePaymentExceptions();
$this->paystack->isPaymentValid('mock_reference', 5000);
$this->assertNull($this->paystack->getPaymentAuthorizationCode());
Expand All @@ -215,22 +217,22 @@ public function testSavePlan()
{
//test create
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulCreatePlanResponse()
MockPaystackApiResponse::getSuccessfulCreatePlanResponse(),
new Response(404),
MockPaystackApiResponse::getSuccessfulUpdatePlanResponse(),
new Response(404),
]));

$plan_code = $this->paystack->savePlan(["name" => "Mock Plan", "amount" => 30000, "interval" => "weekly"]);
$this->assertEquals("plan_code", $plan_code);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plan_code = $this->paystack->savePlan(["name" => "Mock Plan", "amount" => 30000, "interval" => "weekly"]);
$this->assertNull($plan_code);

//test update
MockHttpClient::appendResponsesToMockHttpClient([MockPaystackApiResponse::getSuccessfulUpdatePlanResponse()]);
$plan_code = $this->paystack->savePlan(["plan_code" => 'plan_code', "name" => "Mock Plan"]);
$this->assertEquals("plan_code", $plan_code);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plan_code = $this->paystack->savePlan(["plan_code" => 'plan_code', "name" => "Mock Plan"]);
$this->assertNull($plan_code);

Expand All @@ -242,46 +244,49 @@ public function testSavePlan()
public function testFetchAllPlans()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulFetchAllPlansResponse()
MockPaystackApiResponse::getSuccessfulFetchAllPlansResponse(),
new Response(404),
new Response(404)
]));

$plans = $this->paystack->fetchAllPlans();
$this->assertEquals(2, count($plans));

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plans = $this->paystack->fetchAllPlans();
$this->assertNull($plans);

$this->paystack->enableHttpExceptions();
$this->expectException(BadResponseException::class);
MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plan = $this->paystack->fetchAllPlans();
}

public function testFetchPlan()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulFetchPlanResponse()
MockPaystackApiResponse::getSuccessfulFetchPlanResponse(),
new Response(404),
new Response(404)
]));

$plan = $this->paystack->fetchPlan("plan_code");
$this->assertEquals(50000, $plan['amount']);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plan = $this->paystack->fetchPlan("plan_code");
$this->assertNull($plan);

$this->paystack->enableHttpExceptions();
$this->expectException(BadResponseException::class);
MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$plan = $this->paystack->fetchPlan("plan_code");
$this->paystack->fetchPlan("plan_code");
}

public function testSaveSubaccount()
{
//test create
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulCreateSubAccountResponse()
MockPaystackApiResponse::getSuccessfulCreateSubAccountResponse(),
new Response(404),
MockPaystackApiResponse::getSuccessfulUpdateSubAccountResponse(),
new Response(404)
]));
$subaccount_code = $this->paystack->saveSubaccount([
"business_name" => "mock business",
Expand All @@ -291,7 +296,6 @@ public function testSaveSubaccount()
]);
$this->assertEquals('subaccount_code', $subaccount_code);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$subaccount_code = $this->paystack->saveSubaccount([
"business_name" => "mock business",
"settlement_bank" => "mock bank",
Expand All @@ -301,11 +305,9 @@ public function testSaveSubaccount()
$this->assertEquals(null, $subaccount_code);

//test update
MockHttpClient::appendResponsesToMockHttpClient([MockPaystackApiResponse::getSuccessfulUpdateSubAccountResponse()]);
$subaccount_code = $this->paystack->saveSubaccount(["subaccount_code" => "subaccount_code"]);
$this->assertEquals("subaccount_code", $subaccount_code);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$subaccount_code = $this->paystack->saveSubaccount(["subaccount_code" => "subaccount_code"]);
$this->assertNull($subaccount_code);

Expand All @@ -317,30 +319,30 @@ public function testSaveSubaccount()
public function testFetchAllSubaccounts()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulFetchAllSubAccountsResponse()
MockPaystackApiResponse::getSuccessfulFetchAllSubAccountsResponse(),
new Response(404),
new Response(404)
]));
$subaccounts = $this->paystack->fetchAllSubaccounts();
$this->assertEquals(3, count($subaccounts));

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$subaccounts = $this->paystack->fetchAllSubaccounts();
$this->assertNull($subaccounts);

$this->expectException(BadResponseException::class);
$this->paystack->enableHttpExceptions();
MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$subaccounts = $this->paystack->fetchAllSubaccounts();
$this->paystack->fetchAllSubaccounts();
}

public function testFetchSubaccount()
{
$this->paystack->setHttpClient(MockHttpClient::getHttpClient([
MockPaystackApiResponse::getSuccessfulFetchSubAccountResponse()
MockPaystackApiResponse::getSuccessfulFetchSubAccountResponse(),
new Response(404)
]));
$subaccount = $this->paystack->fetchSubaccount("subaccount_id");
$this->assertEquals(55, $subaccount['id']);

MockHttpClient::appendResponsesToMockHttpClient([new Response(404)]);
$subaccount = $this->paystack->fetchSubaccount("subaccount_id");
$this->assertNull($subaccount);
}
Expand Down

0 comments on commit a6d4be2

Please sign in to comment.