Blame | Letzte Änderung | Log anzeigen | RSS feed
.. index::single: Mocking; Final Classes/MethodsDealing with Final Classes/Methods==================================One of the primary restrictions of mock objects in PHP, is that mockingclasses or methods marked final is hard. The final keyword prevents methods somarked from being replaced in subclasses (subclassing is how mock objects caninherit the type of the class or object being mocked).The simplest solution is to implement an interface in your final class andtypehint against / mock this.However this may not be possible in some third party libraries.Mockery does allow creating "proxy mocks" from classes marked final, or fromclasses with methods marked final. This offers all the usual mock objectgoodness but the resulting mock will not inherit the class type of the objectbeing mocked, i.e. it will not pass any instanceof comparison. Methods markedas final will be proxied to the original method, i.e., final methods can't bemocked.We can create a proxy mock by passing the instantiated object we wish tomock into ``\Mockery::mock()``, i.e. Mockery will then generate a Proxy to thereal object and selectively intercept method calls for the purposes of settingand meeting expectations.See the :ref:`creating-test-doubles-partial-test-doubles` chapter, the subsectionabout proxied partial test doubles.