Skip to content

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

Description

@samsonasik

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

  • OS: OSX
  • Web server Apache
  • PHP version 7.4

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