简体   繁体   中英

Unit testing Fungku's Hubspot API Library. Laravel 5

Goal: Use TDD to test code that works with Fungku's Hubspot Library ( https://packagist.org/packages/fungku/hubspot-php ) to submit subscription info to Hubspot.

Where: Running Laravel 5.1 inside vagrant VM on a Mac.

What: On a final checkout page, there is a subscribe checkbox. If that is checked we want the already entered and validated customer information to be sent to hubspot. I have setup a class that will be a middleman between Fungkus library and our code:

namespace app\Classes;

use App\Models\CountriesQuery;
use Fungku\HubSpot\HubSpotService;

class Hubspot
{
    private $hubspotOBJ;

    public function __construct() {
        $this->hubspotOBJ = HubSpotService::make();
    }

    public function subscribeCustomer($customerEmail, $customerArray)     {
        $result = $this->hubspotOBJ->contacts()->createOrUpdate($customerEmail, $customerArray);

        if($result->getStatusCode() == 200){
            return true;
        }
        return false;
    }
}

The arguments passed to the function look like:

$customerEmail = "test@test.com"

$customerArray = array(
    array('property' => 'email', 'value' => $customerEmail),
    array('property' => 'firstname', 'value' => "FirstName"),
    array('property' => 'lastname', 'value' => "LastName"),
    array('property' => 'phone', 'value' => "1234567890"),
    array('property' => 'mobilephone', 'value' => "9876543210"),
    array('property' => 'fax', 'value' => "1112223456"),
    array('property' => 'address', 'value' => "123 Some St."),
    array('property' => 'street_address_2', 'value' => "Apt. 4"),
    array('property' => 'state', 'value' => "IL"),
    array('property' => 'city', 'value' => "City"),
    array('property' => 'zip', 'value' => "12345"),
    array('property' => 'country', 'value' => "USA"),
    array('property' => 'lifecyclestage', 'value' => "customer"),
    array('property' => 'hs_persona', 'value' => "persona_3")
);

currently, I don't even have a full test written. This:

use Illuminate\Foundation\Testing\WithoutMiddleware;

class HubspotTest extends TestCase {
    use WithoutMiddleware;

    public function tearDown() {
        Mockery::close();
    }

    /**
     * @test
     */
    function it_subscribes_new_customer()
    {
        $mock = Mockery::mock('Fungku\HubSpot\HubSpotService');
    }
}

When I run phpunit, gives me this:

1) HubspotTest::it_subscribes_new_customer
ErrorException: Declaration of Mockery_0_Fungku_HubSpot_HubSpotService::__call() should be compatible with Fungku\HubSpot\HubSpotService::__call($name, $arguments = NULL)

/vagrant/REPO/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php:34
/vagrant/REPO/vendor/mockery/mockery/library/Mockery/Container.php:231
/vagrant/REPO/vendor/mockery/mockery/library/Mockery.php:80
/vagrant/REPO/tests/HubspotTest.php:17

What is this error telling me?

As a side note. The code that sends information to hubspot is working fine. So I know everything other than the test is working as expected. Why am I getting the above error?

I know I need to mock Fungku's Library as I don't want to actually include the Hubspot API in the test. I'd like to be able to say that the call to

$this->hubspotOBJ->contacts()->createOrUpdate()

happens to say, I want to return that it was successful. Or maybe that it failed, and to test my code that handles that.

What am I missing? Any pointers or assistance would be gratefully appreciated!

Thanks

That is a well known problem. You can refer to this issue: https://github.com/padraic/mockery/issues/263

As you can see the __call() method in HubSpotService class takes an optional parameter that causes the issue. Accordingly to PHP both parameters to the __call method are mandatory.

The only solution would be to create an appropriate PR for HubSpot to fix the issue in core. Otherwise you simply won't be able to mock this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM