ZMChris
3 weeks ago

Laravel 7 HTTP test with a Mock Object

Posted 3 weeks ago by ZMChris

Hi there,

We are struggling to see how we might be able to test a payment platform (it's an SDK written by the provider themselves and it uses their own built cURL library which we cannot tap into (e.g., with Http::fake()). We are unsure how we can combine mocking an object with HTTP tests. We would like to do something like:

public function test_That_We_Get_An Unsuccessful_Authorization()
{
    $response = $this->postJson('/auth/payment', [
            'creditCardNumber' => '1234 5678 8765 4321',
            'cvv' => '123',
            'currency' => 'USD',
            'amount' => 500
    ]);

    $this->assertEquals('Unauthorized', $response['message']);
}

public function test_That_We_Get_A_Successful_Authorization()
{
    $response = $this->postJson('/auth/payment', [
            'creditCardNumber' => '1234 5678 8765 4321',
            'cvv' => '321',
            'currency' => 'USD',
            'amount' => 500
    ]);

    $this->assertEquals('Authorized', $response['message']);
}

How might we go about, in the below Controller example, of

class Pay
{
    private function getPaymentClient()
    {
        return new PaymentProcessor(config('app.payment_processor'));
    }

    public function authorize()
    {
        $response = $this->getPaymentClient()->authorize(
            request()->post('creditCardNumber),
            request()->post('cvv),
            request()->post('currency),
            request()->post('amount)
        );

        // Do something with the response
    }

    public function capture()
    {
        $response = $this->getPaymentClient()->capture(
            request()->post('authReference),
            request()->post('currency),
            request()->post('amount
        );

        // Do something with the response
    }
}

Please sign in or create an account to participate in this conversation.