Skip to content

Commit

Permalink
Restructure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olssonm committed Aug 9, 2024
1 parent 57bfa5f commit 84d54c5
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 467 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest,ubuntu-20.04]
php: [8.1,8.2]
laravel: [^9.0,^10.0]
php: [8.1,8.2,8.3]
laravel: [^10.0,^11.0]

name: ${{ matrix.os }}, PHP ${{ matrix.php }} / Laravel ${{ matrix.laravel }}

Expand Down
6 changes: 2 additions & 4 deletions src/Api/Payouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ public function create($payout): PayoutResult
));

$location = $response->getHeaderLine('Location');
$token = $response->getHeaderLine('PaymentRequestToken');

return new PayoutResult([
'id' => Id::parse($response),
'location' => strlen($location) > 0 ? $location : null,
'paymentRequestToken' => strlen($token) > 0 ? $token : null,
'payoutInstructionUUID' => Id::parse($response),
'location' => strlen($location) > 0 ? $location : null
]);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Olssonm\Swish\Callback;
use Olssonm\Swish\Payment;
use Olssonm\Swish\Refund;

test('callback', function () {
$paymentCallback = '{
"id": "5D59DA1B1632424E874DDB219AD54597",
"payeePaymentReference": "0123456789",
"paymentReference": "1E2FC19E5E5E4E18916609B7F8911C12",
"callbackUrl": "https://example.com/api/swishcb/paymentrequests",
"payerAlias": "4671234768",
"payeeAlias": "1231181189",
"amount": 100.00,
"currency": "SEK",
"message": "Kingston USB Flash Drive 8 GB",
"status": "PAID",
"dateCreated": "2019-01-02T14:29:51.092Z",
"datePaid": "2019-01-02T14:29:55.093Z",
"errorCode": null,
"errorMessage": ""
}';

$refundCallback = '{
"amount": "100.00",
"originalPaymentReference": "5D59DA1B1632424E874DDB219AD54597",
"dateCreated": "2020-10-29T13:40:27.950+0100",
"payerPaymentReference": null,
"payerAlias": "1231181189",
"callbackUrl": "https://9036c2a41fe0.ngrok.io/callback",
"currency": "SEK",
"id": "136A8AA7052F42CCB8563C78AB54C66B",
"payeeAlias": null,
"status": "DEBITED"
}';

$payment = Callback::parse($paymentCallback);
$refund = Callback::parse($refundCallback);

$this->assertInstanceOf(Payment::class, $payment);
$this->assertEquals('5D59DA1B1632424E874DDB219AD54597', $payment->id);

$this->assertInstanceOf(Refund::class, $refund);
$this->assertEquals('5D59DA1B1632424E874DDB219AD54597', $refund->originalPaymentReference);
$this->assertEquals('136A8AA7052F42CCB8563C78AB54C66B', $refund->id);
});
97 changes: 97 additions & 0 deletions tests/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

use Olssonm\Swish\Callback;
use Olssonm\Swish\Exceptions\CallbackDecodingException;
use Olssonm\Swish\Exceptions\ClientException;
use Olssonm\Swish\Exceptions\InvalidUuidException;
use Olssonm\Swish\Exceptions\ServerException;
use Olssonm\Swish\Exceptions\ValidationException;
use Olssonm\Swish\Payment;
use Olssonm\Swish\Refund;

it('throws InvalidUuidException', function () {
$this->expectException(InvalidUuidException::class);
new Payment(['id' => 'invalid-uuid']);
});

it('throws BadMethodCallException', function () {
$this->expectException(\BadMethodCallException::class);

$container = [];
$client = get_mock_client(200, [], null, $container);
$client->cancel(new Refund(['id' => '5D59DA1B1632424E874DDB219AD54597']));
});

