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(); } }