How can I check that an EventListener has been called on an event in Symfony?

What do I have:

I have an EventListener that listens for the event of a PreRemove object in Symfony.

services:
   my_bundle.entity_listener.my_listener:
      class: 'MyCompany\MyBundle\MyListener'
      public: false
      tags:
        - { name: doctrine.orm.entity_listener, entity: 'MyCompany\MyBundle\Entity\MyEntity', event: preRemove }

      

What I want:

I want to have a test (functional / integral / unitary or whatever really) that somehow checks that a specific EventListener is called when MyEntity is removed.

UPDATE

I don't want to do this in a unit test, it needs to check if the event dispatcher will actually call that particular event listener for that particular event.

UPDATE 2

I thought it was obvious, but it doesn't seem to be the case - the solution should NOT change the EventListener or Event.

UPDATE 3

I pointed out that I don't care what the name of the test is: functional, singular, or whatever.

UPDATE 4

The solution must ensure that the test passes in the context of any environment. So, if someone extends my packages and clutters with my definitions, I can still check if EventHandling actually works.

Also, checking the processing result is not an option, because:

  • The EventListener can do absolutely anything - there may be times when I can't just check the result and know for sure that the EventListener is working.

  • Someone can handle the event in much the same way, so the "result" is the same, but the "path" is wrong.

+3


source to share


5 answers


A functional test is intended to test functionality; it is not intended to test the implementation of this functionality or the configuration of that implementation.

A test like the one you propose will be fragile and not very useful.



You probably need to check the function that the event listener implements.

+1


source


check out this: Write UnitTest for Symfony EventListener



This might help as I had (sort of) a similar problem / question

0


source


How to create a compiler profile and add it to the last stage of container compilation:

$container->addCompilerPass(
    new CheckEntityListenerRegistered(),
    PassConfig::TYPE_AFTER_REMOVING,
    -1000
);

      

This compiler will be the last to execute. From now on, you can check if your listener is registered correctly, and if so, you can assume that it will be executed by the Doctrine Job Control.

0


source


I think I understand what you want. You need an integration test (to test it across your entire environment) without changing the listener, event dispatcher, etc.

Solution 1

When you are working on dev, testing, or forcing symfony to load dispatchers for different events, it uses the same interface and behavior, but they are different implementations (I haven't tested the doctrine alone).

This way you will have different dispatchers for each environment and you don't want to know what is going on inside. Call this black box.

Action delete -> [ black box  ->  listener called ] -> listener effect

      

Don't want to look at the black box or touch it in any way? Find the effect the listener has on the system. Database, log file, mailer, etc.

Solution 2

If you let me connect to the black box, I will listen to the listener using the proxy and monitor if the listener was called to the proxy.

Solution 3

Alternatively you can use the data collectors from the symfony profiler, but you probably won't be able to enable this in production.

0


source


Maybe you can use the symfony profiler?

see How to use Profiler in functional test

in the profiler events section you have two tabs with callable / non-callable Listeners http://whatever.com/app_dev.php/_profiler/352211?panel=events

0


source







All Articles