2

I have a preUpdate event hook in an event listener and I want to write a test that just verifies that it gets executed when a particular document is updated. The only documentation I've been able to find about testing anything Doctrine-related was about testing query builders. I am new to Doctrine and this seems like a very simple thing to do but I really can't figure it out.

beth
  • 1,916
  • 4
  • 23
  • 39
  • 1
    You can either do one of two things (or both): **1.** unit test the listener, mocking the objects you need to pass to the listener, and then just calling the function and making sure the entity updates. **2.** Write a functional test that actually performs a database operation that would trigger the listener, then poll the database value to make sure it changed to what you expected. – Jason Roman Dec 31 '14 at 18:25
  • Did you ever get anywhere with this? Would you prefer to functional test or unit test? – Jason Roman Jan 01 '15 at 19:39

1 Answers1

1

You can use partial mock. Lets say the class that has preUpdate hook is called Entity.

$mock = $this->getMockBuilder('Mock')->setMethods(array('preUpdate'))->getMock();

This create an object for which only the preUpdate method is stubbed and you can write expectations to this method:

$mock->expects($this->once())->method('preUpdate');
// some code that triggers the hook

All other methods of the class will work as in the original class - they would not be overriden

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89