Hey,

My understanding is that mocking final methods and types is a limitation that 
is not specific to just PHPUnit, or indeed to PHP itself. Mockery, another 
established PHP mock object framework, relies on a workaround for mocking final 
methods and types that prevents testing code with type constraints or 
instanceof checks[1]. On the JVM, the popular mocking framework Mockito 
similarly has to rely on instrumentation to provide support for this use 
case[2].

Personally, I do not have enough context to judge whether there is added value 
in declaring those methods in e.g. WANObjectCache as final. It may have been 
intended to explicitly prevent subclassing and overriding by extension code, 
although this is just a guess. A question that could be posed is whether these 
benefits are worth the tradeoff in terms of reduced testability.

What do you think?

——
[1] http://docs.mockery.io/en/latest/reference/final_methods_classes.html
[2] 
https://static.javadoc.io/org.mockito/mockito-core/3.0.0/org/mockito/Mockito.html#39

Máté Szabó 
SOFTWARE ENGINEER
+36 30 947 5903

WIKIA sp. z o.o. z siedzibą w Poznaniu, ul. Abp. A. Baraniaka 6
Sąd Rejonowy Poznań – Nowe Miasto i Wilda w Poznaniu, VIII Wydział Gospodarczy 
Krajowego Rejestru Sądowego, KRS 0000254365
NIP: 5252358778
Kapitał zakładowy: 50.000,00 złotych


> On 27 Aug 2019, at 20:56, Daimona <[email protected]> wrote:
> 
> Personally, I don't like these limitations in PHPUnit and the like. IMHO,
> they should never be a reason for changing good code. And sometimes,
> methods have to be final. I don't know about the specific case, though.
> 
> Anyway, some time ago I came across [1], which allows mocking final methods
> and classes. IIRC, it does that by removing the `final` keywords from the
> tokenized PHP code. I don't know how well it works, nor if it could degrade
> performance, but if it doesn't we could bring it in via composer.
> 
> 
> [1] - https://github.com/dg/bypass-finals
> 
> 
> Il giorno martedì 27 agosto 2019, Aryeh Gregor <[email protected]> ha scritto:
> 
>> I see that in some classes, like WANObjectCache, most methods are declared
>> final. Why is this? Is it an attempt to optimize?
>> 
>> The problem is that PHPUnit mocks can't touch final methods. Any ->method()
>> calls that try to do anything to them silently do nothing. This makes
>> writing tests harder.
>> 
>> If we really want these methods to be marked final for some reason, the
>> workaround for PHP is to make an interface that has all the desired
>> methods, have the class implement the interface, and make type hints all
>> refer to the interface instead of the class. But if there's no good reason
>> to declare the methods final to begin with, it's simplest to just drop it.
>> _______________________________________________
>> Wikitech-l mailing list
>> [email protected]
>> https://lists.wikimedia.org/mailman/listinfo/wikitech-l
> 
> 
> 
> -- 
> https://meta.wikimedia.org/wiki/User:Daimona_Eaytoy
> "Daimona" is not my real name -- he/him
> _______________________________________________
> Wikitech-l mailing list
> [email protected]
> https://lists.wikimedia.org/mailman/listinfo/wikitech-l


_______________________________________________
Wikitech-l mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Reply via email to