symfony 5 how to mock/replace service in container - symfony

I want to make a mock for some class in my functional test. Let's say I want to mock some custom command bus realization.
First of all I need to register that service as a public in the services_test.yaml:
services:
_defaults:
public: true
App\CommandBus: ~
but still even if I did so I'm getting error service is already initialized, you cannot replace it when doing something like:
$commandBus = $this->createMock(CommandBus::class);
$commandBus->expects(self::once())
->method('dispatch')
->with($data)
->willReturn($response);
self::$container->set(CommandBus::class, $commandBus);
So how can I easily mock some object and insert mocked version to the container???
Probably I can make some wrapper for tests link it with interface in services_test.yaml, and add a methods like setExpectiotion which will chekc if expectation set and if so instead of running command return it. But it's extra work and it's taking a lot of time to design such a class for each service I want to mock.
Is there any other way to mock some service?? Thanks in advice!

I assume your command bus is using an asynchronous transport type. If you wish you can solve this problem using sync transport for tests instead. You don't need to mock it in this case, because if you will mock command bus, you will test mock instead of the whole process.
Let's create a new config in config/packages/test/messenger.yaml
framework:
messenger:
transports:
command_bus: 'sync://'
Then enable the functional test without worrying about the consumer.
If you want to debug container for tests, you can check it using command
./bin/console debug:container -e test
Please let me know if that solves your problem.

Related

Multiple SpringBootTests using EmbeddedKafka fails with "only one 'RetryTopicConfigurationSupport'"

We have an application that makes use of Spring Kafka's non blocking retries via the RetryableTopic annotation.
We are in the middle of upgrading spring-kafka from 2.8.4 to 2.9.0.
We have several SpringBootTests that makes use of EmbeddedKafka. Each of these tests are marked with DirtyContext and AutoConfigureMockMvc
After upgrading, the first test would run fine, but the later test would fail to start the application with
Constructor threw exception; nested exception is java.lang.IllegalStateException: Only one 'RetryTopicConfigurationSupport' is allowed
I understand that the RetryTopicConfigurationSupport tries to ensures only one instance of itself is ever instantiated. So in the use case of running multiple unit tests, which includes multiple different SpringBootTests, how do we avoid hitting this problem?
I have tried marking the context dirty, but of course that didn't solve the problem as RetryTopicConfigurationSupport is using a static variable to track whether it has been instantiated before or not.
I have tried NOT marking the ContextDirty, but the later tests would fail to start the application because the Port is already in use.
Appreciate any advise!
Looks like this problem has been addressed in spring-kafka 3.0 and backported to 2.9.3
https://github.com/spring-projects/spring-kafka/issues/2477

Finding status if MDB is running

I want to use mbeans on startup of j2ee application to check if all the MDBs are running and jms specification has been activated.
Any pointers will be very helpful
The only way I know of to do this would be to use the ServerEndpointControl MBean. This is a Liberty specific MBean for controlling the input sources for work into the runtime. This can also be used to get status on http listeners.
The best place to find the Javadoc for the MBean is here. To find out if an MBean is running you call the isPaused method providing the MDB name which is defined as:
ApplicationName#ModuleName#BeanName
if the MDB is running it'll return false.

TelemetryProcessor not called without an InstrumentationKey?

I've created a class derived from ITelemetryProcessor, so I can capture telemetry data during a unit test of a C# .Net Class Library. Being a unit test, there is no InstrumentationKey provided as unit tests should have no network dependencies. (I cannot factor the telemetry to an injected interface.)
I create and use TelemetryClient's and log custom events during the unit test methods. However, I noticed my Process() method was not getting called when I logged telemetry items.
After doing some experimentation, I realized that if set an InstrumentationKey to a dummy Guid, then my Processor() method started to get called.
TelemetryConfiguration.Active.InstrumentationKey = Guid.NewGuid().ToString();
Question: why should I need to provide an InstrumentationKey in order for processors to be invoked?
Thanks
-John
TelemetryProcessor's are meant to apply additional processing/filtering to telemetry items before being sent to AI. If there is no ikey, then there is no point sending to AI as it will not be accepted anyway. Why run overhead of running all processors.

a service which would be able to run jobs on a timed basis

I am working for my client using Asp.net webAPI2 and angularJS. Now my client have following requirement,but i am unable to understand what type of project i have to create like WebAPI project,window service or any other? Anyone please tell me what the client actually want and how can i do it?
QueueManager will need to be some kind of a service which would be able to run jobs on a timed basis. We envision it being a service that runs on a continuous loop, but has a Thread.Sleep at the end of each iteration with a duration of x-seconds (“x” being set in a config file.) You should create this QueueManager service as a new project within the Core.Jobs project; I would like to have the project name be “Core.Jobs.QueueManager”, along with the base namespace.
Here are the functions that the QueueManager will do for each iteration:
1) Do a worker healthcheck (JobsAPI: Queue/WorkerHealthCheck – already created)
a. This method will just return a 200 status code, and a count of workers. Not need to act on the return value.
Look at Hangfire, it is quite easy to set up and simple to use.
http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html

Symfony2 calling console command in controller from vendor

I want to use a console command from this bundle within my controller: http://knpbundles.com/dizda/CloudBackupBundle
The developer proposes cronjobs, however I want to use the command to backup my database from within my controller.
How would I do that?
I am getting this error message when i simply try to register this command as a service:
You have requested a non-existent service "backupcommandservice".
Thanks for the help!
commands don't quite work that way. Per the note on http://symfony.com/doc/current/cookbook/console/console_command.html#register-commands-in-the-service-container
registering a command as a service doesn't do much other than control location and dependency injection.
if you want to call a command: http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command
that being said you shouldn't call commands from within a controller since you're basically asking to wait for this command to finish executing before you return a response. You'd be better off just sending a request to a queue box (for example beanstalk) and have a worker perform the job.

Resources