it('throws ValidationException', function () {
$payment = new Payment();
$payment->id = '5D59DA1B1632424E874DDB219AD54597';
$payment->amount = 100;
$payment->currency = 'SEK';
$payment->payee = '123456789';

$container = [];
$client = get_mock_client(422, [], '{
"id": "5D59DA1B1632424E874DDB219AD54597",
"payeePaymentReference": "0123456789",
"paymentReference": "1E2FC19E5E5E4E18916609B7F8911C12",
"callbackUrl": "",
"payerAlias": "4671234768",
"payeeAlias": "1231181189",
"amount": 100.00,
"currency": "SEK",
"message": "Kingston USB Flash Drive 8 GB",
"status": "PAID",
"dateCreated": "2019-01-02T14:29:51.092Z",
"datePaid": "2019-01-02T14:29:55.093Z",
"errorCode": "RP03",
"errorMessage": "Callback URL is missing or does not use HTTPS."
}', $container);

try {
$response = $client->create($payment);
} catch (ValidationException $e) {
$this->assertInstanceOf(ValidationException::class, $e);
$this->assertEquals($e->getErrors()[0]->errorCode, 'RP03');
$this->assertEquals($e->getErrors()[0]->errorMessage, 'Callback URL is missing or does not use HTTPS.');
}

$this->assertEquals(422, $container[0]['response']->getStatusCode());
$this->assertEquals('/swish-cpcapi/api/v2/paymentrequests/5D59DA1B1632424E874DDB219AD54597', $container[0]['request']->getUri()->getPath());
$this->assertEquals('PUT', $container[0]['request']->getMethod());
});

it('throws InvalidArgumentException', function () {
$this->expectException(InvalidArgumentException::class);

$container = [];
$client = get_mock_client(429, [], null, $container);

$client->create(new stdClass());
});

it('throws ClientException', function () {
$this->expectException(ClientException::class);

$container = [];
$client = get_mock_client(429, [], null, $container);

$client->create(new Payment());
});

it('throws ServerException', function () {
$this->expectException(ServerException::class);

$container = [];
$client = get_mock_client(500, [], null, $container);
$client->create(new Payment());
});

it('throws CallbackDecodingException on invalid', function () {
$this->expectException(CallbackDecodingException::class);
Callback::parse('invalid');
});

it('throws CallbackDecodingException on null', function () {
$this->expectException(CallbackDecodingException::class);
Callback::parse(null);
});
32 changes: 32 additions & 0 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Olssonm\Swish\Certificate;
use Olssonm\Swish\Client;

Expand All @@ -10,7 +15,34 @@ function get_real_client($certificate = null)
__DIR__ . '/certificates/Swish_Merchant_TestCertificate_1234679304.pem',
'swish',
__DIR__ . '/certificates/Swish_TLS_RootCA.pem',
__DIR__ . '/certificates/Swish_Merchant_TestSigningCertificate_1234679304.key',
);
}
return new Client($certificate, Client::TEST_ENDPOINT);
}

function get_mock_client($code, $expectedHeaders, $expectedBody, &$history)
{
$mock = new MockHandler([
new Response($code, $expectedHeaders, $expectedBody),
]);
$stack = HandlerStack::create($mock);
$stack->push(Middleware::history($history));

return new Client(null, Client::TEST_ENDPOINT, new GuzzleHttpClient([
'handler' => $stack,
'http_errors' => false,
'curl' => [
CURLOPT_TCP_KEEPALIVE => 1,
CURLOPT_TCP_KEEPIDLE => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_CONNECTTIMEOUT => 20,
'verify' => __DIR__ . '/certificates/Swish_TLS_RootCA.pem',
'cert' => [
__DIR__ . '/certificates/Swish_Merchant_TestCertificate_1234679304.pem',
'swish'
]
],
'base_uri' => Client::TEST_ENDPOINT,
]));
}
83 changes: 83 additions & 0 deletions tests/MMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use Olssonm\Swish\Payment;
use Olssonm\Swish\PaymentResult;
use Olssonm\Swish\Payout;
use Olssonm\Swish\PayoutResult;
use Olssonm\Swish\Refund;
use Olssonm\Swish\RefundResult;
use Olssonm\Swish\Util\Time;
use Olssonm\Swish\Util\Uuid;

