mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Add tests
This commit is contained in:
parent
4c5f0552f9
commit
4cecb908e6
3 changed files with 119 additions and 14 deletions
|
@ -60,12 +60,13 @@ class PayPalRequestIdRepository {
|
||||||
* @param string $request_id The ID.
|
* @param string $request_id The ID.
|
||||||
*/
|
*/
|
||||||
public function set( string $key, string $request_id ): void {
|
public function set( string $key, string $request_id ): void {
|
||||||
$all = $this->all();
|
$all = $this->all();
|
||||||
$all[ $key ] = array(
|
$day_in_seconds = 86400;
|
||||||
|
$all[ $key ] = array(
|
||||||
'id' => $request_id,
|
'id' => $request_id,
|
||||||
'expiration' => time() + 10 * DAY_IN_SECONDS,
|
'expiration' => time() + 10 * $day_in_seconds,
|
||||||
);
|
);
|
||||||
$all = $this->cleanup( $all );
|
$all = $this->cleanup( $all );
|
||||||
update_option( self::KEY, $all );
|
update_option( self::KEY, $all );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
|
||||||
|
|
||||||
|
use Mockery;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||||
|
use WooCommerce\PayPalCommerce\TestCase;
|
||||||
|
use function Brain\Monkey\Functions\when;
|
||||||
|
|
||||||
|
class PayPalRequestIdRepositoryTest extends TestCase
|
||||||
|
{
|
||||||
|
private $testee;
|
||||||
|
|
||||||
|
private $data = [];
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->testee = new PayPalRequestIdRepository();
|
||||||
|
|
||||||
|
when('get_option')->alias(function () {
|
||||||
|
return $this->data;
|
||||||
|
});
|
||||||
|
when('update_option')->alias(function (string $key, array $data) {
|
||||||
|
$this->data = $data;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testForOrder()
|
||||||
|
{
|
||||||
|
$this->testee->set_for_order($this->createPaypalOrder('42'), 'request1');
|
||||||
|
$this->testee->set_for_order($this->createPaypalOrder('43'), 'request2');
|
||||||
|
|
||||||
|
self::assertEquals('request1', $this->testee->get_for_order($this->createPaypalOrder('42')));
|
||||||
|
self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43')));
|
||||||
|
self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('41')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExpiration()
|
||||||
|
{
|
||||||
|
$this->testee->set_for_order($this->createPaypalOrder('42'), 'request1');
|
||||||
|
$this->data['42']['expiration'] = time() - 1;
|
||||||
|
$this->testee->set_for_order($this->createPaypalOrder('43'), 'request2');
|
||||||
|
|
||||||
|
self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('42')));
|
||||||
|
self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43')));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createPaypalOrder(string $id): Order {
|
||||||
|
$order = Mockery::mock(Order::class);
|
||||||
|
$order
|
||||||
|
->shouldReceive('id')
|
||||||
|
->andReturn($id);
|
||||||
|
return $order;
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,11 +32,12 @@ class AuthorizedPaymentsProcessorTest extends TestCase
|
||||||
private $amount = 42.0;
|
private $amount = 42.0;
|
||||||
private $currency = 'EUR';
|
private $currency = 'EUR';
|
||||||
private $paypalOrder;
|
private $paypalOrder;
|
||||||
|
private $authorization;
|
||||||
private $orderEndpoint;
|
private $orderEndpoint;
|
||||||
private $paymentsEndpoint;
|
private $paymentsEndpoint;
|
||||||
private $notice;
|
private $notice;
|
||||||
private $config;
|
private $config;
|
||||||
private $subscription_helper;
|
private $subscription_helperauthorization;
|
||||||
private $testee;
|
private $testee;
|
||||||
|
|
||||||
public function setUp(): void {
|
public function setUp(): void {
|
||||||
|
@ -44,7 +45,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase
|
||||||
|
|
||||||
$this->wcOrder = $this->createWcOrder($this->paypalOrderId);
|
$this->wcOrder = $this->createWcOrder($this->paypalOrderId);
|
||||||
|
|
||||||
$this->paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED)]);
|
$this->authorization = $this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED);
|
||||||
|
$this->paypalOrder = $this->createPaypalOrder([$this->authorization]);
|
||||||
|
|
||||||
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
|
||||||
$this->orderEndpoint
|
$this->orderEndpoint
|
||||||
|
@ -176,6 +178,57 @@ class AuthorizedPaymentsProcessorTest extends TestCase
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testVoid()
|
||||||
|
{
|
||||||
|
$authorizations = [
|
||||||
|
$this->createAuthorization('id1', AuthorizationStatus::CREATED),
|
||||||
|
$this->createAuthorization('id2', AuthorizationStatus::VOIDED),
|
||||||
|
$this->createAuthorization('id3', AuthorizationStatus::PENDING),
|
||||||
|
$this->createAuthorization('id4', AuthorizationStatus::CAPTURED),
|
||||||
|
$this->createAuthorization('id5', AuthorizationStatus::DENIED),
|
||||||
|
$this->createAuthorization('id6', AuthorizationStatus::EXPIRED),
|
||||||
|
$this->createAuthorization('id7', AuthorizationStatus::COMPLETED),
|
||||||
|
];
|
||||||
|
$this->paypalOrder = $this->createPaypalOrder($authorizations);
|
||||||
|
|
||||||
|
$this->paymentsEndpoint
|
||||||
|
->expects('void')
|
||||||
|
->with($authorizations[0]);
|
||||||
|
$this->paymentsEndpoint
|
||||||
|
->expects('void')
|
||||||
|
->with($authorizations[2]);
|
||||||
|
|
||||||
|
$this->testee->void_authorizations($this->paypalOrder);
|
||||||
|
|
||||||
|
self::assertTrue(true); // fix no assertions warning
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoidWhenNoVoidable()
|
||||||
|
{
|
||||||
|
$exception = new RuntimeException('void error');
|
||||||
|
$this->paymentsEndpoint
|
||||||
|
->expects('void')
|
||||||
|
->with($this->authorization)
|
||||||
|
->andThrow($exception);
|
||||||
|
|
||||||
|
$this->expectExceptionObject($exception);
|
||||||
|
|
||||||
|
$this->testee->void_authorizations($this->paypalOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoidWhenNoError()
|
||||||
|
{
|
||||||
|
$authorizations = [
|
||||||
|
$this->createAuthorization('id1', AuthorizationStatus::VOIDED),
|
||||||
|
$this->createAuthorization('id2', AuthorizationStatus::EXPIRED),
|
||||||
|
];
|
||||||
|
$this->paypalOrder = $this->createPaypalOrder($authorizations);
|
||||||
|
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
|
||||||
|
$this->testee->void_authorizations($this->paypalOrder);
|
||||||
|
}
|
||||||
|
|
||||||
private function createWcOrder(string $paypalOrderId): WC_Order {
|
private function createWcOrder(string $paypalOrderId): WC_Order {
|
||||||
$wcOrder = Mockery::mock(WC_Order::class);
|
$wcOrder = Mockery::mock(WC_Order::class);
|
||||||
$wcOrder
|
$wcOrder
|
||||||
|
@ -192,14 +245,7 @@ class AuthorizedPaymentsProcessorTest extends TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createAuthorization(string $id, string $status): Authorization {
|
private function createAuthorization(string $id, string $status): Authorization {
|
||||||
$authorization = Mockery::mock(Authorization::class);
|
return new Authorization($id, new AuthorizationStatus($status));
|
||||||
$authorization
|
|
||||||
->shouldReceive('id')
|
|
||||||
->andReturn($id);
|
|
||||||
$authorization
|
|
||||||
->shouldReceive('status')
|
|
||||||
->andReturn(new AuthorizationStatus($status));
|
|
||||||
return $authorization;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createCapture(string $status): Capture {
|
private function createCapture(string $status): Capture {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue