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: Cannot pass value in the function in ControllerTest #2470

Closed
charlesng opened this issue Jan 1, 2020 · 1 comment
Closed

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

charlesng opened this issue Jan 1, 2020 · 1 comment
Assignees
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Milestone

Comments

@charlesng
Copy link

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

@charlesng charlesng added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 1, 2020
@lonnieezell
Copy link
Member

Because it doesn't go through all of the routing, etc when doing a ControllerTest, you must provide the parameters as you've done in the last example. However, it shouldn't die without a URI, so I'll dig into that.

@lonnieezell lonnieezell self-assigned this Jan 2, 2020
@lonnieezell lonnieezell added this to the 4.0.0 milestone Jan 2, 2020
lonnieezell added a commit that referenced this issue Jan 2, 2020
ControllerTest should work without URI specified. Fixes #2470
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

No branches or pull requests

2 participants