3

I'm looking at a class in CRM_Utils_Cache here specificly and it looks exactly like what I need to write some unit tests against my extension.

The issue is that the $cache array is private rather than protected...

This means I can't extend this class into a test mock that allows my unit tests to manipulate and observe the cache state.

For this reason I'm wonder if there is a downside to changing this (and maybe other) properties to protected?

2
  • I see there is a get() and set() public function to store and retrieve values in _cache. Are you not able to use them ? Dec 28, 2016 at 13:19
  • Not to count the cache entries or inspect without knowing what the key is @jitendrapurohit Dec 28, 2016 at 22:18

1 Answer 1

3

I think submitting a PR to make this variable protected would be perfectly reasonable.

Alternatively, there is a way to access private properties in PHP for the sake of unit testing via Reflection. It's slightly cludgy but it does work.

3
  • mmm - hard to know which to prefer of those 2 methods - wasn't aware of reflection technique - it seems like a reasonable approach for unit testing where you're allowed to do naughty things with mocks? Jan 1, 2017 at 8:18
  • Yea but in this case I think making the variable protected really wouldn't hurt anything. Go ahead and submit the PR and save the Reflection in your back pocket, you'll probably find a use for it later.
    – Coleman
    Jan 1, 2017 at 15:59
  • cool thanks for the advice! PR: here Jan 2, 2017 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.