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

Controller response property overriding by ControllerResponse inPHPUnit #1834

Closed
plegenza opened this issue Mar 14, 2019 · 4 comments
Closed

Comments

@plegenza
Copy link


name: Controller response property overriding by ControllerResponse in PHPUnit
about: ControllerResponse instance overriding property of standard Controller in unit testing controller.


Code to reproduce:

Controller:

namespace App\Controllers;

class Home extends \CodeIgniter\Controller {
    function index() {
        $response = $this->response->setJSON([
            'lang' => $this->request->getLocale()
        ]);
    }
}

ControllerTest:

<?php

namespace Tests\Controllers;
use CodeIgniter\Test\ControllerTester;
use CodeIgniter\Test\CIDatabaseTestCase;

class HomeTest extends CIDatabaseTestCase {

    use ControllerTester;

    public function testIndex() {
        $result = $this->withURI('http://example.com/rest/')
          ->controller(\App\Controllers\Home::class)
          ->execute('index');
    }
}
  • OS: Win 7
  • XAMPP
  • 7.2
@lonnieezell
Copy link
Member

What is that supposed to test? Your test doesn't show it actually testing the response lang that you set.

But also - what are you doing with the $response in the controller? Unless you return a redirect response instance from the controller method, the system is designed to work with the Controller's response.

So I'm confused what the bug actually is here and how you're confirming that.

@plegenza
Copy link
Author

Okay, code have only block's to show error.

Full test:

public function testIndex() {
        $result = $this->withURI('http://example.com/rest/')
          ->controller(\App\Controllers\Home::class)
          ->execute('index');

        $response = json_decode($result->getBody());
        $this->assertEquals('pl', $response->message);
 }

Full controller method:

function index() {
        $response = $this->response->setJSON([
            'lang' => $this->request->getLocale()
        ]);

        return $response;
}

Error in PHPUnit:
test

@plegenza
Copy link
Author

That's Throwable error from ControllerTester.php:

test

@jim-parry
Copy link
Contributor

I have fixed this, but there is also a problem with your test.

    $response = json_decode($result->getBody());
    $this->assertEquals('pl', $response->message);

After json_decode, $response is an associate array, with a 'lang' key. There is no "message" property or key.

jim-parry added a commit that referenced this issue Mar 22, 2019
Fix: ControllerTester::execute. Fixes #1834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants