Skip to content

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

Closed
@mbabker

Description

@mbabker

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions