php - PHPUnit testing, simulate call to external API -
i have class follows
class accountsprocessor{ protected $remoteaccountdata = []; /** * process data passed input array */ public function process($inputcsv): array { $this->loadremotedata($inputcsv); return $this->remoteaccountdata; } /** * given data retrieved local csv file, iterate each account, retrieve status info server * , save result instance variable array remoteaccountdata * * @param $inputcsv */ protected function loadremotedata($inputcsv) { foreach ($inputcsv $account) { // lookup status data on remote server account , add remoteaccountdata array $this->remoteaccountdata["{$account[0]}"] = $this->callapi("get", "http://examplurl.com/v1/accounts/{$account[0]}"); } } /** * curl call remote server 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 ... } }
the class has public process method accepts array , passes on protected function iterates each account in array , makes call external api using curl
my question this, when unit testing class want test process method public method, others private or protected.
i can so:
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]); }
but running test makes call external api
how can simulate call private function callapi() ?
Comments
Post a Comment