FvsJson's avatar

Why does Laravel HTTP fake not work...??!!??!

I have a project im testing and its connection to Hubspot. I have a simple service that has one method that needs to create a user in hubspot.

I create a pest unit test for it but http fake is not getting intercepting the request and i cant understand WHY?! :(


    Http::fake([
        'https://api.hubapi.com/crm/v3/objects/*' =>  Http::response(
            array(
                "properties" => array(
                    "email" => "[email protected]",
                    "date_of_birth" => "1984/10/19",
                    "firstname" => "Piet1",
                    "lastname" => "Pompies1",
                    "phone" => "0822234567",
                    "registration_p1" => "Yes",
                    "registration_p2" => "Yes",
                    "membership_linked" => "Yes",
                    "membership_number" => "123456789"
                )
            )
            , 201),
    ]);

    $hubSpotService = new SNPHubSpotService();

    $result = $hubSpotService->createContact([

        'email'         => fake()->safeEmail(),
        'date_of_birth' => '1986-09-04',
        'firstname'     => fake()->name(),
        'lastname'      => fake()->lastName(),
        'phone'         => '0825912364'

    ]);

    expect($result)->toBeTrue();

});

PLEASE HELP...

0 likes
3 replies
nexxai's avatar

Can you show your SNPHubSpotService class? Do you know if it's using the HTTP facade? If not, faking the facade isn't going to do anything and you'll need to mock it another way.

FvsJson's avatar

@nexxai

<?php

namespace App\Api\Hubspot;

use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInputForCreate;
use HubSpot\Discovery\Discovery;
use HubSpot\Factory;
use Illuminate\Support\Facades\Log;

class SNPHubSpotService
{
    private $hubspot;

    public function __construct()
    {

        $this->hubspot = Factory::createWithAccessToken(config('custom.hubspot.client_token'));

    }

    /**
     * @param  array  $properties
     * @return bool
     */
    public function createContact(array $properties): bool
    {
        $contactInput = new SimplePublicObjectInputForCreate();
        $contactInput->setProperties($properties);
        try {
            $this->hubspot->crm()->contacts()->basicApi()->create($contactInput);

            $created = true;
        } catch (\Exception $exception) {

            Log::error('Failed to create hubspot user', [
                $exception->getMessage()
            ]);

            $created = false;
        }

        return $created;
    }

    /**
     * @return Discovery
     */
    public function getHubspot(): \HubSpot\Discovery\Discovery
    {
        return $this->hubspot;
    }

    /**
     * @param  Discovery  $hubspot
     * @return void
     */
    public function setHubspot(\HubSpot\Discovery\Discovery $hubspot): void
    {
        $this->hubspot = $hubspot;
    }
}

nexxai's avatar

@FvsJson Ok, you'll need to go source diving into the HubSpot package and find out how they're actually making the HTTP requests to their API. My guess is that they're just using Guzzle and so you'll need to figure out a way to just mock out their whole package/response instead of using an HTTP fake.

Please or to participate in this conversation.