我有一个特定外部 API 的包装器,它是这样构造的(它是 PHP8 语法思想):
public function __construct(private HttpClientInterface $httpClient, private LoggerInterface $logger, private string $url, private string $token) {}
所以这个类的规范是这样构造的:
private static string $token = 'token';
private static string $url = 'url';
public function let(HttpClientInterface $httpClient, LoggerInterface $logger): void
{
$this->beConstructedWith($httpClient, $logger, self::$url, self::$token);
}
我在它使用那些静态方法进行调用时对其进行调试,所以它似乎正确地模拟了构造函数。
这个 API 客户端类有这样一个方法(简化):
public function getSurveys(): array
{
$surveysResponse = $this->httpClient->request(
'GET',
$this->url . 'getSurveysUrl',
[
'headers' => [
'x-access-token' => 'token',
]
]);
$targetingResponses = [];
$quotaResponses = [];
$surveys = [];
foreach($surveysResponse as $survey) {
$surveyObject = Survey::fromRespone(survey);
$targetingResponses[$surveyObject->getIdentifier()] = $this->httpClient->request(
'GET',
$this->url . 'getTargetingsUrl' . $surveyObject->getIdentifier(),
[
'headers' => [
'x-access-token' => 'token',
]
]);
$quotaResponses[$surveyObject->getIdentifier()] = $this->httpClient->request(
'GET',
$this->url . 'getQuotasUrl' . $surveyObject->getIdentifier(),
[
'headers' => [
'x-access-token' => 'token',
]
]);
$surveys[] = surveyObject;
}
// those calls for targeting and quota async so now we need to get actuall responses
foreach ($surveys as $survey) {
$targetingResponse = $targetingResponses[$survey->getIdentifier()])->toArray();
$quotaResponse = $quotaResponses[$survey->getIdentifier()])->toArray();
$survey->assoicateTargetings(new Targetings(targetingResponse));
$survey->assoicateQuotas(new Quotas(quotaResponse))
}
return $surveys;
}
我想为此写一个规范
public function it_should_return_all_surveys()
{
// make a call to get surveys, in this case only 1 survey
$surveysResponse = [
'apiStatus' => 'success',
'result' => [
(new InnovateSurveyArray())->build(),
]
];
$surveyId = surveysResponse['result'][0]['surveyId'];
$response->toArray()->shouldBeCalled()->willReturn($surveysResponse);
$httpClient->request(
'GET',
self::$url . 'getSurveysUrl',
[
'headers' => [
'x-access-token' => self::$token,
]
]
)->willReturn($response);
// make a call to get targetings
$targetingResponse = []; //response here, I omit it as it's not not important here
$response->toArray()->shouldBeCalled()->willReturn($targetingResponse);
$httpClient->request(
'GET',
self::$url . 'getTargetingsUrl' . $surveyId,
[
'headers' => [
'x-access-token' => self::$token,
]
]
)->willReturn($response);
// make call to get quotas
$targetingResponse = []; //response here, I omit it as it's not not important here
$response->toArray()->shouldBeCalled()->willReturn($quotaResponse);
$httpClient->request(
'GET',
self::$url . 'getQuotasUrl' . $surveyId,
[
'headers' => [
'x-access-token' => self::$token,
]
]
)->willReturn($response);
$survey = Survey::fromRespone($surveysResponse['result'][0]);
$survey->associateQuotas(new Quotas($quotaResponse['result']));
$survey->associateTargeting(new Targetings($targetingResponse['result']));
$this->getSurveys()->shouldReturn([$survey]);
}
问题是规范制作getQuotasUrl
是第一次调用而不是制作getSurveysUrl
,我做错了什么?或者也许预言不考虑间谍输入参数并总是使用最后一个?
提前致谢!