Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Unify casing in BaseService::injectMock#10313

Closed
ThomasMeschke wants to merge 1 commit into
codeigniter4:4.8from
ThomasMeschke:4.8
Closed

fix: Unify casing in BaseService::injectMock#10313
ThomasMeschke wants to merge 1 commit into
codeigniter4:4.8from
ThomasMeschke:4.8

Conversation

@ThomasMeschke

@ThomasMeschke ThomasMeschke commented Jun 16, 2026

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

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 16, 2026
@ThomasMeschke ThomasMeschke changed the title Unify casing in BaseService::injectMock fix: Unify casing in BaseService::injectMock Jun 16, 2026
@michalsn michalsn added wrong branch PRs sent to wrong branch and removed 4.8 PRs that target the `4.8` branch. labels Jun 16, 2026
@michalsn

Copy link
Copy Markdown
Member

Thank you - that makes sense, but this PR should target the develop branch.

@ThomasMeschke ThomasMeschke changed the base branch from 4.8 to develop June 17, 2026 07:49
@ThomasMeschke ThomasMeschke changed the base branch from develop to 4.8 June 17, 2026 07:49
@ThomasMeschke

Copy link
Copy Markdown
Contributor Author

True, my bad.
#10316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wrong branch PRs sent to wrong branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants