使用 PHPUnit 测试 Guzzle 调用时无法找到包装器

Unable to find wrapper when testing Guzzle call with PHPUnit

我正在为我正在开发的 API 编写单元测试。 API 是在 Codeigniter 框架中编写的,它使用 Guzzle 调用另一个 API。我正在编写的测试验证 API 调用 returns 正确的响应。

Test.php文件包含以下代码

require '/application/libraries/apiWrappers/Breathehr.php';

class BreathehrTest extends PHPUnit_Framework_TestCase {

    public function testCanReturnEmployeeArray() {
        $breatheHR = new Breathehr();

        $employees = $breatheHR->list_employees(1);
        $this->assertArrayHasKey('employees', $employees);
    }


}

正在测试的方法如下

class Breathehr {

    function __construct() {

    }

    public function list_employees($page)
    {
        $client = new GuzzleHttp\Client(
            ['base_uri' => 'https://xxx/',
                'headers' => ['X-API-KEY' => 'xxx'],
                'verify' => false]
        );

        $request = $client->get('employees?page='.$page);
        $employees = json_decode($request->getBody(true));

        $employeeData = array(
            'employees' => array(),
            'pagination' => array()
        );

        $i = 0;
        foreach($employees->employees as $employee) {
            if($employee->status !== 'Ex-employee') {
                $employeeData['employees'][$i]['firstName'] = $employee->first_name;
                $employeeData['employees'][$i]['lastName'] = $employee->last_name;
                $employeeData['employees'][$i]['jobTitle'] = $employee->job_title;
                if(isset($employee->line_manager)) {
                    $employeeData['employees'][$i]['lineManagerName'] = $employee->line_manager->first_name . ' '. $employee->line_manager->last_name;
                    $employeeData['employees'][$i]['lineManagerID'] = $employee->line_manager->id;
                }
                $employeeData['employees'][$i]['workingHours'] = $employee->full_or_part_time;
                $employeeData['employees'][$i]['email'] = $employee->email;
                $employeeData['employees'][$i]['workPhone'] = $employee->ddi;
                $employeeData['employees'][$i]['personalMobile'] = $employee->personal_mobile;
                $employeeData['employees'][$i]['homeTelephone'] = $employee->home_telephone;
                $employeeData['employees'][$i]['birthday'] = $employee->dob;
                $i++;
            }
        }

        $nextLink = $request->getHeader('Link');
        $nextLinkSplit = explode(',', $nextLink[0]);

        $pageination = array();

        foreach($nextLinkSplit as $data) {
            $split = explode(';', $data);
            preg_match('/"(.*?)"/', $split[1], $keyMatch);
            $key = isset($keyMatch[1]) ? $keyMatch[1] : FALSE;
            $number = substr($split[0], -2, 1);

            $pageination[$key] = $number;
        }

        array_push($employeeData['pagination'], $pageination);

        return $employeeData;
    }

}

API 调用可以通过 Postman 和浏览器正常工作,但是 运行 PHPUnit 从命令行的结果如下

RuntimeException: Error creating resource: [message] fopen(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

[message] fopen(https://api.breathehr.com/v1/employees?page=1): failed to open stream: No such file or directory

我用谷歌搜索了错误消息并发现了这个 SO post

进行这些更改没有任何区别。值得注意的是,这是在本地主机上,运行 MAMP。

有什么想法吗?

谢谢

有时 CLI 使用与 Apache 不同的 php.ini,因此您通过 WAMP 菜单进行的设置不适用于 CLI。

检查是否加载了正确的扩展启动

command php -i | grep ssl 

您可以用同样的方式找到 php.ini 脚本:

php -i | grep ini

希望对您有所帮助