目标:使用TDD测试与Fungku的Hubspot库一起工作的代码(https://packagist.org/packages/fungku/hubspot-php)向Hubspot提交订阅信息。
其中:在Mac上的流浪虚拟机中运行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()
碰巧说,我想回报它是成功的。或者它失败了,并测试我的代码来处理它。
我错过了什么?如有任何建议或帮助,我们将不胜感激!
感谢
这是一个众所周知的问题。您可以参考此问题:https://github.com/padraic/mockery/issues/263
正如您所看到的,HubSpotService
类中的__call()
方法采用了一个导致问题的可选参数。因此,对于PHP,__call
方法的两个参数都是强制性的。
唯一的解决方案是为HubSpot创建一个适当的PR来解决核心问题。否则,您将无法模拟此类。