Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lock] Narrowed return type of LockFactory makes mocks with NoLock incompatible #53713

Closed
mbabker opened this issue Jan 31, 2024 · 1 comment · Fixed by #54084
Closed

[Lock] Narrowed return type of LockFactory makes mocks with NoLock incompatible #53713

mbabker opened this issue Jan 31, 2024 · 1 comment · Fixed by #54084

Comments

@mbabker
Copy link
Contributor

mbabker commented Jan 31, 2024

Symfony version(s) affected

7.0+

Description

As part of #50852 the return types for both methods in Symfony\Component\Lock\LockFactory were narrowed from the base LockInterface to the SharedLockInterface. In some test cases, we are mocking the lock factory and the mock provides a NoLock instance to disable locks in the context of those tests. Since NoLock does not implement SharedLockInterface, using it with the LockFactory is no longer possible.

How to reproduce

$lockFactory = $this->createMock(LockFactory::class);
$lockFactory->method('createLock')
    ->willReturnCallback(static fn () => new NoLock());
// Test fails with:
// TypeError : Mock_LockFactory_a4c266f2::createLock(): Return value must be of type Symfony\Component\Lock\SharedLockInterface, Symfony\Component\Lock\NoLock returned

Possible Solution

One of:

  • Restore the return type of the LockFactory methods to the base LockInterface
  • Implement SharedLockInterface in the NoLock class

Additional Context

No response

@stof
Copy link
Member

stof commented Jan 31, 2024

Using a more precise type was expected (but could not be done before the major version).
The right fix is to implement SharedLockInterface in NoLock IMO.

nicolas-grekas added a commit that referenced this issue Feb 27, 2024
…mbabker)

This PR was merged into the 7.1 branch.

Discussion
----------

[Lock] Make NoLock implement the SharedLockInterface

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | Fix #53713
| License       | MIT

This PR updates the `Symfony\Component\Lock\NoLock` stub to implement `Symfony\Component\Lock\SharedLockInterface` and returns it to being compatible with the `Symfony\Component\Lock\LockFactory` return types.

Commits
-------

22600d0 Make NoLock implement the SharedLockInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants