For the past few weeks, I’ve been writing about unit testing for the members of the site (and something that I plan to continue doing in the next post). It’s something that I think; if you write server-side code, you should be doing.
Of course, it’s easier for me to say it than do it so although I try to make sure I do a good job of it, there’s always room for me to improve. I say that more as a personal gut check than anything else, so I digress.
One of the concepts that often come up during testing is the idea of testing the values of private attributes. For example, let’s say that you have a setter, but you don’t necessarily have a getter for that particular value.
It’s easy to say “Well, then you need to write a getter,” but that’s not always the case. I mean what if you’re storing some information within the class that doesn’t need to be exposed to third-party classes?
How then are we supposed to write tests against that kind of data when we want to access it but don’t have the ability to do so and don’t want to compromise the integrity of our work?
That’s where reflection comes into play.
Reflection in PHP
Specifically, to understand how to inspect the value of a given variable from the outside in, you have to know how to use reflection.
Luckily, PHP provides us with a powerful API to do that, and it’s probably worth getting into the deep details of that in another post. But, for this one, will stick with the basics.
A Working Definition
First, a look at what reflection means. The PHP manual defines it as:
The ReflectionClass class reports information about a class.
But I think it’s worth having something a little more solid. Let’s go with something like this at least for this post:
Reflection is the how a program to inspect itself and modify itself during run time.
Maybe it’s not great; maybe not.
But it will serve the purpose this post.
Reading a Value via Reflection
Let’s assume that for this post, you have a namespaces class at Acme\Plugin\APIClient and it has a property called username. We’ll take a look at what a very basic implementation of this may look like later.
Of course, it’d be much more fleshed out in an actual plugin.
Let’s say, though, that you want to set the value of the attribute and then read its value. The caveat is that the property is marked as private and there’s no way to read it from the outside.
This is where reflection comes in handy. That is, we can use a part of the program to look at itself and report back what it sees. (Reflection, get it? It’s like when we want to know what’s up with ourselves and no one else is around so we look into a mirror and see what’s there.)
To do this, you need to do five things:
- Instantiate the class you want to test,
- Set the value of the variable,
- Grab an instance of the ReflectionClass for the class we want to test,
- Set it’s property to accessible,
- Read the value.
So here’s a series of gists that will provide the steps necessary to do exactly that.
1. Instantiate the Class
2. Set the Value
3. Instantiate Reflected Objects
4. Set the Property, Mark It as Accessible
5. Read the Value
And That’s the Basic Primer on Reflection
At this point, this should give you some basic information on what Reflection is, how to use it, and why it’s useful especially in the case of unit testing.
This is one of those concepts that can get more complex because PHP’s reflection API is pretty powerful (but relatively easy to understand). When you couple it with unit testing, though, there’s a lot of stuff that can be done.
My Shameless Plug
With that said, if you’re interested in the learning the ins-and-outs for this kind of stuff, don’t hesitate to check out the members-only area of the site. I’m building, each week, a backlog of stuff to help us focus on embracing better practices as WordPress developers.
Unit Testing, Reflection, and more is just the latest part of it.