samedi 6 février 2016

How do you mock method calls called inside a closure in php?

Consider the following two methods from a class:

public function getItemHistoryForRegion() {

    $pool = new Pool($this->client, $this->createdRequests, $this->getOptions());

    $promise = $pool->promise();
    $promise->wait();
}

protected function getOptions() {
    return [
        'concurrency' => 18,
        'fulfilled'   => function ($response, $index) {
            $this->eveLogHandler->responseLog($response, 'eve_online_region_item_history_responses.log');
            $responseJson = json_decode($response->getBody()->getContents());
            $this->acceptedResponses[$index] = $responseJson;

            $this->populateHistoricalDataContainer($acceptedResponses, $this->regionAndItem);
        },
        'rejected'    => function ($reason, $index) use(&$rejectedResponses)  {
            $this->eveLogHandler->messageLog($reason, 'eve_online_region_item_history_rejected_responses.log');
        },
    ];
}

Now I have a mock in place to handle the $this->eveLogHandler->responseLog($response, 'eve_online_region_item_history_responses.log');

When its not called inside a closure method:

public function getLogMock() {
    return $this->getMockBuilder('EveOnline\Logging\EveLogHandler')
                ->getMock();
}

What I am wondering is how do you mock something thats inside a closure method? Should I inject it via the arguments and then mock the arguments for the method?

The error I get in my test is:

Error: Call to undefined method Mock_EveLogHandler_53e478bc::responseLog()

Aucun commentaire:

Enregistrer un commentaire