|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WooCommerce\PayPalCommerce\Webhooks; |
| 6 | + |
| 7 | +use Mockery; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use WooCommerce\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint; |
| 10 | +use WooCommerce\PayPalCommerce\ApiClient\Entity\WebhookEvent; |
| 11 | +use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookEventFactory; |
| 12 | +use WooCommerce\PayPalCommerce\TestCase; |
| 13 | +use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException; |
| 14 | +use WooCommerce\PayPalCommerce\Webhooks\Handler\RequestHandler; |
| 15 | +use WooCommerce\PayPalCommerce\Webhooks\Status\WebhookSimulation; |
| 16 | +use WP_REST_Request; |
| 17 | +use WP_REST_Response; |
| 18 | + |
| 19 | +/** |
| 20 | + * @covers \WooCommerce\PayPalCommerce\Webhooks\IncomingWebhookEndpoint |
| 21 | + */ |
| 22 | +class IncomingWebhookEndpointTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var WebhookEventFactory&Mockery\MockInterface */ |
| 25 | + private $webhook_event_factory; |
| 26 | + |
| 27 | + /** @var WebhookSimulation&Mockery\MockInterface */ |
| 28 | + private $simulation; |
| 29 | + |
| 30 | + /** @var WebhookEventStorage&Mockery\MockInterface */ |
| 31 | + private $last_webhook_event_storage; |
| 32 | + |
| 33 | + /** @var LoggerInterface&Mockery\MockInterface */ |
| 34 | + private $logger; |
| 35 | + |
| 36 | + public function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->webhook_event_factory = Mockery::mock(WebhookEventFactory::class); |
| 41 | + $this->simulation = Mockery::mock(WebhookSimulation::class); |
| 42 | + $this->last_webhook_event_storage = Mockery::mock(WebhookEventStorage::class); |
| 43 | + $this->logger = Mockery::mock(LoggerInterface::class); |
| 44 | + |
| 45 | + $this->logger->shouldReceive('debug'); |
| 46 | + $this->logger->shouldReceive('info'); |
| 47 | + |
| 48 | + $this->last_webhook_event_storage->shouldReceive('save'); |
| 49 | + $this->simulation->shouldReceive('is_simulation_event')->andReturn(false); |
| 50 | + } |
| 51 | + |
| 52 | + private function createRequest(): WP_REST_Request |
| 53 | + { |
| 54 | + /** @var WP_REST_Request&Mockery\MockInterface $request */ |
| 55 | + $request = Mockery::mock('WP_REST_Request, ArrayAccess'); |
| 56 | + $request->allows('get_params')->andReturn([]); |
| 57 | + $request->allows('offsetExists')->andReturn(true); |
| 58 | + $request->allows('offsetGet')->with('event_type')->andReturn('CHECKOUT.ORDER.APPROVED'); |
| 59 | + |
| 60 | + return $request; |
| 61 | + } |
| 62 | + |
| 63 | + private function createEndpoint(RequestHandler ...$handlers): IncomingWebhookEndpoint |
| 64 | + { |
| 65 | + $webhook_endpoint = Mockery::mock(WebhookEndpoint::class); |
| 66 | + |
| 67 | + return new IncomingWebhookEndpoint( |
| 68 | + $webhook_endpoint, |
| 69 | + null, |
| 70 | + $this->logger, |
| 71 | + false, |
| 72 | + $this->webhook_event_factory, |
| 73 | + $this->simulation, |
| 74 | + $this->last_webhook_event_storage, |
| 75 | + ...$handlers |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @scenario When a handler's handle_request() throws an exception that is not a |
| 81 | + * RuntimeException (e.g. PayPalOrderMissingException, which extends the plain |
| 82 | + * \Exception class), the exception must not propagate uncaught. Previously this |
| 83 | + * caused an uncaught fatal (translating to an HTTP 500 with no framework-level |
| 84 | + * catch), which made PayPal retry the webhook indefinitely. |
| 85 | + */ |
| 86 | + public function test_unexpected_exception_from_handler_does_not_propagate_uncaught(): void |
| 87 | + { |
| 88 | + $event = new WebhookEvent('evt-1', null, 'checkout-order', '1.0', 'CHECKOUT.ORDER.APPROVED', '', '', (object) []); |
| 89 | + $this->webhook_event_factory->shouldReceive('from_array')->andReturn($event); |
| 90 | + |
| 91 | + $handler = Mockery::mock(RequestHandler::class); |
| 92 | + $handler->shouldReceive('responsible_for_request')->andReturn(true); |
| 93 | + $handler->shouldReceive('event_types')->andReturn(['CHECKOUT.ORDER.APPROVED']); |
| 94 | + $handler->shouldReceive('handle_request')->andThrow( |
| 95 | + new PayPalOrderMissingException('There was an error processing your order.') |
| 96 | + ); |
| 97 | + |
| 98 | + $this->logger->shouldReceive('error')->once(); |
| 99 | + |
| 100 | + $sut = $this->createEndpoint($handler); |
| 101 | + |
| 102 | + $response = $sut->handle_request($this->createRequest()); |
| 103 | + |
| 104 | + $this->assertInstanceOf(WP_REST_Response::class, $response); |
| 105 | + $this->assertSame(200, $response->get_status()); |
| 106 | + $this->assertFalse($response->get_data()['success']); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @scenario The happy path (handler returns a response normally) must keep working |
| 111 | + * unchanged after wrapping handle_request() in a try/catch. |
| 112 | + */ |
| 113 | + public function test_handler_response_is_returned_unchanged_on_success(): void |
| 114 | + { |
| 115 | + $event = new WebhookEvent('evt-1', null, 'checkout-order', '1.0', 'CHECKOUT.ORDER.APPROVED', '', '', (object) []); |
| 116 | + $this->webhook_event_factory->shouldReceive('from_array')->andReturn($event); |
| 117 | + |
| 118 | + $expected_response = new WP_REST_Response(['success' => true]); |
| 119 | + |
| 120 | + $handler = Mockery::mock(RequestHandler::class); |
| 121 | + $handler->shouldReceive('responsible_for_request')->andReturn(true); |
| 122 | + $handler->shouldReceive('event_types')->andReturn(['CHECKOUT.ORDER.APPROVED']); |
| 123 | + $handler->shouldReceive('handle_request')->andReturn($expected_response); |
| 124 | + |
| 125 | + $this->logger->shouldNotReceive('error'); |
| 126 | + |
| 127 | + $sut = $this->createEndpoint($handler); |
| 128 | + |
| 129 | + $response = $sut->handle_request($this->createRequest()); |
| 130 | + |
| 131 | + $this->assertSame($expected_response, $response); |
| 132 | + } |
| 133 | +} |
0 commit comments