Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API Endpoint testing response issue #419

Open
rlefever opened this issue Feb 10, 2025 · 1 comment
Open

REST API Endpoint testing response issue #419

rlefever opened this issue Feb 10, 2025 · 1 comment

Comments

@rlefever
Copy link

PHPUnit 9.6.22
ci-phpunit-test 3.0.4 via composer
php 7.4
Doc Reference

Problem: When I run this test

    /**
     * Testing retrieval of Courses via API call w/o Authorization Header
     * Should return a JSON object with message
     */
    public function test_call_courses_wo_auth_header()
    {
        $output = $this->request('GET', 'api/v1/courses');

        // Tried:
        // ob_start();
        // $this->request('GET', 'api/v1/courses');
        // $output = ob_get_clean();
        // Same result

        // Tried:
        // $output = var_export($this->request('GET', 'api/v1/courses'), TRUE);
        // Same result

        $this->assertResponseCode(400);

        $exp_json_resp = '{
            "status": "errors",
            "code": "400",
            "messages": [
                "Authorization Header Missing"
            ]
        }';
        $this->assertSame($exp_json_resp, $output);

        $arr_response = json_decode($output);

        $this->assertIsArray($arr_response);

        $this->assertArrayHasKey('status', $arr_response);
        $this->assertArrayHasKey('code', $arr_response);
        $this->assertArrayHasKey('messages', $arr_response);

        $this->assertSame('errors', $arr_response['status']);
        $this->assertSame('400', $arr_response['code']);
        $this->assertIsArray($arr_response, $arr_response['messages']);

        $this->assertSame('Authorization Header Missing', $arr_response['messages'][0]);
    }

I get this response and no other tests run
PHPUnit 9.6.22 by Sebastian Bergmann and contributors.

Warning: No code coverage driver available

{
"status": "errors",
"code": "400",
"messages": [
"Authorization Header Missing"
]
}

It is like the response is not going to the variable but directly to stdout. The same thing happens when I include the --stderr switch in the cli call.

$this->request() calls to other controllers seem to be working fine.

I don't know if this is a ci-phpunit-test issue or something I am doing wrong. I don't understand why a request returning a JSON string returns different from returning an HTML page.

Any assistance is appreciated.

@rlefever rlefever changed the title REEST API Endpoint testing REST API Endpoint testing response issue Feb 10, 2025
@rlefever
Copy link
Author

rlefever commented Feb 10, 2025

Example

I was able to get the rest of the tests to run by adding

set_is_cli(FALSE);
$this->warningOff();

at the top and reversing it at the bottom of the test

$this->warningOn();
set_is_cli(TRUE);

Still shows an error, but at least the other tests run

  1. Api_controller_test::test_call_courses_wo_auth_header
    RuntimeException: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time on line 303 in file /web/sys-3.1.13/libraries/Session/Session.php

/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:180
/web/sys-3.1.13/libraries/Session/Session.php:303
/web/sys-3.1.13/libraries/Session/Session.php:103
/web/app-v1/tests/_ci_phpunit_test/replacing/core/Loader.php:1352
/web/app-v1/tests/_ci_phpunit_test/replacing/core/Loader.php:1243
/web/app-v1/tests/_ci_phpunit_test/replacing/core/Loader.php:1098
/web/app-v1/tests/_ci_phpunit_test/replacing/core/Loader.php:1144
/web/app-v1/tests/_ci_phpunit_test/replacing/core/Loader.php:227
/web/app-v1/core/MY_Controller.php:116
/web/app-v1/core/MY_Controller.php:633
/web/app-v1/controllers/api/v1/Courses.php:31
/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:348
/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:297
/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:160
/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:155
/web/app-v1/tests/controllers/Api_Controller_test.php:31

Using this

        try{
            $output = $this->request('GET', 'api/v1/courses');
        }catch(CIPHPUnitTestExitException $e){
            $output = ob_get_clean();
        }catch(Exception $e){
            $output = ob_get_clean();
        }

I get this in cli (again other tests ran):
There was 1 error:

  1. Api_controller_test::test_call_courses_wo_auth_header
    LogicException: Status code is not set. You must call $this->request() first

/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:403
/web/app-v1/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:311
/web/app-v1/tests/controllers/Api_Controller_test.php:50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant