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

[2.0] Allow to mock services when running command #463

Open
magnetik opened this issue Oct 24, 2018 · 5 comments
Open

[2.0] Allow to mock services when running command #463

magnetik opened this issue Oct 24, 2018 · 5 comments
Milestone

Comments

@magnetik
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm "mocking" some services when using the test client. It's something like that:

$client = $this->getClient();
$clientMock = $this->getMockBuilder(Client::class)
		->disableOriginalConstructor()
		->getMock();

$clientMock
	->method('doThings')
	->willReturn(true);

$client->getContainer()->set(Client::class, $clientMock);

Describe the solution you'd like
Instead of running the command directly, a CommandClient could be created (that could be like the "HttpClient", that allows to run any command and access the container before.

Describe alternatives you've considered
Maybe instead of having the ability to reuse the current kernel in protected function runCommand(string $name, array $params = [], bool $reuseKernel = false), it could be useful to provide your own kernel (and use a kernel from createClient ?)

@Jean85
Copy link
Contributor

Jean85 commented Oct 24, 2018

I fear that that approach could be flawed. The reason for reusing the kernel is that it's already booted, so any edit is not lost before launching the command.

How would you suggest to pass a different Kernel? Wouldn't be dangerous, due to differences in respect to the system under test?

@magnetik
Copy link
Contributor Author

magnetik commented Oct 24, 2018

Indeed I prefer the first approach (having a CommandClient that allow container modification).

@alexislefebvre alexislefebvre added this to the 2.0 milestone Nov 24, 2018
@maximium
Copy link

maximium commented Jan 31, 2025

Currently, I use such a helper method to set mocked services in container. It would be handy to have it in the base class or have such a trait in the test bundle.

    protected function setPrivateService(ContainerInterface $container, string $serviceId, object $mock): void
    {
        $containerRef = new \ReflectionObject($container);

        if ($containerRef->hasProperty('services')) {
            $servicesProperty = $containerRef->getProperty('services');
            $services = $servicesProperty->getValue($container);
            $services[$serviceId] = $mock;
            $servicesProperty->setValue($container, $services);
        }
    }

@alexislefebvre
Copy link
Collaborator

@maximium Feel free to add a PR.

@maximium
Copy link

maximium commented Feb 5, 2025

Done #646
Also I had failed tests because of missing annotation_reader service when running on symfony7. I've made the separate PR #645

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants