Skip to content

Commit

Permalink
Merge pull request #10 from yii-cms/tests
Browse files Browse the repository at this point in the history
basic tests
  • Loading branch information
gonimar authored Jun 19, 2018
2 parents 8785073 + f563f3c commit 7558c10
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 7 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

/composer.lock

# composer itself is not needed
composer.phar

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml

# local tests configuration
/tests/data/config.local.php

# runtime cache
/tests/runtime
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

sudo: true

# cache vendor dirs
cache:
directories:
- $HOME/.composer/cache

install:
- travis_retry composer self-update && composer --version
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

script:
- phpunit --verbose $PHPUNIT_FLAGS

after_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
cd ../../..
travis_retry wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi
15 changes: 10 additions & 5 deletions Merchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace robokassa;

use Yii;
use yii\base\Object;
use yii\base\BaseObject;

class Merchant extends Object
class Merchant extends BaseObject
{
public $sMerchantLogin;

Expand All @@ -23,9 +23,11 @@ public function payment($nOutSum, $nInvId, $sInvDesc = null, $sIncCurrLabel=null
$url = $this->baseUrl;

$signature = "{$this->sMerchantLogin}:{$nOutSum}:{$nInvId}:{$this->sMerchantPass1}";

if (!empty($shp)) {
$signature .= ':' . $this->implodeShp($shp);
}

$sSignatureValue = $this->encryptSignature($signature);

$url .= '?' . http_build_query([
Expand All @@ -37,7 +39,7 @@ public function payment($nOutSum, $nInvId, $sInvDesc = null, $sIncCurrLabel=null
'IncCurrLabel' => $sIncCurrLabel,
'Email' => $sEmail,
'Culture' => $sCulture,
'IsTest' => (int)$this->isTest,
'IsTest' => $this->isTest ? 1 : null,
]);

if (!empty($shp) && ($query = http_build_query($shp)) !== '') {
Expand All @@ -55,24 +57,27 @@ public function payment($nOutSum, $nInvId, $sInvDesc = null, $sIncCurrLabel=null
private function implodeShp($shp)
{
ksort($shp);

foreach($shp as $key => $value) {
$shp[$key] = $key . '=' . $value;
}

return implode(':', $shp);
}

public function checkSignature($sSignatureValue, $nOutSum, $nInvId, $sMerchantPass, $shp)
public function checkSignature($sSignatureValue, $nOutSum, $nInvId, $sMerchantPass, $shp = [])
{
$signature = "{$nOutSum}:{$nInvId}:{$sMerchantPass}";

if (!empty($shp)) {
$signature .= ':' . $this->implodeShp($shp);
}

return strtolower($this->encryptSignature($signature)) === strtolower($sSignatureValue);

}

private function encryptSignature($signature)
protected function encryptSignature($signature)
{
return hash($this->hashAlgo, $signature);
}
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
yii2-robokassa
==============

[![Latest Stable Version](https://poser.pugx.org/yii-cms/yii2-robokassa/v/stable.png)](https://packagist.org/packages/yii-cms/yii2-robokassa)
[![Total Downloads](https://poser.pugx.org/yii-cms/yii2-robokassa/downloads.png)](https://packagist.org/packages/yii-cms/yii2-robokassa)
[![Build Status](https://travis-ci.org/yii-cms/yii2-robokassa.svg?branch=master)](https://travis-ci.org/yii-cms/yii2-robokassa)


## Install via Composer

~~~
Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*"
"yiisoft/yii2": "~2.0.13"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"autoload": {
"psr-4": { "robokassa\\": "" }
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
}
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Yii2 Robokassa Test Suite">
<directory>./tests/unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">.</directory>
<exclude>
<directory suffix=".php">vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
17 changes: 17 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

error_reporting(E_ALL);

define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

Yii::setAlias('@robokassa/tests/unit', __DIR__ . '/unit');
Yii::setAlias('@robokassa', dirname(__DIR__));

require_once(__DIR__ . '/compatibility.php');
51 changes: 51 additions & 0 deletions tests/compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* Ensures compatibility with PHPUnit < 6.x
*/
namespace PHPUnit\Framework\Constraint {
if (!class_exists('PHPUnit\Framework\Constraint\Constraint') && class_exists('PHPUnit_Framework_Constraint')) {
abstract class Constraint extends \PHPUnit_Framework_Constraint {}
}
}

namespace PHPUnit\Framework {
if (!class_exists('PHPUnit\Framework\TestCase') && class_exists('PHPUnit_Framework_TestCase')) {
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* @param string $exception
*/
public function expectException($exception)
{
$this->setExpectedException($exception);
}

/**
* @param string $message
*/
public function expectExceptionMessage($message)
{
$parentClassMethods = get_class_methods('PHPUnit_Framework_TestCase');
if (in_array('expectExceptionMessage', $parentClassMethods)) {
parent::expectExceptionMessage($message);
return;
}
$this->setExpectedException($this->getExpectedException(), $message);
}

/**
* @param string $messageRegExp
*/
public function expectExceptionMessageRegExp($messageRegExp)
{
$parentClassMethods = get_class_methods('PHPUnit_Framework_TestCase');
if (in_array('expectExceptionMessageRegExp', $parentClassMethods)) {
parent::expectExceptionMessageRegExp($messageRegExp);
return;
}
$this->setExpectedExceptionRegExp($this->getExpectedException(), $messageRegExp);
}
}
}
}
94 changes: 94 additions & 0 deletions tests/unit/MerchantTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
namespace robokassa\tests\unit;

use robokassa\Merchant;

class MerchantTest extends TestCase
{
public function testRedirectUrl()
{
$merchant = new Merchant([
'sMerchantLogin' => 'demo',
'sMerchantPass1' => 'password_1',
'hashAlgo' => 'md5',
'isTest' => true,
]);

$returnUrl = $merchant->payment(100, 1, 'Description', null, null, 'en', [], true);

$this->assertEquals("https://auth.robokassa.ru/Merchant/Index.aspx?MrchLogin=demo&OutSum=100&InvId=1&Desc=Description&SignatureValue=8a50b8d86ed28921edfc371cff6e156f&Culture=en&IsTest=1", $returnUrl);

// disable test
$merchant->isTest = false;

$returnUrl = $merchant->payment(100, 1, 'Description', null, null, 'en', [], true);

$this->assertEquals("https://auth.robokassa.ru/Merchant/Index.aspx?MrchLogin=demo&OutSum=100&InvId=1&Desc=Description&SignatureValue=8a50b8d86ed28921edfc371cff6e156f&Culture=en", $returnUrl);
}

public function testSignature()
{
$merchant = new Merchant([
'sMerchantLogin' => 'demo',
'sMerchantPass1' => 'password_1',
'hashAlgo' => 'md5',
'isTest' => true,
]);

$signature = md5('100:1:pass1'); // '1e8f0be69238c13020beba0206951535'

$check = $merchant->checkSignature($signature, 100, 1, 'pass1');

$this->assertInternalType('boolean', $check);

$this->assertTrue($check);
}

public function testSignatureUserParams()
{
$merchant = new Merchant([
'sMerchantLogin' => 'demo',
'sMerchantPass1' => 'password_1',
'hashAlgo' => 'md5',
'isTest' => true,
]);

$signature = md5('100:1:pass1:shp_id=1:shp_login=user1'); // 'd2b1beae30b0c2586eb4b4a7ce23aedd'

$this->assertTrue($merchant->checkSignature($signature, 100, 1, 'pass1', [
'shp_id' => 1,
'shp_login' => 'user1',
]));
}

public function testSignatureInvalidSortUserParams()
{
$merchant = new Merchant([
'sMerchantLogin' => 'demo',
'sMerchantPass1' => 'password_1',
'hashAlgo' => 'md5',
'isTest' => true,
]);

$signatureInvalidSort = md5('100:1:pass1:shp_login=user1:shp_id=1');

$this->assertFalse($merchant->checkSignature($signatureInvalidSort, 100, 1, 'pass1', [
'shp_id' => 1,
'shp_login' => 'user1',
]));
}

public function testSignatureAlgo()
{
$merchant = new Merchant([
'sMerchantLogin' => 'demo',
'sMerchantPass1' => 'password_1',
'hashAlgo' => 'sha256', // <=== 'sha256'
'isTest' => true,
]);

$signature = hash('sha256', '100:1:pass1');

$this->assertTrue($merchant->checkSignature($signature, 100, 1, 'pass1'));
}
}
Loading

0 comments on commit 7558c10

Please sign in to comment.