1

I'm crawling the web since a lot of time but I can't find any answer to my question. I started a new project, and I use Pimple to maanage DI. Of course I want to unit test my classes and I need a Mock for one. THe problem is that with classic DI method ( pass class to the constructor) It's easy to create a mock and pass it to the class.

But as I use Pimple I don't know how to do that as I don't pass every class I need in the object in the constructor. I pass a Pimple Container object.

So, how am I supposed to proceed when I want to use mocks ? I mean the best practices. I though about creating a specific pimple container with all my mocks and use it in unit testing instead of the real one, but I'm not sure there isn't an easy/better way to do that. THanks for your help

  • Do not pass Pimple : ) passing this container you reach anit-pattern: https://en.wikipedia.org/wiki/Service_locator_pattern – sectus Jul 21 '16 at 09:46
  • So what do you suggest? Old school method: passing dependencies one by one in the constructor using the "new" keyword? – David Vander Elst Jul 26 '16 at 08:29
  • Could you provide some examples of classes than need to be mocked? Also, have you seen pimple examples? – sectus Jul 26 '16 at 08:33
  • Actually I found a solution at my own. I pass a specific container created only for unit testing where I instanciate required mocks. I guess this is the best way to do it. Tell me if I'm wrong? :) Thanks – David Vander Elst Jul 29 '16 at 09:28

0 Answers0