You write unit tests for a Magento 2 module and need to mock a core class, like the store model. This class has a few dependencies which have more dependencies and it’s getting complicated to mock. Although PHPUnit 5.4 has a createMock()
method that automatically stubs methods and let them return new stubs if they have a return type.
Or: you mock a service contract like getList()
in the product repository, which takes a search criteria instance as parameter, which must be returned by a search criteria factory. Then it returns a product search result, which has a getItems()
method, which returns a array of products. So you end up mocking countless dependendencies for a single method call
Do these situations sound familiar? It does not need to be like that, though! Let me show you a sane way to deal with Magento dependencies.