woocommerce-paypal-payments/tests/PHPUnit/Button/Endpoint/ValidateCheckoutEndpointTest.php

97 lines
2.4 KiB
PHP
Raw Normal View History

2023-02-07 15:30:59 +02:00
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Mockery;
2023-03-01 16:20:31 +02:00
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
2023-02-07 15:30:59 +02:00
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
2023-02-07 15:30:59 +02:00
class ValidateCheckoutEndpointTest extends TestCase
{
2023-03-01 16:20:31 +02:00
use MockeryPHPUnitIntegration;
2023-02-07 15:30:59 +02:00
private $requestData;
private $formValidator;
private $logger;
private $sut;
private $session = [];
2023-02-07 15:30:59 +02:00
public function setUp(): void
{
parent::setUp();
$this->requestData = Mockery::mock(RequestData::class);
$this->formValidator = Mockery::mock(CheckoutFormValidator::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->sut = new ValidateCheckoutEndpoint(
$this->requestData,
$this->formValidator,
$this->logger
);
$this->requestData->expects('read_request')->andReturn(['form' => ['field1' => 'value']]);
when('WC')->alias(function () {
return (object) ['session' => (object) $this->session];
});
2023-02-07 15:30:59 +02:00
}
public function testValid()
{
$this->formValidator->expects('validate')->once();
expect('wp_send_json_success')->once();
$this->sut->handle_request();
}
public function testInvalid()
{
$exception = new ValidationException(['Invalid value']);
$this->formValidator->expects('validate')->once()
->andThrow($exception);
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => false]);
$this->sut->handle_request();
}
public function testInvalidAndRefresh()
{
$exception = new ValidationException(['Invalid value']);
$this->formValidator->expects('validate')->once()
->andThrow($exception);
$this->session['refresh_totals'] = true;
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => true]);
2023-02-07 15:30:59 +02:00
$this->sut->handle_request();
}
public function testFailure()
{
$exception = new Exception('BOOM');
$this->formValidator->expects('validate')->once()
->andThrow($exception);
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage()]);
$this->logger->expects('error')->once();
$this->sut->handle_request();
}
}