PHPUnit/Mockery - Partially mock a function

Viewed 30

Say i have this function. In this function i create a Mollie payment using the Mollie Laravel API Wrapper. The function $molliePayment needs to be mocked as i don't want to call the API during testing. How can i achieve this?

 public static function create(string $price, string $description, string $redirectUrl, array $metadata)
    {
        $molliePayment = \Mollie\Laravel\Facades\Mollie::api()->payments->create([
            'amount' => [
                'currency' => 'EUR',
                'value' => number_format((float) $price, 2, '.', ''),
            ],
        ]);

        $payment->update(['mollie_id' => $molliePayment->id]);

        // redirect customer to Mollie checkout page
        redirect($molliePayment->getCheckoutUrl(), 303);

        return ['status' => 200];
    }

How can i mock this API call?

\Mollie\Laravel\Facades\Mollie::api()->payments->create([])

This is my test:

test('Mollie creates new payment', function ($order, $mollie) {

    // Mock Mollie API here

    // Call binded Mollie class
    (new App\Services\MollieService())->create(
        '10',
        'Test payment',
        'https://google.nl',
        ['team' => $order['team']]
    );

    $this->assertDatabaseHas('payments', [
        'price' => $mollie['amount']['value'],
    ]);

})->with('order', 'mollie');

Edit:

I've tried Mocking the facade using:

// Mock Mollie API here
   \Mollie\Laravel\Facades\Mollie::shouldReceive('api')->once()->andReturnSelf();

But when i do, i get the error:

Undefined property: Mockery_2_Mollie_Laravel_MollieManager::$payments
0 Answers
Related