Skip to content

Commit

Permalink
use phpunit mocks builder for interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-netFantom committed Jul 25, 2023
1 parent e20e3d4 commit f10cc56
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 122 deletions.
119 changes: 64 additions & 55 deletions tests/RobokassaApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use netFantom\RobokassaApi\Results\{InvoicePayResult, ReceiptAttachResult, ReceiptStatusResult, SmsSendResult};
use netFantom\RobokassaApi\RobokassaApi;
use PHPUnit\Framework\TestCase as Unit;
use tests\data\WrongPsr18ClientWithoutRequestFactory;
use tests\data\WrongPsr18ClientWithoutStreamFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* @group robokassa
Expand Down Expand Up @@ -53,6 +54,30 @@ public function testExpirationDate(): void
$this->assertEquals($expected, $returnUrl);
}

public function testGetInvoicePayResultFromRequestArray(): void
{
$signatureValue = md5('100:1:password_2');
$email = 'test@exmaple.com';

$_POST['OutSum'] = 100;
$_POST['InvId'] = 1;
$_POST['SignatureValue'] = $signatureValue;
$_POST['shp_email'] = $email;

$expectedInvoicePayResult = new InvoicePayResult(
outSum: 100,
invId: 1,
signatureValue: $signatureValue,
userParameters: [
'email' => 'test@exmaple.com'
]
);

$invoicePayResult = RobokassaApi::getInvoicePayResultFromRequestArray($_POST);

$this->assertEquals($expectedInvoicePayResult, $invoicePayResult);
}

public function testGetPaymentParameters(): void
{
$robokassa = new RobokassaApi(
Expand Down Expand Up @@ -116,57 +141,6 @@ public function testGetPaymentParameters(): void
$this->assertEquals($expectedPaymentParameters, $paymentParameters);
}

public function testGetInvoicePayResultFromRequestArray(): void
{
$signatureValue = md5('100:1:password_2');
$email = 'test@exmaple.com';

$_POST['OutSum'] = 100;
$_POST['InvId'] = 1;
$_POST['SignatureValue'] = $signatureValue;
$_POST['shp_email'] = $email;

$expectedInvoicePayResult = new InvoicePayResult(
outSum: 100,
invId: 1,
signatureValue: $signatureValue,
userParameters: [
'email' => 'test@exmaple.com'
]
);

$invoicePayResult = RobokassaApi::getInvoicePayResultFromRequestArray($_POST);

$this->assertEquals($expectedInvoicePayResult, $invoicePayResult);
}

public function testgetSmsParameters(): void
{
$robokassa = new RobokassaApi(
merchantLogin: 'robo-demo',
password1: 'password#1',
password2: 'password#2',
);

$expectedParameters = [
'login' => 'robo-demo',
'phone' => 70001234567,
'message' => 'message text',
'signature' => '745a08955b089f7d4a64a24e6ba37611',
];
$parameters = $robokassa->getSendSmsData(70001234567, 'message text');

$this->assertEquals($expectedParameters, $parameters);

$robokassa->getSendSmsData(70001234567, str_repeat('x', 128));

try {
$robokassa->getSendSmsData(70001234567, str_repeat('x', 129));
$this->fail('Expect ' . TooLongSmsMessageException::class);
} catch (TooLongSmsMessageException) {
}
}

public function testPaymentReceipt(): void
{
$robokassa = new RobokassaApi(
Expand Down Expand Up @@ -480,23 +454,58 @@ public function testWrongPsr18ClientWithoutRequestFactory(): void
{
$this->expectException(MissingRequestFactory::class);

/** @noinspection PhpParamsInspection */
new RobokassaApi(
merchantLogin: 'robokassa_state',
password1: 'robokassatest',
password2: 'password#2',
psr18Client: new WrongPsr18ClientWithoutRequestFactory(),
psr18Client: $this->createMockForIntersectionOfInterfaces([
ClientInterface::class,
StreamFactoryInterface::class,
]),
);
}

public function testWrongPsr18ClientWithoutStreamFactory(): void
{
$this->expectException(MissingStreamFactory::class);

/** @noinspection PhpParamsInspection */
new RobokassaApi(
merchantLogin: 'robokassa_state',
password1: 'robokassatest',
password2: 'password#2',
psr18Client: new WrongPsr18ClientWithoutStreamFactory(),
psr18Client: $this->createMockForIntersectionOfInterfaces([
ClientInterface::class,
RequestFactoryInterface::class,
]),
);
}

public function testgetSmsParameters(): void
{
$robokassa = new RobokassaApi(
merchantLogin: 'robo-demo',
password1: 'password#1',
password2: 'password#2',
);

$expectedParameters = [
'login' => 'robo-demo',
'phone' => 70001234567,
'message' => 'message text',
'signature' => '745a08955b089f7d4a64a24e6ba37611',
];
$parameters = $robokassa->getSendSmsData(70001234567, 'message text');

$this->assertEquals($expectedParameters, $parameters);

$robokassa->getSendSmsData(70001234567, str_repeat('x', 128));

try {
$robokassa->getSendSmsData(70001234567, str_repeat('x', 129));
$this->fail('Expect ' . TooLongSmsMessageException::class);
} catch (TooLongSmsMessageException) {
}
}
}
39 changes: 0 additions & 39 deletions tests/data/WrongPsr18ClientWithoutRequestFactory.php

This file was deleted.

28 changes: 0 additions & 28 deletions tests/data/WrongPsr18ClientWithoutStreamFactory.php

This file was deleted.

0 comments on commit f10cc56

Please sign in to comment.