-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jimanx2/main
Added unit tests
- Loading branch information
Showing
7 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
/composer.lock | ||
/.phpunit.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) : "")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |