Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
samsonasik opened this issue Mar 9, 2020 · 3 comments · Fixed by #2686
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@samsonasik
Copy link
Member

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
@samsonasik samsonasik added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 9, 2020
@nyufeng
Copy link
Contributor

nyufeng commented Mar 10, 2020

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

@samsonasik
Copy link
Member Author

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

@samsonasik
Copy link
Member Author

I created PR #2686 for it.

MGatner added a commit that referenced this issue Mar 18, 2020
Fix #2676 : add ability to test redirect()->route() via ControllerTester
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants