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?

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

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.

| improve this answer | |
  • 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? – ErichBSchulz Jan 1 '17 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 '17 at 15:59
  • cool thanks for the advice! PR: here – ErichBSchulz Jan 2 '17 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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