【发布时间】:2016-09-16 20:42:13
【问题描述】:
我正在使用 Guzzle 的异步请求,并将它们实现在我现在想测试的服务中。
我的方法是这样的(假的,所以如果不是100%有效,请见谅)
public function getPlayer(string $uiid, array &$player = [])
{
$options['query'] = ['id' => $uiid];
$promise = $this->requestAsync('GET', $this->endpoint, $options);
$promise->then(function (ResponseInterface $response) use (&$player) {
$player = $response->getBody()->getContents();
});
return $players;
}
现在我想测试它,但我真的不知道如何模拟可调用对象,因为我总是收到错误
1) tzfrs\PlayerBundle\Tests\Api\Player\PlayerServiceTest::testGetPlayer
Prophecy\Exception\InvalidArgumentException: Expected callable or instance of PromiseInterface, but got object.
这就是我目前的实现方式
/** @var ObjectProphecy|PromiseInterface $response */
$promise = $this->prophesize(PromiseInterface::class);
$promise->then()->will($this->returnCallback(function (ResponseInterface $response) use (&$player){}));
没用。还有这个
$this->returnCallback(function (ResponseInterface $response) use (&$player){})
也没有用。同样的错误。当只是尝试一个虚拟回调时
$promise->then(function(){});
我得到了错误Error: Call to a member function then() on string,即使在->reveal()首先承诺之后也是如此。有什么想法吗?
【问题讨论】:
标签: php promise phpunit guzzle prophecy