I came across a problem when mocking Doctrine entities. All is fine until I want to assign custom Id to the entity (Id is inherited from the Identifier trait, Entity extends BaseEntity). Is there a satisfying way to do this?

Mockista doesn't seem to be able to mock __set() and __get() methods.

The getId() method has to be final, because otherwise the proxy would wrap it and even tho every proxy has always it's ID, simple call of $entity->getId() would initialize the entity.

If you really need to mock the entity, you're better of setting the value with reflection directly to the property.

  • Comments:

  • Yeah, seems like there is no other way. Thanks for the answer! :)

    by Filip Klimeš at 23.1.2015 0:00
  • Actually Filip this might not be correct anymore. Doctrine now generates this code for non-final getId method: http://pastebin.com/FagykrWX

    by Jáchym Toušek at 5.2.2015 19:32

You can try Mockery for mocking. It should be able to mock all methods including final, private etc. It is more powerful then Mockista in many ways by my experience.

You must first log in to participate in this discussion