Skip to content

Bug: Running Feature Tests on multiple endpoints using filters #3085

Closed
@dcaswel

Description

@dcaswel

Describe the bug
I have a Feature Test class with multiple tests on the same endpoint. This endpoint uses a filter to handle authentication which then adds the authenticated user to the request so it can be used in the controller. When I run the tests individually, it works. However, if I try to run all of the tests in the test class, it gives me an error when trying to access the authenticated user that has been added to the request.

After doing A LOT of digging, I think I have found the issue. The filters functionality alters the request object by altering $this->request in the Filters object. This works most of the time because that property points to the same object as the request that later gets passed to the controller. However, in the use case that I described above, they do not. This is because in Codeigniter::handleRequest() when it gets the filters object using Services::filters(), it is getting a shared instance of Filters which already exists when the second test runs. So it gets back a Filters instance whose request parameter no longer points to the same request as what gets passed to the controller.

A simple solution to this would be to alter Codeigniter::handleRequest() so that when it finishes running the filters, after checking to see if $possibleRedirect is a response, you add this:

if ($possibleRedirect instanceof RequestInterface)
{
    $this->request = $possibleRedirect;
}    

CodeIgniter 4 version
v4.0.3

Affected module(s)

  • CodeIgniter\CodeIgniter
  • CodeIgniter\Filters\Filters

Expected behavior, and steps to reproduce if appropriate

  1. Create a Feature Test class with 2 tests that are testing the same endpoint
  2. Create a filter and register it to run on both endpoints. (In mine the filter is in general so it runs everywhere)
  3. In your before() on the filter, add a new property to the request and return the request.
  4. In your controller endpoint, do something to access the property that you added.
  5. Run all the tests in the Feature Test class.
  6. This should work but currently it will throw an error saying that the new property doesn't exist on the request object.

Context

  • OS: CentOs7
  • Web server: Apache 2.4.6
  • PHP version: 7.2.24

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions