Skip to content

[Security][BUG] Security final class can not be mocked in unit tests #29946

Closed
@anitadavid

Description

@anitadavid

Symfony version(s) affected: v4.1.9

Description

When writing unit tests on classes that use Security as dependency get the "Class "Symfony\Component\Security\Core\Security" is declared "final" and cannot be mocked." warning.

How to reproduce

class A
{
    /** @var Security */
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
    }
}

class ATest
{
    /** @var Security|ObjectProphecy */
    private $security;

    public function setUp()
    {
        $this->security = $this->prophesize(Security::class);
    }
}

Run the test.

Possible Solution

Remove final? Or somehow when mocking the class mark the original class as not final.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions