Add unit tests

This commit is contained in:
dinamiko 2022-04-22 11:49:30 +02:00
parent 1082094e37
commit c7cf9bc3e9
6 changed files with 133 additions and 11 deletions

View file

@ -22,7 +22,7 @@ use WP_Error;
/**
* Class OrderEndpoint.
*/
class OrderEndpoint {
class PayUponInvoiceOrderEndpoint {
use RequestTrait;

View file

@ -18,7 +18,7 @@ use Automattic\WooCommerce\Admin\Notes\NoteTraits;
*/
class DeactivateNote {
use NoteTraits;
//use NoteTraits;
/**
* Name of the note for use in the database.

View file

@ -32,7 +32,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNet;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNetSessionId;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNetSourceWebsiteId;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\OrderEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceOrderEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PaymentSourceFactory;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoice;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
@ -2151,8 +2151,8 @@ return array(
$container->get( 'wcgateway.settings' )
);
},
'wcgateway.pay-upon-invoice-order-endpoint' => static function ( ContainerInterface $container ): OrderEndpoint {
return new OrderEndpoint(
'wcgateway.pay-upon-invoice-order-endpoint' => static function ( ContainerInterface $container ): PayUponInvoiceOrderEndpoint {
return new PayUponInvoiceOrderEndpoint(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.factory.order' ),

View file

@ -39,7 +39,7 @@ class PayUponInvoice {
/**
* The order endpoint.
*
* @var OrderEndpoint
* @var PayUponInvoiceOrderEndpoint
*/
protected $order_endpoint;
@ -76,7 +76,7 @@ class PayUponInvoice {
*
* @param string $module_url The module URL.
* @param FraudNet $fraud_net The FraudNet entity.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PayUponInvoiceOrderEndpoint $order_endpoint The order endpoint.
* @param LoggerInterface $logger The logger.
* @param Settings $settings The settings.
* @param Environment $environment The environment.
@ -85,7 +85,7 @@ class PayUponInvoice {
public function __construct(
string $module_url,
FraudNet $fraud_net,
OrderEndpoint $order_endpoint,
PayUponInvoiceOrderEndpoint $order_endpoint,
LoggerInterface $logger,
Settings $settings,
Environment $environment,

View file

@ -29,7 +29,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
/**
* The order endpoint.
*
* @var OrderEndpoint
* @var PayUponInvoiceOrderEndpoint
*/
protected $order_endpoint;
@ -64,14 +64,14 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
/**
* PayUponInvoiceGateway constructor.
*
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PayUponInvoiceOrderEndpoint $order_endpoint The order endpoint.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param PaymentSourceFactory $payment_source_factory The payment source factory.
* @param Environment $environment The environment.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
OrderEndpoint $order_endpoint,
PayUponInvoiceOrderEndpoint $order_endpoint,
PurchaseUnitFactory $purchase_unit_factory,
PaymentSourceFactory $payment_source_factory,
Environment $environment,

View file

@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice;
use Mockery;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\when;
class PayUponInvoiceGatewayTest extends TestCase
{
private $order_endpoint;
private $purchase_unit_factory;
private $payment_source_factory;
private $environment;
private $logger;
private $testee;
public function setUp(): void
{
parent::setUp();
$this->order_endpoint = Mockery::mock(PayUponInvoiceOrderEndpoint::class);
$this->purchase_unit_factory = Mockery::mock(PurchaseUnitFactory::class);
$this->payment_source_factory = Mockery::mock(PaymentSourceFactory::class);
$this->environment = Mockery::mock(Environment::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->setInitStubs();
$this->testee = new PayUponInvoiceGateway(
$this->order_endpoint,
$this->purchase_unit_factory,
$this->payment_source_factory,
$this->environment,
$this->logger
);
}
public function testProcessPayment()
{
list($order, $purchase_unit, $payment_source) = $this->setTestStubs();
$this->order_endpoint->shouldReceive('create')->with(
[$purchase_unit],
$payment_source,
''
)->andReturn($order);
$result = $this->testee->process_payment(1);
$this->assertEquals('success', $result['result']);
}
public function testProcessPaymentError()
{
list($order, $purchase_unit, $payment_source) = $this->setTestStubs();
$this->logger->shouldReceive('error');
when('wc_add_notice')->justReturn();
when('wc_get_checkout_url')->justReturn();
$this->order_endpoint->shouldReceive('create')->with(
[$purchase_unit],
$payment_source,
''
)->andThrows(\RuntimeException::class);
$result = $this->testee->process_payment(1);
$this->assertEquals('failure', $result['result']);
}
private function setInitStubs(): void
{
when('get_option')->justReturn([
'title' => 'foo',
'description' => 'bar',
]);
when('get_bloginfo')->justReturn('Foo');
$woocommerce = Mockery::mock(\WooCommerce::class);
$cart = Mockery::mock(\WC_Cart::class);
when('WC')->justReturn($woocommerce);
$woocommerce->cart = $cart;
$cart->shouldReceive('empty_cart');
}
/**
* @return array
*/
private function setTestStubs(): array
{
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder->shouldReceive('update_meta_data');
$wcOrder->shouldReceive('update_status');
when('wc_get_order')->justReturn($wcOrder);
$order = Mockery::mock(Order::class);
$order->shouldReceive('id')->andReturn('1');
$order->shouldReceive('intent')->andReturn('CAPTURE');
$purchase_unit = Mockery::mock(PurchaseUnit::class);
$payment_source = Mockery::mock(PaymentSource::class);
$this->payment_source_factory->shouldReceive('from_wc_order')
->with($wcOrder, '')
->andReturn($payment_source);
$this->purchase_unit_factory->shouldReceive('from_wc_order')
->with($wcOrder)
->andReturn($purchase_unit);
$this->environment->shouldReceive('current_environment_is')->andReturn(true);
return array($order, $purchase_unit, $payment_source);
}
}