Skip to content

Commit

Permalink
Merge pull request #1 from jimanx2/main
Browse files Browse the repository at this point in the history
Added unit tests
  • Loading branch information
apis17 authored Apr 5, 2023
2 parents 6766cd4 + 00a97ba commit 21de93c
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/composer.lock
/.phpunit.*
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"version": "1.0.0",
"version": "1.0.1",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"require-dev": {
"phpunit/phpunit": "^9"
},
"scripts": {
"test": "vendor/bin/phpunit --testdox -c phpunit.xml"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="">
<directory>./src/tests</directory>
</testsuite>
</testsuites>

<php>
<env name="RMS_MERCHANT_ID" value="SB_sandbox"/>
<env name="RMS_VERIFY_KEY" value="--replaceme--"/>
<env name="RMS_SECRET_KEY" value="--replaceme---"/>
<env name="RMS_ENVIRONMENT" value="sandbox"/>
<env name="DEBUG" value="false"/>
</php>
</phpunit>
1 change: 1 addition & 0 deletions src/lib/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct($merchantId, $verifyKey, $secretKey, $environment)

public function getPaymentUrl($orderid, $amount, $bill_name, $bill_email, $bill_mobile, $bill_desc = 'RMS PHP Library', $channel = null, $currency = null, $returnUrl = null, $callbackurl = null, $cancelurl = null)
{
$amount = number_format($amount, 2, '.', '');
$data = [
'orderid' => $orderid,
'amount' => $amount,
Expand Down
25 changes: 25 additions & 0 deletions src/support/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

if (! function_exists("env")) {
function env($key, $default = null) {
$envval = getenv($key);
if (empty($envval)) {
return null;
}
return $envval;
}
}

if (! function_exists("dump")) {
function dump($val) {
fwrite(STDERR, print_r($val, 1) . PHP_EOL);
}
}

if (! function_exists("writelog")) {
function writelog($val, $level = "INFO", $extras = null) {
if (env("DEBUG", false)) {
dump("↓↓ [$level] $val ".(isset($extras) ? json_encode($extras) : ""));
}
}
}
27 changes: 27 additions & 0 deletions src/tests/PackageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

require_once(__DIR__."/../support/helpers.php");

use PHPUnit\Framework\TestCase;
use RazerMerchantServices\Payment as RmsPayment;

final class PackageTest extends TestCase
{
public function testCanDiscoverThisPackage(): void
{
$this->assertTrue(
class_exists(RmsPayment::class)
);
}

/**
* @depends testCanDiscoverThisPackage
*/
public function testCanInstantateBaseClass(): void
{
$rms = new RmsPayment(null, null, null, null);

$this->assertNotNull($rms);
$this->assertEquals(get_class($rms), RmsPayment::class);
}
}
148 changes: 148 additions & 0 deletions src/tests/PaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php declare(strict_types=1);

require_once(__DIR__."/../support/helpers.php");

use PHPUnit\Framework\TestCase;
use RazerMerchantServices\Payment as RmsPayment;

final class PaymentTest extends TestCase
{
private static $orderid = null;
private static $amount = null;
private static $bill_name = null;
private static $bill_email = null;
private static $bill_mobile = null;
private static $logbuf = [];

public static function setupBeforeClass(): void
{
self::$orderid = "TEST-ORDER";
self::$amount = "1.10";
self::$bill_name = "TEST USER";
self::$bill_email = "[email protected]";
self::$bill_mobile = "+60123456789";
}

public function testCanHandleInstantiationErrors(): RmsPayment
{
$rms = new RmsPayment(env('RMS_MERCHANT_ID'), env('RMS_VERIFY_KEY'), env('RMS_SECRET_KEY'), env('RMS_ENVIRONMENT'));

$this->assertEquals(
RmsPayment::class,
get_class($rms)
);

// TODO: add handling for invalid instantiations
// and then test the errors here
// $this->expectException(InvalidArgumentException::class);

return $rms;
}

/**
* @depends testCanHandleInstantiationErrors
*/
public function testCanCreatePaymentLink(RmsPayment $rms): string
{
$url = $rms->getPaymentUrl(
self::$orderid,
self::$amount,
self::$bill_name,
self::$bill_email,
self::$bill_mobile
);

writelog("Url - $url");
// check payment type
$this->assertIsString($url);

// check url must start with https://
$this->assertStringStartsWith("https://", $url);

// check domain must be pointing to correct endpoint
if (env("RMS_ENVIRONMENT") == "sandbox") {
$this->assertStringStartsWith("https://sandbox.merchant.razer.com", $url);
} else {
$this->assertStringStartsWith("https://pay.merchant.razer.com", $url);
}

return $url;
}

/**
* @depends testCanCreatePaymentLink
*/
public function testUrlContainsRequiredParameters(string $url): string
{
// check url must contain required parameters
$this->assertStringContainsString("orderid=".self::$orderid, $url);
$this->assertStringContainsString("amount=".self::$amount, $url);
$this->assertStringContainsString("bill_name=".urlencode(self::$bill_name), $url);
$this->assertStringContainsString("bill_email=".urlencode(self::$bill_email), $url);
$this->assertStringContainsString("bill_mobile=".urlencode(self::$bill_mobile), $url);
$this->assertStringContainsString("bill_desc=".urlencode("RMS PHP Library"), $url);

return $url;
}

/**
* @depends testUrlContainsRequiredParameters
*/
public function testVcodeIsProperlyCalculated(string $url): void
{
$amount = number_format(self::$amount*1, 2, '.', '');
writelog("Amount: $amount");
$calculatedVcode = md5($amount . env('RMS_MERCHANT_ID') . self::$orderid . env('RMS_VERIFY_KEY'));
writelog("Calculated Vcode - $calculatedVcode");
// check url contains valid vcode
$this->assertStringContainsString("&vcode=$calculatedVcode", $url);
}

/**
* @depends testCanHandleInstantiationErrors
*/
public function testCanVerifyValidSkey($rms) : void
{
$request = (object)[
"tranID" => 1000,
"amount" => "1.10",
"orderid" => "TEST-ORDER",
"status" => "00",
"currency" => "MYR",
"paydate" => date("Ymd H:i:s"),
"domain" => env("RMS_MERCHANT_ID"),
"appcode" => "123456",
];

$key = md5($request->tranID.$request->orderid.$request->status.$request->domain.$request->amount.$request->currency);
$request->skey = md5($request->paydate.$request->domain.$key.$request->appcode.env('RMS_SECRET_KEY'));

$this->assertTrue(
$rms->verifySignature($request->paydate, $request->domain, $key, $request->appcode, $request->skey)
);
}

/**
* @depends testCanHandleInstantiationErrors
*/
public function testCanVerifyInvalidSkey($rms) : void
{
$request = (object)[
"tranID" => 1000,
"amount" => "1.10",
"orderid" => "TEST-ORDER",
"status" => "00",
"currency" => "MYR",
"paydate" => date("Ymd H:i:s"),
"domain" => env("RMS_MERCHANT_ID"),
"appcode" => "123456",
];

$key = md5($request->tranID.$request->orderid.$request->status.$request->domain.$request->amount.$request->currency);
$request->skey = "just-a-tampered-skey";

$this->assertNotTrue(
$rms->verifySignature($request->paydate, $request->domain, $key, $request->appcode, $request->skey)
);
}
}

0 comments on commit 21de93c

Please sign in to comment.