Skip to content

Commit

Permalink
Merge pull request #9 from njoguamos/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
njoguamos authored Mar 18, 2024
2 parents 5e44a76 + 1a52f58 commit 6180fbb
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
os: [ubuntu-latest, windows-latest]
php: [8.3, 8.2, 8.1]
laravel: [10.*]
stability: [prefer-stable]
stability: [prefer-stable, prefer-lowest]
include:
- laravel: 10.*
testbench: 8.*
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.1 | ^8.2 | ^8.3",
"illuminate/contracts": "^10.0",
"saloonphp/saloon": "^3.0",
"spatie/laravel-package-tools": "^1.14.0"
"saloonphp/saloon": "^3.7.0",
"spatie/laravel-package-tools": "^1.16.3"
},
"require-dev": {
"laravel/pint": "^1.14.0",
Expand All @@ -30,9 +30,9 @@
"pestphp/pest": "^2.34.4",
"pestphp/pest-plugin-arch": "^2.7.0",
"pestphp/pest-plugin-laravel": "^2.3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.16"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/Fixtures/CreatePesapalIpn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 18 Mar 2024 07:23:21 GMT",
"Content-Type": "application\/json; charset=utf-8",
"Content-Length": "349",
"Connection": "keep-alive",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"Expires": "-1",
"X-AspNet-Version": "4.0.30319",
"X-Powered-By": "ASP.NET",
"CF-Cache-Status": "DYNAMIC",
"Set-Cookie": "__cf_bm=TGl7fKsZWrlcSpSdAkNNFYfdkns8Jlrb6D.Jbfryb4Q-1710746601-1.0.1.1-CJX4cWrhy_pPn_jbXUylfd2x5YnjeCK6_yvtfUcsfUgVqEhnxXeu0IKPD_JkfBjtiYM2.OaQj9dJh1omCTNjyA; path=\/; expires=Mon, 18-Mar-24 07:53:21 GMT; domain=.pesapal.com; HttpOnly; Secure; SameSite=None",
"Server": "cloudflare",
"CF-RAY": "86637a13692ab199-NBO"
},
"data": "{\"id\":4254,\"url\":\"http:\/\/kihn.biz\/repellat-voluptatem-officiis-reiciendis-facere-blanditiis-quia-accusantium.html\",\"created_date\":\"2024-03-18T07:23:21.6391704Z\",\"ipn_id\":\"0fb0514f-da8c-43e4-b841-dd759364c16b\",\"notification_type\":0,\"ipn_notification_type_description\":\"GET\",\"ipn_status\":1,\"ipn_status_decription\":\"Active\",\"error\":null,\"status\":\"200\"}"
}
19 changes: 19 additions & 0 deletions tests/Fixtures/CreatePesapalToken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 18 Mar 2024 07:23:21 GMT",
"Content-Type": "application\/json; charset=utf-8",
"Content-Length": "564",
"Connection": "keep-alive",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"Expires": "-1",
"X-AspNet-Version": "4.0.30319",
"X-Powered-By": "ASP.NET",
"CF-Cache-Status": "DYNAMIC",
"Set-Cookie": "__cf_bm=JjT4WUkvuEe_I.db7.GYNJ4Zk9Xxu9SUQ9rihKy0l4M-1710746601-1.0.1.1-.E9pYm_AbCJa3dAetg0CYha5v9kzpGSqYICO.Nf612g5PNab0YEhEzLHNvpLghpbhnA9vaSvVPjHB7IfDUXDGw; path=\/; expires=Mon, 18-Mar-24 07:53:21 GMT; domain=.pesapal.com; HttpOnly; Secure; SameSite=None",
"Server": "cloudflare",
"CF-RAY": "86637a103aa3b196-NBO"
},
"data": "{\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3VzZXJkYXRhIjoiZWQ2MTkwMGYtZGNiMy00NjM2LWIxNGUtY2U1MGQwYzk2M2I1IiwidWlkIjoicWtpbzFCR0dZQVhUdTJKT2ZtN1hTWE5ydW9ac3JxRVciLCJuYmYiOjE3MTA3NDY2MDEsImV4cCI6MTcxMDc1MDIwMSwiaWF0IjoxNzEwNzQ2NjAxLCJpc3MiOiJodHRwOi8vY3licWEucGVzYXBhbC5jb20vIiwiYXVkIjoiaHR0cDovL2N5YnFhLnBlc2FwYWwuY29tLyJ9.Ygrl0JshPpelP_sIK_fRy41ctNKNWtvV0ZuCsPMxvqI\",\"expiryDate\":\"2024-03-18T08:23:21.1688946Z\",\"error\":null,\"status\":\"200\",\"message\":\"Request processed successfully\"}"
}
19 changes: 19 additions & 0 deletions tests/Fixtures/GetPesapalIpns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 18 Mar 2024 07:23:22 GMT",
"Content-Type": "application\/json; charset=utf-8",
"Content-Length": "2",
"Connection": "keep-alive",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"Expires": "-1",
"X-AspNet-Version": "4.0.30319",
"X-Powered-By": "ASP.NET",
"CF-Cache-Status": "DYNAMIC",
"Set-Cookie": "__cf_bm=ZQbkCCHgyCncNSiUyxpscONmuptjuJ3DVe2Dd6wBPXs-1710746602-1.0.1.1-u2nKN5GlFMJQg6dJxSr4WDA9K0gF7_Th28T3n7IIln..gltmiBkiHdg8tAwitfc5oMG74SFYfQFEpEg9sDcO5w; path=\/; expires=Mon, 18-Mar-24 07:53:22 GMT; domain=.pesapal.com; HttpOnly; Secure; SameSite=None",
"Server": "cloudflare",
"CF-RAY": "86637a16587e1b5f-NBO"
},
"data": "[]"
}
17 changes: 17 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
<?php

use NjoguAmos\Pesapal\Requests\CreatePesapalIpn;
use NjoguAmos\Pesapal\Requests\CreatePesapalToken;
use NjoguAmos\Pesapal\Requests\GetPesapalIpns;
use NjoguAmos\Pesapal\Tests\TestCase;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\MockConfig;
use Saloon\Config;

uses(TestCase::class)->in(__DIR__);


MockConfig::setFixturePath('tests/Fixtures');
Config::preventStrayRequests();

MockClient::global(mockData: [
CreatePesapalToken::class => MockResponse::fixture(name: 'CreatePesapalToken'),
CreatePesapalIpn::class => MockResponse::fixture(name: 'CreatePesapalIpn'),
GetPesapalIpns::class => MockResponse::fixture(name: 'GetPesapalIpns'),
]);
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp(): void
);
}

protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [
PesapalServiceProvider::class,
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Requests/CreatePesapalIpnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use NjoguAmos\Pesapal\Enums\IpnType;
use NjoguAmos\Pesapal\Requests\CreatePesapalIpn;

it(description: 'returns correct endpoint', closure: function () {
$request = new CreatePesapalIpn(url: fake()->url, ipnType: IpnType::GET);

expect(value: $request->resolveEndpoint())->toBe(expected: '/api/URLSetup/RegisterIPN');
});

it(description: 'forms the correct request body', closure: function () {
$url = fake()->url;

$request = new CreatePesapalIpn(url: $url, ipnType: IpnType::GET);

expect(value: $request->body()->get(key: 'url'))->toBe(expected: $url)
->and(value: $request->body()->get(key: 'ipn_notification_type'))->toBe(expected: IpnType::GET->name);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

$request = new CreatePesapalToken();

expect(value: $request->body()->get(key: 'consumer_key'))->toBe(expected: $consumerKey);
expect(value: $request->body()->get(key: 'consumer_secret'))->toBe(expected: $consumerSecret);
expect(value: $request->body()->get(key: 'consumer_key'))->toBe(expected: $consumerKey)
->and(value: $request->body()->get(key: 'consumer_secret'))->toBe(expected: $consumerSecret);
});

it(description: 'throws an exception when consumer key and secret are not set')->todo();
9 changes: 9 additions & 0 deletions tests/Unit/Requests/GetPesapalIpnsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use NjoguAmos\Pesapal\Requests\GetPesapalIpns;

it(description: 'returns correct endpoint', closure: function () {
$request = new GetPesapalIpns();

expect(value: $request->resolveEndpoint())->toBe(expected: '/api/URLSetup/GetIpnList');
});

0 comments on commit 6180fbb

Please sign in to comment.