woocommerce-paypal-payments/tests/PHPUnit/Button/Endpoint/ValidateCheckoutEndpointTest.php
Alex P 3c557907f2
Refresh checkout totals after validation if needed
Refreshing the totals the same way as WC does this.
2023-04-04 11:35:16 +03:00

96 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
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;
class ValidateCheckoutEndpointTest extends TestCase
{
use MockeryPHPUnitIntegration;
private $requestData;
private $formValidator;
private $logger;
private $sut;
private $session = [];
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];
});
}
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]);
$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();
}
}