codeigniter-phpunit icon indicating copy to clipboard operation
codeigniter-phpunit copied to clipboard

Can't acces method in Controller

Open zahidmuhammadzaki opened this issue 10 years ago • 10 comments

I have this function in Welcome Controller

public function format_tanggal($tanggal)
{
 return date('d-m-Y',strtotime($tanggal));
}

But when i write test in WelcomeTest

public function testFormatTanggal(){
        $this->requireController('Welcome');
        $this->CIC = new Welcome();
        $this->assertInstanceOf('CI_Controller', $this->CIC, 'Controller was called correctly');
        $this->assertEquals($this->CIC->format_tanggal('02-05-2015'),'05-02-2015');
}

I get this error

error

zahidmuhammadzaki avatar Feb 05 '16 04:02 zahidmuhammadzaki

Should be easy to XDebug it. Your $this->CIC must not have been initialized with the Welcome Controller you want. I also checked CI docs for your method name (that underscore raised some flags for me), but it doesn't seem to be a reserved word since it doesn't actually begins with an underscore.

fmalk avatar Feb 05 '16 16:02 fmalk

Thank's, it solved. But i have another question, is it support HMVC?

zahidmuhammadzaki avatar Feb 06 '16 08:02 zahidmuhammadzaki

@zahidmuhammadzaki you mean HHVM? Yes it does. I have a Travis CI on it: https://travis-ci.org/fmalk/codeigniter-phpunit/jobs/99513092

fmalk avatar Feb 10 '16 18:02 fmalk

No, I mean Hierarchical Model View Controller, it's modular method in CI.

zahidmuhammadzaki avatar Feb 11 '16 03:02 zahidmuhammadzaki

Oh, sorry. No idea. It puts files in core folder so it'd have to be tested. I honestly don't know if it is a popular extension.

fmalk avatar Feb 11 '16 17:02 fmalk

Yeah, it is popular extension.. I hope it can be included in the next development.. Thanks for your response..

zahidmuhammadzaki avatar Feb 11 '16 20:02 zahidmuhammadzaki

I'll consider compatibility within a next minor release, so I'll reopen this issue as a "feature request"

fmalk avatar Feb 11 '16 20:02 fmalk

@zahidmuhammadzaki don't forget to comment here if you find any success or bugs trying HMVC with this project

fmalk avatar Feb 11 '16 20:02 fmalk

Yes, i will..

zahidmuhammadzaki avatar Feb 11 '16 20:02 zahidmuhammadzaki

I found a hack solution for this HMVC problem..

But firstly, i want to describe what i use and i do:

  1. I use wiredesignz HMVC The folders structure looks like this 1
  2. I add this function to CITestCase.php to get controller from modules folders
    public function requireModuleController($module_path, $prefix = null)
    {
        list($module,$class) = explode('/', $module_path);
        $filepath = APPPATH.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'controllers'.DIRECTORY_SEPARATOR.$prefix.DIRECTORY_SEPARATOR.$class.'.php';
        if (file_exists($filepath))
        {
            require_once($filepath);
        }
    }

The problem is when i use this function i get this error error2

So, i debug the function in the Modules.php error3 And i get NULL value in phpunit

But i get CI_Config Instance when i run in browser error4

It seem global $CFG is not work in phpunit, i have no idea..

I found some solution to prevent phpunit backups all global variables. So, I try to disable php backupGlobals to "false" error6 But it does not work, and i still get null value in phpunit

Then i add few of code to Modules.php error5

And, It's works!

I want to make it works without hack the HMVC source file. Can anyone help?

zahidmuhammadzaki avatar Feb 15 '16 10:02 zahidmuhammadzaki