我有一个课程如下
class AccountsProcessor{
protected $remoteAccountData = [];
/**
* Process the data passed as an input array
*/
public function process($inputCsv): array
{
$this->loadRemoteData($inputCsv);
return $this->remoteAccountData;
}
/**
* Given a data retrieved from Local CSV file, iterate each account, retrieve status info from server
* and save result to instance variable array remoteAccountData
*
* @param $inputCsv
*/
protected function loadRemoteData($inputCsv)
{
foreach ($inputCsv as $account)
{
// Lookup status data on remote server for this account and add it to RemoteAccountData Array
$this->remoteAccountData["{$account[0]}"]
= $this->CallApi("GET", "http://examplurl.com/v1/accounts/{$account[0]}");
}
}
/**
* Curl call to Remote server to retrieve missing status data on each account
*
* @param $method
* @param $url
* @param bool $data
* @return mixed
*/
private function CallAPI($method, $url, $data = false)
{
..... internal code ...
}
}
该类有一个公共进程方法,它接受一个数组并将其传递给一个受保护的函数,该函数迭代数组中的每个帐户并使用CURL调用外部API
我的问题是,当我对这个类进行单元测试时,我只想测试进程方法,因为这是唯一的公共方法,其他方法是私有的或受保护的 .
我可以轻松地这样做:
protected $processor;
protected function setUp()
{
$this->processor = new AccountsProcessor();
}
/** @test */
public function it_works_with_correctly_formatted_data()
{
$inputCsv = [
["12345", "Beedge", "Kevin", "5/24/16"],
["8172", "Joe", "Bloggs", "1/1/12"]
];
$processedData = $this->processor->process($inputCsv);
$this->assertEquals("good", $processedData[0][3]);
}
但运行此测试实际上会调用外部API
如何从私有函数CallAPI()模拟该调用?