Welcome Guest, Not a member yet? Register   Sign In
Issue with HTTP Feature Testing

(This post was last modified: 05-26-2021, 11:58 AM by stopz.)

I'll try to be as short with this as i can: so I have a test file with contents:

PHP Code:

namespace MyName\MyPackage;


EndpointTest extends FeatureTestCase
    use DatabaseTestTrait;
    use FeatureTestTrait;

    protected $namespace 'MyName\MyPackage';
    protected $basePath VENDORPATH 'myname/mypackage/src/Database';

    public function testEndpoints()
        $resp $this->get('mycontroller/amethod');
        $resp->assertStatus(200'Endpoint responds.'); // works

        $resp $this->get('mycontroller/amethod', [
            'token' => 'some-token'

        // i have also tried:
        // $resp = $this->get('mycontroller/amethod?token=some-token'); // same exact result as above.


Within the controller i have a simple check right now just to get parsed query string array:

PHP Code:

namespace MyName\MyPackage\Controllers;


Mycontroller extends BaseController
    public function amethod()
        if (! isset($_GET['token']))

            die(); // this triggers, thus i get no 'token' from my test.

Now i run tests via composer run-script test and results are:

Quote:Array() // this is the $_GET array.

object(CodeIgniter\HTTP\IncomingRequest)#103 (20) {
  ["enableCSRF":protected]=> bool(false)
  ["uri"]=> object(CodeIgniter\HTTP\URI)#105 (14) {
    ["uriString":protected]=> NULL
    ["segments":protected]=> array(2) {
      [0]=> string(8) "mycontroller"
      [1]=> string(7) "amethod"
    ["scheme":protected]=> string(5) "https"
    ["user":protected]=> NULL
    ["password":protected]=> NULL
    ["host":protected]=> string(19) "localhost"
    ["port":protected]=> NULL
    ["path":protected]=> string(16) "mycontroller/amethod"
    ["fragment":protected]=> string(0) ""
    ["query":protected]=> array(0) {} // An empty query was sent?

Any ideas what i should try or is there a thing that you see right away that is wrong with my code? Any help Helps!

(This post was last modified: 05-27-2021, 03:59 AM by stopz.)

Alright i found an aswer for my own question. I think it has to do with

Quote:The $params array does not make sense for every HTTP verb, but is included for consistency.

as written in the manual that not all request types support $params. Maybe GET is one of them. But for sake of consistency i wrote my own tiny solution to request endpoints in testing:

PHP Code:
 * Request endpoint.
 * @method req
public function req(string $type 'GET'string $url '', array $params = []):object
    # Prepare parameters & type.
    $params http_build_query($params);
    $type  strtoupper($type);

    # Base url with get parameters.
    $base_url rtrim(config('app')->baseURL'/') . '/' ltrim($url'/') . (
        ($type == 'GET')
            '?' $params
    $contents file_get_contents($base_urlcontextstream_context_create([
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false
        'http' => [
            'method'  => $type,
            'header'  => 'Content-type: application/x-www-form-urlencoded' "\r\n" .
                        'Content-Length: ' strlen($params) . "\r\n",
            'content' => $params,
            'ignore_errors' => true

    # http headers.
    $httphead $http_response_header;
    $head = [];

    # Assoc headers.
    foreach ($httphead as $n => $value)
        $explode explode(':'$value);
        $key = ($n == || count($explode) == 1)
strtolower(reset($explode)) ;

        # Set header key and value.
        $head[$key] = $value;

    # Proper status and url.
    $head['url']    $base_url;
    $head['status'] = $head[0];

    # Remove numeric status representation.

    # Extract response code.
    $head['code'] = (int) explode(' '$head['status'])[1];

    # Return headers.
    return (object) [
        'header' => (object) $head,
        'body' => $contents

What helped alot is that CodeIgniter actually got my request by:

PHP Code:

Hope this helps someone!

Thanks for sharing your solution! Just to add: FeatureTestCase does not necessarily populate the superglobals (like $_GET) nor recreate the full URI. I think you *should* still be able to access the parameters from the Request though - try this instead:
PHP Code:
public function amethod()
        if (! $token $this->request->getVar('token'))

            die(); // this triggers, thus i get no 'token' from my test.

Theme © iAndrew 2016 - Forum software by © MyBB