我遇到了一个不确定如何测试的场景。
我的应用程序使用官方的PHP SDK与Zoho CRM API来回通信。这是我遇到的用户类的一部分:
class App_ZCRM_User {
protected $record;
public function __construct( ZCRMRecord $record ) {
$this->record = $record;
}
public function create_default_tasks( array $tasks ) {
$records = [];
foreach( $tasks as $task ) {
$record = ZCRMRecord::getInstance( 'Tasks', null ); // Create empty ZCRMRecord
$record->setFieldValue( 'Subject', $task );
$record->setFieldValue( 'User', $this->record );
$records[] = $record;
}
$tasks = ZCRMModule::getInstance( 'Tasks' );
$insert = $tasks->createRecords( $records );
// Returns array of EntityResponse objects
return $insert->getEntityResponses();
}
}
使用联系人唯一ID的Zoho CRM记录实例化此类。
// ZCRMRecord object
$record = ZCRMRecord::getInstance( 'Contacts', 'xxxxxxxxxx' );
$user = new App_ZCRM_User( $record );
$user->create_default_tasks( array(
'Choose vehicle make',
'Choose vehicle model',
) );
create_default_tasks()
方法与Zoho API通信。
我遇到的问题是我不希望每次运行测试时都将这些记录插入CRM。
我尝试模拟ZCRMModule for Tasks,但是该类具有私有构造函数,并使用静态方法getInstance()
。在此类上使用$tasks = ZCRMModule::getInstance( 'Tasks' );
并调用$tasks->createRecords()
将产生实时API请求。
我的测试在下面,但它返回消息:
不能在模拟对象上调用静态方法“ getInstance”
public function test_create_default_tasks() {
$tasks = array(
'Task 1',
'Task 2'
);
$records = [];
foreach( $tasks as $task ) {
$record = ZCRMRecord::getInstance( 'Tasks', null );
$record->setFieldValue( 'Subject', $task );
$record->setFieldValue( 'User', $this->record );
$records[] = $record;
}
$user = new App_ZCRM_User( $this->record );
$user->create_default_tasks( $tasks ); // Does API request
/**
* I want to mock the ZCRMModule tasks and simulate the createRecords() method
*/
$tasks = $this->getMockBuilder( 'ZCRMModule' )
->disableOriginalConstructor() // Constructor is private
->getMock();
$tasks::getInstance( 'Tasks' );
$insert = $tasks->createRecords( $records ); // Method returns null
$created = $insert->getEntityResponses();
// Not sure how to assert the value here...
//$this->assertEquals( );
}
我迷失了方向……我该如何在不使用向Zoho发出实时API请求的情况下测试此方法?