eloquent / enumeration

An enumeration implementation for PHP.
MIT License
147 stars 8 forks source link

Mocking support #27

Open Bilge opened 5 years ago

Bilge commented 5 years ago

Enumerations are useful to guarantee type safety, but painful for the test application to deal with elegantly. Often one wants to test an interface or implementation that requires an enumeration type, where the test doesn't care about the specific enumeration member used for the purposes of the feature it is testing. However, since enumerations are final, they cannot be mocked, so one has to use a specific member of the enumeration even though the specific value we pick is arbitrary and unimportant to the test. We can write comments every time we use insignificant enumeration members in this way, but mocking the enumeration is a clearer way to convey this intention without writing additional commentary.

I understand it might not be technically possible to provide a solution to this problem, however I wanted to start the discussion in case anyone has any bright ideas on how to solve this.

ezzatron commented 5 years ago

I can understand where you're coming from. Having tests that clearly express what's actually being tested can definitely be valuable. Unfortunately I don't have any great solutions to offer.

Perhaps using something like dg/bypass-finals, which removes final keywords when autoloading, only while running tests? But I suspect it would wreak havoc with coverage results.

You could also introduce an interface that only your enumeration implements, and change your type hints to accept that interface. Of course that also weakens the guarantee offered by the type hint.