Skip to content

Commit

Permalink
Added unit tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed May 22, 2019
1 parent da2a11b commit d9eb38a
Show file tree
Hide file tree
Showing 19 changed files with 578 additions and 75 deletions.
20 changes: 0 additions & 20 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,4 @@ public function setYandexClient(Client $client): void
{
$this->client = $client;
}

// public function getTransactionId()
// {
// return $this->getParameter('orderId');
// }
//
// public function setTransactionId($value)
// {
// return $this->setParameter('orderId', $value);
// }
//
// public function getOrderId()
// {
// return $this->getParameter('orderId');
// }
//
// public function setOrderId($value)
// {
// return $this->setParameter('orderId', $value);
// }
}
1 change: 1 addition & 0 deletions src/Message/IncomingNotificationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Class IncomingNotificationRequest
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @method IncomingNotificationResponse send()
*/
class IncomingNotificationRequest extends AbstractRequest
{
Expand Down
2 changes: 1 addition & 1 deletion src/Message/IncomingNotificationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(RequestInterface $request, $data)
parent::__construct($request, $data);
}

public function getOrderId()
public function getTransactionId()
{
return $this->data['object']['metadata']['transactionId'] ?? null;
}
Expand Down
46 changes: 19 additions & 27 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,41 @@
*/
class PurchaseRequest extends AbstractRequest
{
/**
* @return CreatePaymentResponse
* @throws InvalidRequestException
*/
public function getData()
{
$this->validate('amount', 'currency', 'returnUrl', 'transactionId');
$this->validate('amount', 'currency', 'returnUrl', 'transactionId', 'description');

return [
'amount' => $this->getAmount(),
'currency' => $this->getCurrency(),
'description' => $this->getDescription(),
'return_url' => $this->getReturnUrl(),
'transactionId' => $this->getTransactionId(),
];
}

public function sendData($data)
{
try {
$paymentResponse = $this->client->createPayment([
'amount' => [
'value' => $this->getAmount(),
'currency' => $this->getCurrency(),
'value' => $data['amount'],
'currency' => $data['currency'],
],
'description' => $this->getDetails(),
'description' => $data['description'],
'confirmation' => [
'type' => 'redirect',
'return_url' => $this->getReturnUrl(),
'return_url' => $data['return_url'],
],
'metadata' => [
'transactionId' => $this->getTransactionId(),
'transactionId' => $data['transactionId'],
],
], 'create-' . $this->getTransactionId());
], 'create-' . $data['transactionId']);

return $paymentResponse;
return $this->response = new PurchaseResponse($this, $paymentResponse);
} catch (Throwable $e) {
throw new InvalidRequestException('Failed to request purchase: ' . $e->getMessage(), 0, $e);
}
}

public function sendData($data)
{
return $this->response = new PurchaseResponse($this, $data);
}

public function getDetails(): ?string
{
return $this->getParameter('details');
}

public function setDetails(string $value)
{
return $this->setParameter('details', $value);
}
}

10 changes: 10 additions & 0 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public function getRedirectUrl()
return $confirmation->getConfirmationUrl();
}

public function getTransactionReference()
{
return $this->data->getId();
}

public function getTransactionId()
{
return $this->data->getMetadata()['transactionId'] ?? null;
}

public function isSuccessful()
{
return false;
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,4 @@ public function testPurchase()
$this->assertSame($this->currency, $request->getCurrency());
$this->assertSame($this->amount, $request->getAmount());
}

// public function testCompletePurchase()
// {
// $request = $this->gateway->completePurchase([
// 'transactionId' => $this->transactionId,
// ]);
//
// $this->assertSame($this->purse, $request->getPurse());
// $this->assertSame($this->secret, $request->getSecret());
// $this->assertSame($this->transactionId, $request->getTransactionId());
// }

}
68 changes: 68 additions & 0 deletions tests/unit/Message/CaptureRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* YandexKassa driver for the Omnipay PHP payment processing library
*
* @link https://github.com/hiqdev/omnipay-YandexKassa
* @package omnipay-YandexKassa
* @license MIT
* @copyright Copyright (c) 2019, HiQDev (http://hiqdev.com/)
*/

namespace Omnipay\YandexKassa\Tests\Message;

use Omnipay\YandexKassa\Message\CaptureRequest;
use Omnipay\YandexKassa\Message\CaptureResponse;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

class CaptureRequestTest extends TestCase
{
/** @var CaptureRequest */
private $request;

private $shopId = '54401';
private $secretKey = 'test_Fh8hUAVVBGUGbjmlzba6TB0iyUbos_lueTHE-axOwM0';
private $transactionReference = '2475e163-000f-5000-9000-18030530d620';
private $transactionId = '5ce3cdb0d1436';
private $amount = '187.50';
private $currency = 'RUB';

public function setUp()
{
parent::setUp();

$httpRequest = new HttpRequest();
$this->request = new CaptureRequest($this->getHttpClient(), $httpRequest);
$this->request->initialize([
'yandexClient' => $this->buildYandexClient($this->shopId, $this->secretKey),
'shopId' => $this->shopId,
'secret' => $this->secretKey,
'transactionReference' => $this->transactionReference,
'transactionId' => $this->transactionId,
'amount' => $this->amount,
'currency' => $this->currency,
]);
}

public function testGetData()
{
$data = $this->request->getData();
$this->assertEmpty($data);
}

public function testSendData()
{
$curlClientStub = $this->getCurlClientStub();
$curlClientStub->method('sendRequest')->willReturn([
[],
$this->fixture('payment.succeeded'),
['http_code' => 200],
]);

$this->getYandexClient($this->request)
->setApiClient($curlClientStub)
->setAuth($this->shopId, $this->secretKey);

$response = $this->request->sendData([]);
$this->assertInstanceOf(CaptureResponse::class, $response);
}
}
68 changes: 68 additions & 0 deletions tests/unit/Message/CaptureResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Omnipay\YandexKassa\Tests\Message;

use Omnipay\YandexKassa\Message\CaptureRequest;
use Omnipay\YandexKassa\Message\CaptureResponse;
use Omnipay\YandexKassa\Message\DetailsResponse;
use Omnipay\YandexKassa\Message\IncomingNotificationRequest;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

class CaptureResponseTest extends TestCase
{
/** @var IncomingNotificationRequest */
private $request;


private $shopId = '54401';
private $secretKey = 'test_Fh8hUAVVBGUGbjmlzba6TB0iyUbos_lueTHE-axOwM0';
private $transactionReference = '2475e163-000f-5000-9000-18030530d620';
private $transactionId = '5ce3cdb0d1436';
private $amount = '187.50';
private $currency = 'RUB';

public function setUp()
{
parent::setUp();

$httpRequest = new HttpRequest();
$this->request = new CaptureRequest($this->getHttpClient(), $httpRequest);
$this->request->initialize([
'yandexClient' => $this->buildYandexClient($this->shopId, $this->secretKey),
'shopId' => $this->shopId,
'secret' => $this->secretKey,
'transactionReference' => $this->transactionReference,
'transactionId' => $this->transactionId,
'amount' => $this->amount,
'currency' => $this->currency,
]);
}

public function testSuccess(): void
{
$curlClientStub = $this->getCurlClientStub();
$curlClientStub->method('sendRequest')
->willReturn([
[],
$this->fixture('payment.succeeded'),
['http_code' => 200]
]);

$this->getYandexClient($this->request)
->setApiClient($curlClientStub)
->setAuth($this->shopId, $this->secretKey);

/** @var DetailsResponse $response */
$response = $this->request->send();

$this->assertInstanceOf(DetailsResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertSame('5ce3cdb0d1436', $response->getTransactionId());
$this->assertSame('2475e163-000f-5000-9000-18030530d620', $response->getTransactionReference());
$this->assertSame('187.50', $response->getAmount());
$this->assertSame('RUB', $response->getCurrency());
$this->assertSame('2019-05-21T10:09:54+00:00', $response->getPaymentDate()->format(\DATE_ATOM));
$this->assertSame('succeeded', $response->getState());
$this->assertSame('Bank card *4444', $response->getPayer());
}
}
63 changes: 63 additions & 0 deletions tests/unit/Message/DetailsRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* YandexKassa driver for the Omnipay PHP payment processing library
*
* @link https://github.com/hiqdev/omnipay-YandexKassa
* @package omnipay-YandexKassa
* @license MIT
* @copyright Copyright (c) 2019, HiQDev (http://hiqdev.com/)
*/

namespace Omnipay\YandexKassa\Tests\Message;

use Omnipay\YandexKassa\Message\DetailsRequest;
use Omnipay\YandexKassa\Message\DetailsResponse;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

class DetailsRequestTest extends TestCase
{
/** @var DetailsRequest */
private $request;

private $shopId = '54401';
private $secretKey = 'test_Fh8hUAVVBGUGbjmlzba6TB0iyUbos_lueTHE-axOwM0';
private $transactionReference = '2475e163-000f-5000-9000-18030530d620';

public function setUp()
{
parent::setUp();

$httpRequest = new HttpRequest();
$this->request = new DetailsRequest($this->getHttpClient(), $httpRequest);
$this->request->initialize([
'yandexClient' => $this->buildYandexClient($this->shopId, $this->secretKey),
'shopId' => $this->shopId,
'secret' => $this->secretKey,
'transactionReference' => $this->transactionReference,
]);
}

public function testGetData()
{
$data = $this->request->getData();
$this->assertEmpty($data);
}

public function testSendData()
{
$curlClientStub = $this->getCurlClientStub();
$curlClientStub->method('sendRequest')
->willReturn([
[],
$this->fixture('payment.waiting_for_capture'),
['http_code' => 200]
]);

$this->getYandexClient($this->request)
->setApiClient($curlClientStub)
->setAuth($this->shopId, $this->secretKey);

$response = $this->request->sendData([]);
$this->assertInstanceOf(DetailsResponse::class, $response);
}
}
Loading

0 comments on commit d9eb38a

Please sign in to comment.