// Make a full standard test against the MSS API with payments and refunds
test('full chain of requests', function () {

$client = get_real_client();

// New payment
$payment = new Payment([
'amount' => 100,
'paymentRefeference' => 'abc123',
'currency' => 'SEK',
'payee' => '123456789',
'message' => 'Kingston USB Flash Drive 8 GB',
'callbackUrl' => 'https://webhook.site/ee23acc8-0ad9-4c34-866e-ef8ef421d7d4',
'payerAlias' => '4671234768',
'payeeAlias' => '1231181189',
]);
$id = $payment->id;

$response = $client->create($payment);
$this->assertEquals(201, $client->getHistory()[0]['response']->getStatusCode());
$this->assertEquals($id, $response->id);
$this->assertEquals(get_class($response), PaymentResult::class);

// Get payment
$response = $client->get(new Payment(['id' => $id]));
$this->assertEquals(200, $client->getHistory()[1]['response']->getStatusCode());
$this->assertEquals($id, $response->id);
$this->assertEquals(get_class($response), Payment::class);

// Refund a payment
$refund = new Refund([
'payerPaymentReference' => '0123456789',
'originalPaymentReference' => 'abc123',
'callbackUrl' => 'https://webhook.site/ee23acc8-0ad9-4c34-866e-ef8ef421d7d4',
'amount' => '100',
'currency' => 'SEK',
'payerAlias' => '1234567839',
'message' => 'Refund for Kingston SSD Drive 320 GB',
]);
$id = $refund->id;
$response = $client->create($refund);
$this->assertEquals(201, $client->getHistory()[2]['response']->getStatusCode());
$this->assertEquals($id, $response->id);
$this->assertEquals(get_class($response), RefundResult::class);

// Get a refund
$response = $client->get(new Refund(['id' => $refund->id]));
$this->assertEquals(200, $client->getHistory()[3]['response']->getStatusCode());
$this->assertEquals($refund->id, $response->id);
$this->assertEquals(get_class($response), Refund::class);

// Make payout
$payout = new Payout([
'payoutInstructionUUID' => Uuid::make(),
'payerPaymentReference' => 'Test',
'signingCertificateSerialNumber' => $client->getCertificate()->getSerial(),
'payerAlias' => '1234679304',
'payeeAlias' => '1234679304',
'payeeSSN' => '195810288083',
'amount' => '100',
'currency' => 'SEK',
'payoutType' => 'PAYOUT',
'message' => 'Test',
'callbackUrl' => 'https://example.com/callback',
'instructionDate' => Time::make(),
]);

$response = $client->create($payout);
$this->assertEquals(201, $client->getHistory()[4]['response']->getStatusCode());
$this->assertEquals(get_class($response), PayoutResult::class);
$this->assertEquals($payout->payoutInstructionUUID, $response->payoutInstructionUUID);
});
35 changes: 0 additions & 35 deletions tests/Payout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,6 @@
use Olssonm\Swish\Util\Time;
use Olssonm\Swish\Util\Uuid;

it('can retrieve certificate serial', function () {
$certificate = new Certificate(
__DIR__ . '/certificates/Swish_Merchant_TestCertificate_1234679304.pem',
'swish',
__DIR__ . '/certificates/Swish_TLS_RootCA.pem',
);

$this->assertEquals($certificate->getSerial(), '4512B3EBDA6E3CE6BFB14ABA6274A02C');
});

it('can perform payout', function() {

$certificate = new Certificate(
__DIR__ . '/certificates/Swish_Merchant_TestCertificate_1234679304.pem',
'swish',
__DIR__ . '/certificates/Swish_TLS_RootCA.pem',
__DIR__ . '/certificates/Swish_Merchant_TestSigningCertificate_1234679304.key',
);

$payout = new Payout([
'payoutInstructionUUID' => Uuid::make(),
'payerPaymentReference' => 'Test',
'signingCertificateSerialNumber' => $certificate->getSerial(),
'payerAlias' => '1234679304',
'payeeAlias' => '1234679304',
'payeeSSN' => '195810288083',
'amount' => '100',
'currency' => 'SEK',
'payoutType' => 'PAYOUT',
'message' => 'Test',
'callbackUrl' => 'https://example.com/callback',
'instructionDate' => Time::make(),
]);

$client = get_real_client($certificate);

$client->create($payout);
});
Loading

0 comments on commit 84d54c5

Please sign in to comment.