#How to capture MollieApiClient requests in Laravel.

1 messages · Page 1 of 1 (latest)

lucid mist
#

Hello, i'am building a payment provider for Lunar (Headless E-Commerce for Laravel) with the mollie-php package and i'am trying to write tests without hitting te real Mollie Api. I found this package but it's still not clear how to fake the guzzle client used by MollieApiClient.

waxen lion
#

@wicked quartz

lucid mist
#

Here a snippet of my test:

<?php

namespace Pixelpillow\LunarMollie\Tests\Unit\Managers;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Payment;
use Pixelpillow\LunarMollie\Actions\SetPaymentIssuerOnCart;
use Pixelpillow\LunarMollie\Managers\MollieManager;
use Pixelpillow\LunarMollie\Tests\TestCase;
use Pixelpillow\LunarMollie\Tests\Utils\CartBuilder;

class MollieManagerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * @var \GuzzleHttp\Client
     */
    protected $guzzleClient;

    /**
     * @var MollieManager
     */
    protected $mollieManager;

    /**
     * @var MollieApiClient
     */
    protected $mollieApiClient;

    public function setUp(): void
    {
        parent::setUp();

        $this->guzzleClient = resolve(\GuzzleHttp\Client::class);
        $this->mollieManager = new MollieManager($this->guzzleClient);
        $this->mollieApiClient = app(MollieApiClient::class);
    }

    public function test_payment_intent_is_created()
    {

        $cart = CartBuilder::build();

        $payment_issuer = 'ideal_ABNANL2A';

        App::make(SetPaymentIssuerOnCart::class)($cart, $payment_issuer);

        $payment = new Payment($this->mollieApiClient);
        $payment->id = uniqid('tr_');

        Http::fake([
            'https://api.mollie.com/*' => Http::response(json_encode($payment)),
        ]);

        $this->mollieManager->createIntent($cart->calculate());

        $this->assertEquals(
            $cart->refresh()->meta->payment_intent,
            $payment->id
        );
    }
}
wicked quartz
#

Hi @lucid mist good to hear Lunar is getting some love!

lucid mist
#

🙂

wicked quartz
#

Are you using mollie-api-php or laravel-mollie?

lucid mist
#

the mollie-api-php

wicked quartz
#

Ok. So the Laravel HTTP fake facade you’re using is available on laravel-mollie. On mollie-api-php, you’d need to use a mocked Guzzle adapter. Which one do you prefer?

lucid mist
#

Ah I'am mixing code now

#

I prefer the mollie-api-php so I need to create a mocked Guzzle adapter and use that one in my mollie manager

wicked quartz
#

Great. I am diving in a bit to see what’s easiest for this case and see if I have an example

lucid mist
#

I would love to use the Http::fake functions of laravel 😏

#

I may need to switch to the Laravel-Mollie package, but this feels a bit excessive.

wicked quartz
#

Ok, so what laravel-mollie does is injecting an adapter into mollie-api-php's client, so you can use the Http facade. You could grab that adapter and use it without laravel-mollie

#

Would that be a solution for you?

lucid mist
#

Hmm let me try

wicked quartz
#

Client instantiation then becomes

$mollie = new MollieApiClient(new MollieLaravelHttpClientAdapter);
#

Feel free to tag me here if you need more help. And tell Lunar's Glenn Jacobs I said hi!

lucid mist
#

@wicked quartz Thanks yeah this is the way! Dank je wel!

wicked quartz
#

Great, enjoy! 🚀

waxen lion
#

Thanks Sander!

lucid mist
#

Do Mollie's payment providers for Ideal have capture modes like manual and automatic, like Stripe does? Or is Ideal always direct and automatic?

waxen lion
#

iDEAL is a guaranteed method. So it's captured directly 🙂

lucid mist
#

Oke 🙂

weak gulch
#

This can’t be a coincidence, I just was looking into lunar for a pet project and was looking for a mollie integration 🙂

lucid mist
#

🎉

lucid mist
#

Hi @wicked quartz, are you willing to review my Lunar-Mollie payment provider when it's ready?

wicked quartz
#

Hi @lucid mist , sure, though you may have to walk me through some Lunar specifics 🙂