使用Promise时的Php单元测试
我使用AWS SDK的Promise将许多文件保存到S3 bucket中。我无法为此构建单元测试 我的职能使用Promise时的Php单元测试,php,promise,phpunit,Php,Promise,Phpunit,我使用AWS SDK的Promise将许多文件保存到S3 bucket中。我无法为此构建单元测试 我的职能 function save($item, array $layers) { $promises = []; foreach ($layers as $key => $layer) { $filename = $this->getS3Filename($item, $key); $promise = $this->s3Clie
function save($item, array $layers)
{
$promises = [];
foreach ($layers as $key => $layer) {
$filename = $this->getS3Filename($item, $key);
$promise = $this->s3Client->uploadAsync(
);
$promise = $promise->then(
function () {
},
function ($reason) use ($item) {
$this->logger->error("The promise to store file on S3 was rejected with {$reason} for item {$item->getId()}");
}
);
$promises[] = $promise;
}
all($promises)->wait();
}
我得到以下错误:
GuzzleHttp\Promise\RejectionException:承诺已被拒绝
原因:调用等待回调无法解析承诺
/Users/sela/PhpstormProjects/ziggy/vendor/guzzlehttp/promises/src/functions.php:112
/Users/sela/PhpstormProjects/ziggy/vendor/guzzlehttp/promises/src/Promise.php:75
运行myphpunit时
以下是我的单元测试代码:
protected function setUp()
{
$this->logger = $this->createMock(LoggerInterface::class);
parent::setUp();
}
public function testSaveBatch()
{
$this->logger
->expects($this->once())
->method('error');
$this->s3Client = $this->getMockBuilder(S3ClientInterface::class)
->setMethods(['uploadAsync'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$promise = $this->createMock(PromiseInterface::class);
$promise
->expects($this->once())
->method('then')
->will($this->returnCallback([$this, 'promiseCallback']));
$this->s3Client
->expects($this->once())
->method('uploadAsync')
->willReturn(
$promise
);
$object = new Object(
);
$layers = ['123' => ''];
$object->save($item, $layers);
}
/**
* @return null|MockObject
*/
public function promiseCallback()
{
$promise = $this->createMock(PromiseInterface::class);
$fulfilledPromise = $this->createMock(FulfilledPromise::class);
$fulfilledPromise
->expects($this->once())
->method('resolve')
->will($this->returnCallback($promise))
;
return $fulfilledPromise;
}