【发布时间】:2020-08-05 02:56:24
【问题描述】:
我正在用 Laravel 编写测试。 但是,我遇到了麻烦,因为我不知道如何测试。 有一种方法可以发出 http 请求,如下所示。 您通常如何测试这种方法? 我应该使用实际可访问的 URL 还是 mock?
PHP 7.4.6 Laravel 7.0
<?php
namespace App\Model;
use Illuminate\Support\Facades\Http;
use Exception;
class Hoge
{
public function getText(string $url, ?string $user, ?string $password, string $ua): bool
{
$header = ["User-Agent" => $ua];
$httpObject = $user && $password ? Http::withBasicAuth($user, $password)->withHeaders($header) : Http::withHeaders($header);
try {
$response = $httpObject->get($url);
if ($response->ok()) {
return $response->body();
}
} catch (Exception $e) {
return false;
}
return false;
}
}
【问题讨论】:
标签: php laravel unit-testing phpunit