3

I am writing a unit test for an API that I am developing. The API is written in the Codeigniter framework, that calls another API using Guzzle. The test I am writing verifies that the API call returns the correct response.

The Test.php file contains the following code

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);
    }


}

The method that is being tested is as follows

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;
    }

}

The API call works correctly via Postman and from a browser, but the result of running PHPUnit from the command line is the following

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

I have googled the error message and came across this SO post Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

Making these changes has made no difference. It's worth noting this is on localhost, running MAMP.

Any ideas?

Thanks

6
  • are you overriding your URL with local dns ? Commented Jun 10, 2015 at 17:21
  • No, just standard MAMP setup, running on localhost Commented Jun 10, 2015 at 17:59
  • 1
    Sometime the CLI use a different php.ini than Apache, so your settings made through the WAMP menu don't apply to CLI.Check if the correct extension are loaded launching the command php -i | grep ssl. In the same manner you can locate the php.ini script: php -i | grep ini Commented Jun 11, 2015 at 5:25
  • 1
    Hi Matteo, that was the problem, the CLI was loading the php.ini from the C:\Windows directory. Thanks so much for your help. Please feel free to add this as the answer and I'll accept. Commented Jun 11, 2015 at 8:30
  • 1
    ahahah!!! Hi @Pete sorry ... Commented Aug 28, 2015 at 10:27

1 Answer 1

4

Sometime the CLI use a different php.ini than Apache, so your settings made through the WAMP menu don't apply to CLI.

Check if the correct extension are loaded launching the

command php -i | grep ssl 

In the same manner you can locate the php.ini script:

php -i | grep ini

hope this help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.