Skip to content

fix: unify casing in BaseService::injectMock#10316

Open
ThomasMeschke wants to merge 1 commit into
codeigniter4:developfrom
ThomasMeschke:develop
Open

fix: unify casing in BaseService::injectMock#10316
ThomasMeschke wants to merge 1 commit into
codeigniter4:developfrom
ThomasMeschke:develop

Conversation

@ThomasMeschke

Copy link
Copy Markdown
Contributor

Description
The BaseService::injectMock method only applied the strtolower function to the name in regard to the mock-list, but not to the instance-list:

public static function injectMock(string $name, $mock)
{
    static::$instances[$name]         = $mock;
    static::$mocks[strtolower($name)] = $mock;
}

Every other method in the BaseService applies strtolower to the name argument before doing anything else, e.g. resetSingle:

public static function resetSingle(string $name)
{
    $name = strtolower($name);
    unset(static::$mocks[$name], static::$instances[$name]);
}

... or getSharedInstance:

protected static function getSharedInstance(string $key, ...$params)
{
    $key = strtolower($key);

    // Returns mock if exists
    if (isset(static::$mocks[$key])) {
        return static::$mocks[$key];
    }

    if (! isset(static::$instances[$key])) {
        // Make sure $getShared is false
        $params[] = false;

        static::$instances[$key] = AppServices::$key(...$params);
    }

    return static::$instances[$key];
}

This results in two different instance entries when injectMock gets called with "fooBar" and with "foobar" respectively, but only one of them (namely "foobar") is ever to be resolved or reset again.
When not using all-lowercase while calling injectMock, any existing instance that is meant to be replaced is therefore not being replaced.

image

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@ThomasMeschke ThomasMeschke marked this pull request as ready for review June 17, 2026 09:18
@michalsn michalsn added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 17, 2026
@michalsn

Copy link
Copy Markdown
Member

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

Successfully merging this pull request may close these issues.

2 participants