-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Labels
bugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them
Milestone
Description
Direction
I have the following Controller Class and would like to write some Controller test using the instruction from documentation (https://codeigniter4.github.io/userguide/testing/controllers.html
Pages.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Pages extends Controller
{
public function index()
{
return view('welcome_message');
}
public function showme($page = 'home')
{
if (!is_file(APPPATH . '/Views/pages/' . $page . '.php')) {
// Whoops, we don't have a page for that!
throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
}
$data['title'] = ucfirst($page); // Capitalize the first letter
return view('pages/' . $page, $data);
}
}PagesTest
<?php
namespace CodeIgniter;
use CodeIgniter\Test\ControllerTester;
use CodeIgniter\Test\CIDatabaseTestCase;
class PagesTest extends CIDatabaseTestCase
{
use ControllerTester;
public function testIndex()
{
$result = $this->withURI('http://localhost:8080/en')
->controller(\App\Controllers\Pages::class)
->execute('index');
$this->assertTrue($result->isOK());
$this->assertTrue($result->see("Welcome"));
}
public function testShowMeWithHome()
{
$result = $this->withURI('http://localhost:8080/en/home')
->controller(\App\Controllers\Pages::class)
->execute('showme');
$this->assertTrue($result->isOK());
$this->assertTrue($result->see("I am home page"));
}
public function testShowMeWithAbout()
{
$result = $this->withURI('http://localhost:8080/en/showme/about')
->controller(\App\Controllers\Pages::class)
->execute('showme');
$this->assertTrue($result->isOK());
$this->assertTrue($result->see("About me"));
}
}Describe the bug
The 3rd test cases testShowMeWithAbout does not pass about value to the showme function in the controller, which cause it always to pass the default value in the function.
And I have tried to remove the withURI function in the testcase and change it as the following
public function testShowMeWithAbout()
{
$result = $this->controller(\App\Controllers\Pages::class)
->execute('showme', 'about');
$this->assertTrue($result->isOK());
$this->assertTrue($result->see("About me"));
}and now it gave me this error after I run ./vendor/bin/phpunit
1) CodeIgniter\PagesTest::testShowMeWithAbout
TypeError: Argument 2 passed to CodeIgniter\HTTP\IncomingRequest::__construct() must be an instance of CodeIgniter\HTTP\URI or null, string given, called in /Users/{myPath}/vendor/codeigniter4/framework/system/Test/ControllerTester.php on line 134
/Users/{myPath}/vendor/codeigniter4/framework/system/HTTP/IncomingRequest.php:161
/Users/{myPath}/vendor/codeigniter4/framework/system/Test/ControllerTester.php:134
/Users/{myPath}/tests/app/Controllers/PagesTest.php:32
And I would like to know if I did something wrong in the test cases.
CodeIgniter 4 version
Codelgniter v4.0.0-rc.3
Metadata
Metadata
Assignees
Labels
bugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them