News for hackers. Stuff that matters.
Re:PHP Sadness by arkh on Thursday November 29, 2018 @05:52AM (#18559345)
> The point of private methods is to prevent what other code can call them. However, in PHP, you also cannot override private methods in a subclass. This is especially important when, for example, trying to build mocks in unit tests.

You may not be testing the right things if you have to override your methods just for your tests.

Re:PHP Sadness by josteink on Thursday November 29, 2018 @06:20AM (#18559438)
Also: many languages prevent all access to private members in derived classes. This is by design: private is private.

If you need derives classes-only to have access, you should typically use the “protected”-modifier.

Doesn’t php have that?

Re:PHP Sadness by dsego on Thursday November 29, 2018 @06:23AM (#18559450)
It does.
Re:PHP Sadness by josteink on Thursday November 29, 2018 @07:24AM (#18559678)
So the PHP sadness in this case refers to lacking PHP knowledge of the person who wrote this blog?
Re:PHP Sadness by wnevets on Thursday November 29, 2018 @12:44PM (#18562040)
The problem stems from people believing that unit in unit testing means a class's method.
Re:PHP Sadness by giancarlostoro on Thursday November 29, 2018 @06:12AM (#18559414)
Yeah you shouldnt override any existing code when doing unit testing, only updating properties if anything... Otherwise you are taking the code beyond its scope, if you need output from a method to be something specific... Just set the variable you are storing that methods return value into to what you need that output to be.
Re:PHP Sadness by viraptor on Thursday November 29, 2018 @06:41AM (#18559516)
This is nice in theory, but you need to design everything to work dependency injection style (or really have a setter for everything) to achieve this. Using a random number? Store the generator. Opening a file? Use a "FooOpener". ...

Sometimes it's easier just to stub what you want.

Re:PHP Sadness by dhimes on Thursday November 29, 2018 @07:17AM (#18559651)
Yeah- Sometimes I've gotten lazy and returned an object that has all the private stuff attached. Once I've verified that it works, I quit returning that object, keeping the methods private for normal use. It gives me a clear logical delineation yet allows me to test while developing.
Re:PHP Sadness by arkh on Thursday November 29, 2018 @08:32AM (#18560012)
If you can't put the thing you're testing (method, class, application) in the state you want to test using only public methods you are not testing something useful. If you're doing to get 100% coverage be happy: that's dead code you can remove.
Re:PHP Sadness by viraptor on Thursday November 29, 2018 @04:08PM (#18564033)
Counterexample: Testing something that uses your language's Array.shuffle equivalent which doesn't expose the randomness provider. You can't put it in the right state without just stubbing the call and making it return the order you want.