Skip to content

Bug: system/Config/BaseService.php getSharedInstance() will not return mocks with non-lowercase key #2534

Description

@bivanbi

Direction
We use github issues to track bugs, not for support.
If you have a support question, or a feature request, raise these as threads on our
forum.

Describe the bug
system/Config/BaseService.php injectMock() method converts argument $name to lowercase,
but getSharedInstance() does not, which leads to BaseService not returning mocks with name containing uppercase letter.

CodeIgniter 4 version
4.0.0-rc.3,
Develop [checkout @ 2020.02.09 09:05]

Affected module(s)
CodeIgniter\Config\BaseService

Expected behavior, and steps to reproduce if appropriate
Steps to reproduce:

  1. create a pair of classes, e.g MyClass and MockMyClass

  2. add method to Config\Services, e.g.:

     public static function myClass(bool $getShared = true) {
         if ($getShared){ 
             return static::getSharedInstance('myClass')
         }
         return new MyClass();
     }
    
  3. Create a test case that injects MockMyClass

    class TestClass extends CIUnitTestCase {
         $mock = MockMyClass();
         Services::injectMock('myClass', $mock);
         $testObject = Services::myClass();
         $this-assertEquals(spl_object_id($mock), spl_object_id($testObject));
    }
    

Expected behaviour: Services should return instance of MockMyClass
Actual behaviour: Services returns new (shared) instance of MyClass

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions