Skip to content

Bug: Cannot pass value in the function in ControllerTest #2470

Closed
@charlesng

Description

@charlesng

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugVerified issues on the current code behavior or pull requests that will fix them

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions