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

时间:2015-06-10 16:41:51

标签: php codeigniter phpunit guzzle

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

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:创建资源时出错:[message] fopen():无法使用   找到包装“https” - 你忘了启用它吗?   配置PHP?

     

[message] fopen(https://api.breathehr.com/v1/employees?page=1):失败了   打开流:没有这样的文件或目录

我用Google搜索了错误消息并发现了这篇SO Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

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

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:4)

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

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

command php -i | grep ssl 

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

php -i | grep ini

希望这个帮助