kenjis / ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x.
http://kenjis.github.io/ci-phpunit-test/
MIT License
587 stars 195 forks source link

Replacement of helper function redirect() does not work #407

Closed ryfighter closed 1 year ago

ryfighter commented 1 year ago

I am having some issues getting the replacement for the helper function redirect() to work correctly.

Example of controller:

class user extends Controller
{
        public function logout()
        {
                // do stuff
                redirect('login');
        }
}

Example of Unit Test

class UserController_Test extends CIPHPUnitTestCase
{
        public function test_UserController_Logout()
        {
            // do other setup stuff
            $this->request('POST', 'user/logout');
        }
}

When I attempt to run this I am getting an error in the replacement function:

Method Mockery_0::site_url() does not exist on this mock object

This is coming from the first few lines of the file

if ( ! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'auto', $code = NULL)
    {
        if ( ! preg_match('#^(\w+:)?//#i', $uri))
        {
            $uri = site_url($uri);
        }
                // Other stuff happens below this line but it fails here
        }
}

The thing is, it does look like a replacement function exists for site_url as well but it is not being loaded for some reason. Any ideas on how to mitigate this issue?

kenjis commented 1 year ago

It seems Mockery reports the error.

ci-phpunit-test does not use Mockery. Remove Mockery or configure it not to report the error.

ryfighter commented 1 year ago

Mockery is used by all of our hundreds of other tests so that is not an option. We have not had any issues till this patched function. Is it only these replacement functions that have an issue with mockery? Why would a global function not contained within a class even be registered by mockery? You can't mock the global element with mockery.

kenjis commented 1 year ago

I don't know what Mockery does. This library does not have Mockery_0::site_url().

If you remove Mockery and it would work, probably Mockery does something wrong. Investigate what Mockery does and make it work without the error.