Welcome Guest, Not a member yet? Register   Sign In
Problem with Autorouting improved and HTTP Testing
#1

(This post was last modified: 06-05-2023, 06:13 AM by massimiliano1.mancini.)

Hi,
I'm new in the forum and I hope this is the right place to request some support for the following issue.
Following the guidelines concerning autorouting improved, I have a controller with two different methods:


PHP Code:
getCreate()
postCreate() 

during testing HTTP Feature if I call

PHP Code:
$result $this->post('section/create'$dataPost); 
then the Section::getCreate method is called instead of Section::postCreate.

As workaround, I added a route in my test

PHP Code:
$routes = [
            ['post''section/create''Section::postCreate'],
        ]; 
and the using it, the test works

PHP Code:
$result $this->withRoutes($routes)->post('section/create'$dataPost); 

Am I doing something wrong? Or there is a problem with autorouting and HTTP Feature test?

Thenk you,
Massimiliano
Reply
#2

It seems there is a bug that caches HTTP verb when HTTP testing.
Reply
#3

Check https://github.com/codeigniter4/CodeIgniter4/pull/7543 if you can.
Reply
#4

I have subscribed the github issue.
Thank you Kenjis
Reply
#5

(This post was last modified: 06-19-2023, 01:04 AM by massimiliano1.mancini.)

Back to this thread. After release 4.3.6 that accepted the specific pull request to solve the verb issue, I tried to remove from my test the routes and I still have a problem.

When a test with parameters comes before a test without parameters, as in the example below:

PHP Code:
public function testUpdateButton()
    {

        $dataDB1 = ['id' => 1'description' => 'test section''intelligence' => 0];
        $this->hasInDatabase('section'$dataDB1);

        $result $this->get('section/update/1');

        $result->assertSeeElement('.modal fade');
        $result->assertSeeLink('Annulla''.btn btn-light');
        $result->assertSeeInField('description''test section');
    }

    public function testAbort()
    {
        $result $this->get('section/abort');
        $result->assertRedirectTo('section');
    
the second tests fails with this error

Code:
1) CodeIgniter\SectionTest::testAbort
CodeIgniter\Exceptions\PageNotFoundException: The param count in the URI are greater than the controller method params. Handler:\App\Controllers\Section::getAbort, URI:section/abort

What I found is that the parameter is not reset from first call to the second. The second call do not have any parameter but parameter still has "1" in first position. I think it comes from previous call infact, doing some debug I found that at this line line the router is not reset and it recalls old parameters array.

PHP Code:
class CodeIgniter
{
    /**
    * The current version of CodeIgniter Framework
    */
    public const CI_VERSION '4.3.6';

...

protected function 
tryToRouteIt(?RouteCollectionInterface $routes null)
    {
        if ($routes === null) {
            $routes Services::routes()->loadRoutes();
        }

        // $routes is defined in Config/Routes.php
        $this->router Services::router($routes$this->request); 

At the moment this workaroud does the trick

PHP Code:
    public function testAbort()
    {
        $routes = [['get''section/abort''Section::getAbort']];
        $result $this->withRoutes($routes)->get('section/abort');
        $result->assertRedirectTo('section');
    

Thank you.
Reply
#6

Test https://github.com/codeigniter4/CodeIgniter4/pull/7597 if you can.
Reply
#7

(06-19-2023, 01:18 AM)kenjis Wrote: Test https://github.com/codeigniter4/CodeIgniter4/pull/7597 if you can.
Tested. It works!

Thank you
Reply
#8

The PR was merged. This bug will be fixed in the next v4.3.7.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB