Better Practice, Dec. 2018

(singke) #1
http://www.phparch.com \ December 2018 \ 21

How to Learn PHP Unit Testing With Katas

PhpStorm or Sublime Text. It can also
be good old Windows Notepad.
You’ll also need access to a command
line like Bash. If you’re doing modern
PHP, you’re probably pretty comfort-
able with this as it’s how most of us turn
on and interact with Composer.
Finally, you’ll need PHPUnit. What’s
PHPUnit? The source for it, phpunit.de,
says it clearly:

PHPUnit is a programmer-oriented
testing framework for PHP. It is an
instance of the xUnit architecture
for unit testing frameworks.

The reference to xUnit may feel like
extra complexity, but it isn’t too much.
It’s identifying that PHPUnit shares a
history with tools like JUnit, the Java
unit testing library. There are others like
it (sUnit, nUnit) and as we’re program-
mers, we love to make the variable first
letter or phrase into a variable.
So PHPUnit is a testing framework
which allows us to make assertions
about our code. You can reasonably
think of PHPUnit as a convenient
wrapper for things you can declare
true about your code at a certain point.
PHPUnit scores and tracks whether or
not they are.
PHPUnit does have features other
than assertions, but we’ll not get into
those. You can get a whole lot of value
from testing with only assertions and
PHPUnit’s runner for it.

What are Code Katas?
The final piece of the puzzle, at last.
This article’s title may have intrigued
you because of the name of “katas.”
Kata (literally: “form”), is a Japa-
nese word used to describe detailed
choreographed patterns of movements
practiced either solo or in pairs. It’s
often the term used to describe practic-
ing specific movements in martial arts.
Code katas are practice movements
we can use to sharpen our testing—
most specifically TDD—muscles in the
same way martial arts katas are used to
make people better.


There are, in various lists you’ll find
online, more katas than I can count. At
the end of this article, I’ll share a short
list of some of the better places to look.
Today we’ll focus on some specific
steps of working through a single kata.
What all katas have in common is you’ll
generally work through one like this:


  1. Start by having a reasonable test
    setup. For me, that’s usually two
    files open, one for my tests and one
    for my production code.

  2. Read over the direction for the kata
    you’re doing. In general, it’s pretty
    short, a few paragraphs at most.

  3. For about 25 minutes, work
    through the Red, Green, Refactor
    process mentioned above (and
    elaborated more below), solving the
    kata.

  4. After your time finishes, put your
    fingers down and throw the code
    away.
    It can feel weird the first few times
    you do this to stop abruptly, in whatever
    state of completion you find your-
    self and then to throw away the work.
    However, those steps are crucial to the
    process. Remember, we’re writing katas
    to get better at the TDD process and not
    to finish something we need to do for
    work. We do katas to hone technique
    rather than to complete a goal.


The Kata We’ll be Doing
Our kata is pretty simple: we’re
making a Roman numeral converter.
It’ll take Arabic (or normal) numbers
and return the string which represents
them in the (strange and inefficient)
Roman numbering system.
So when we pass 1 we’ll get I and for
2018 we’ll get MMXVIII. I could go on, but
think an encyclopedia would fill in any
gap in understanding you may have
better than I can.
If time allows, the next step of the
kata is to build the reverse direction:
something which takes strings of
Roman numerals and returns the inte-
ger. I rarely get to this in the time I allow
myself for katas, but you may find the
time.

Set Up Steps for Our Roman
Numeral Kata
The Getting Started^1 page from
PHPUnit is quite good. For doing this
kata, I’ve typically used the global
PHPUnit PHAR (archive) file. I prefer
this because it stops me from adding to
what can otherwise be a two-file opera-
tion a whole vendor directory and need
for Composer.
When using PHPUnit in projects, I
prefer to use the Composer installation
method. It prevents the issue where
different developers on your team have
different PHPUnit’s installed globally
and so get different output from your
test suite.

Doing a Code Kata
In general, as we’ve discussed, the
cycle you’ll use in doing a code kata is
the following:


  1. Identify a scenario your code
    should handle, and write a test you
    can foresee failing.

  2. Confirm the test does fail and
    PHPUnit gives you a “red” failure.

  3. Write just enough production code
    that you expect the test to pass.

  4. Confirm the test passes (are
    “green”). (Repeat step three if not.)

  5. If you need, refactor the passing
    code to better reflect “good code”
    practices. Keep all the tests passing
    when you do this.
    That’s it. You can naturally break
    down the steps more or less, but you get
    the basic idea right there. Write a test,
    see it fail, write code, see the test(s) pass,
    and then do any general refactoring if
    you need to.


A Few Steps Through the Kata
We’ll do a few instructive steps and
leave the rest to you when you truly get
a chance to practice it.

Getting Set Up
As mentioned above, my preference
for using PHPUnit when doing katas
is to keep the folder structure simple.
I’ll typically have a src.php file, for the

1 Getting Started:
https://phpa.me/phpunit-started-7
Free download pdf