【问题标题】:Unit testing Fungku's Hubspot API Library. Laravel 5单元测试 Fungku 的 Hubspot API 库。拉拉维尔 5
【发布时间】:2016-08-27 16:23:34
【问题描述】:

目标: 使用 TDD 测试与 Fungku 的 Hubspot 库 (https://packagist.org/packages/fungku/hubspot-php) 一起使用的代码,以向 Hubspot 提交订阅信息。

在哪里: 在 Mac 上的 vagrant VM 中运行 Laravel 5.1。

什么: 在最终结帐页面上,有一个订阅复选框。如果选中该选项,我们希望将已经输入和验证的客户信息发送到 hubspot。我已经设置了一个类,它将成为 Fungkus 库和我们的代码之间的中间人:

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;
    }
}

传递给函数的参数如下所示:

$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")
);

目前,我什至没有编写完整的测试。这个:

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');
    }
}

当我运行 phpunit 时,给我这个:

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

这个错误告诉我什么?

作为旁注。向 hubspot 发送信息的代码运行良好。所以我知道除了测试之外的一切都按预期工作。为什么会出现上述错误?

我知道我需要模拟 Fungku 的库,因为我不想在测试中实际包含 Hubspot API。我想说的是对

的调用
$this->hubspotOBJ->contacts()->createOrUpdate()

碰巧说,我想回报它是成功的。或者也许它失败了,并测试我处理它的代码。

我错过了什么?任何指点或帮助将不胜感激!

谢谢

【问题讨论】:

    标签: php unit-testing phpunit tdd laravel-5.1


    【解决方案1】:

    这是一个众所周知的问题。可以参考这个问题:https://github.com/padraic/mockery/issues/263

    如您所见,HubSpotService 类中的 __call() 方法采用了导致问题的可选参数。根据 PHP,__call 方法的两个参数都是必需的。

    唯一的解决方案是为 HubSpot 创建一个适当的 PR 以解决核心问题。否则你根本无法模拟这个类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2021-01-17
      • 2018-10-16
      • 2018-06-08
      • 2021-05-23
      • 2020-08-27
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多