Skip to content

[DependencyInjection] Container::set() is useless if the entry is an alias #16461

Closed
@mnapoli

Description

@mnapoli

In my integration tests I want to replace a service by a fake implementation. For example I want to temporarily replace the event_dispatcher by a mock that will store all triggered events:

$container->set('event_dispatcher', $fakeDispatcher);

The problem is that event_dispatcher is an alias, and Container::get() first resolves aliases before trying to find the entry.

That makes Container::set() silently useless when setting an entry that is an alias.

IMO this is a bug, and I see 2 solutions (by order of preference), feel free to suggest alternatives:

  • set() should erase any existing alias because it should just work
  • set() should throw an exception if setting an alias

I can implement the solution if one is chosen.

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