codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.35k stars 1.9k forks source link

Bug: Can't test redirect()->route('routename'), while redirect()->to('path') is working with ControllerTester #2676

Closed samsonasik closed 4 years ago

samsonasik commented 4 years ago

Describe the bug

For example, I have a controller function Home::index that redirect to route "product-index":

<?php namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return redirect()->route('product-index');
    }
}

On Test part, I create test with:

<?php namespace AppTest\Controller;

use App\Controllers\Home;
use CodeIgniter\Test\CIDatabaseTestCase;
use CodeIgniter\Test\ControllerTester;

class HomeTest extends CIDatabaseTestCase
{
    use ControllerTester;

    public function testIndexRedirectToProduct()
    {
        $result = $this->controller(Home::class)
                       ->execute('index');

        $this->assertTrue($result->isRedirect());
    }
}

and it got error:

vendor/bin/phpunit tests/Controller/HomeTest.php
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 284 ms, Memory: 12.00 MB

There was 1 error:

1) AppTest\Controller\HomeTest::testIndexRedirectToProduct
CodeIgniter\HTTP\Exceptions\HTTPException: 

/Users/samsonasik/www/ci4/vendor/codeigniter4/framework/system/HTTP/Exceptions/HTTPException.php:168
/Users/samsonasik/www/ci4/vendor/codeigniter4/framework/system/HTTP/Response.php:314
/Users/samsonasik/www/ci4/vendor/codeigniter4/framework/system/Test/ControllerTester.php:191
/Users/samsonasik/www/ci4/tests/Controller/HomeTest.php:14

If I change the code with redirect()->to(base_url('product')).

<?php namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return redirect()->to(base_url('product'));
    }
}

It working:

vendor/bin/phpunit tests/Controller/HomeTest.php
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 258 ms, Memory: 12.00 MB

OK (1 test, 1 assertion)

CodeIgniter 4 version 4.0.2

Expected behavior, and steps to reproduce if appropriate Working with redirect()->route()

Context

nyufeng commented 4 years ago

user route config can't autoload to Tests. function route nead named_route image

samsonasik commented 4 years ago

The "product-index" already in app/Config/Routes.php, why it needs manually redifine it in test?

samsonasik commented 4 years ago

I created PR #2686 for it.