Before more functionality is added there is some refactoring
to do.
We are going to do some timing tests and so the
TimeTestCase class definitely needs
its own file.
Let's say tests/time_test_case.php...
require() this file into
the all_tests.php script.
I don't know quite what the format of the log message should
be for the test, so to check for a timestamp we could do the
simplest possible thing, which is to look for a sequence of digits.
Log
object and writes a message.
We look for a digit sequence and then test it against the current
time using our Clock object.
Of course it doesn't work until we write the code.
We can get the tests to pass simply by adding a timestamp
when writing out to the file.
Yes, of course all of this is trivial and
I would not normally test this fanatically, but it is going
to illustrate a more general problem.
The log.php file becomes...
Our new test is full of problems, though.
What if our time format changes to something else?
Things are going to be a lot more complicated to test if this
happens.
It also means that any changes to the clock class time
format will cause our logging tests to fail also.
This means that our log tests are tangled up with the clock tests
and extremely fragile.
It lacks cohesion, which is the same as saying it is not
tightly focused, testing facets of the clock as well as the log.
Our problems are caused in part because the clock output
is unpredictable when
all we really want to test is that the logging message
contains the output of
Clock::now().
We don't
really care about the contents of that method call.
Can we make that call predictable?
We could if we could get the log to use a dummy version
of the clock for the duration of the test.
The dummy clock class would have to behave the same way
as the Clock class
except for the fixed output from the
now() method.
Hey, that would even free us from using the
TimeTestCase class!
We could write such a class pretty easily although it is
rather tedious work.
We just create another clock class with same interface
except that the now() method
returns a value that we can change with some other setter method.
That is quite a lot of work for a pretty minor test.
Except that it is really no work at all.
To reach instant testing clock nirvana we need
only three extra lines of code...
eval()s the new code to
create the new class.
Our test case is on the first steps of a radical clean up...
MockClock
object and then sets the return value of the
now() method to be the string
"Timestamp".
Every time we call $clock->now()
it will return this string.
This should be easy to spot.
Next we create our log and send a message.
We pass into the message()
call the clock we would like to use.
This means that we will have to add an optional parameter to
the logging class to make testing possible...
Does that extra parameter in the Log
class bother you?
We have changed the interface just to facilitate testing after
all.
Are not interfaces the most important thing?
Have we sullied our class with test code?
Possibly, but consider this. Next chance you get, look at a circuit board, perhaps the motherboard of the computer you are looking at right now. On most boards you will find the odd empty hole, or solder joint with nothing attached or perhaps a pin or socket that has no obvious function. Chances are that some of these are for expansion and variations, but most of the remainder will be for testing.
Think about that. The factories making the boards many times over wasting material on parts that do not add to the final function. If hardware engineers can make this sacrifice of elegance I am sure we can too. Our sacrifice wastes no materials after all.
Still bother you? Actually it bothers me too, but not so much here. The number one priority is code that works, not prizes for minimalism. If it really bothers you, then move the creation of the clock into another protected factory method. Then subclass the clock for testing and override the factory method with one that returns the mock. Your tests are clumsier, but your interface is intact.
Again I leave the decision to you.