Get Ready for TDD Katas with PHPUnit + PhpStorm

Are you a PHP developer looking to start with unit tests? Do you want to follow my weekly TDD Kata posts but don’t know how to start? Let’s get you set up step by step, so that you are ready to write your first test!

Read the first weekly Kata post to learn what it is all about.

Prerequisites

  • PhpStorm IDE
  • Local PHP installation (preferrably PHP 7)

Create kata project

  1. Create a new project of type “PHP Empty Project”

  2. Go to the menu Tools > Composer > Init Composer… and enter the path to PHP and composer. If you don’t have composer installed, PhpStorm can download it for you right there

  3. A new file composer.json opens. Change the project details if you like, and add an “autoload” section:
    {
      "name": "my/katas",
      "description": "My TDD Katas",
      "minimum-stability": "stable",
      "license": "proprietary",
      "authors": [
        {
          "name": "me",
          "email": "me@example.com"
        }
      ],
      "autoload": {
        "psr-4": {
          "Katas\\": ["src", "tests"]
        }
      }
    }
    
  4. Go to the menu Tools > Composer > Add dependency…. Type phpunit/phpunit and select the latest non-dev version:

  5. Click “Install”, composer will now install PHPUnit which might take a few minutes. When it’s done, click “Close”

Your first test

Let’s start with the Bowling Game kata:

  1. Create a file tests/Bowling/GameTest.php as follows:
    <?php
    namespace Katas\Bowling;
    class GameTest extends \PHPUnit_Framework_TestCase
    {
        public function testTest()
        {
            $this->fail('Hello!');
        }
    }
  2. The first test I create in a new project is always one that tests if the test framework itself is running properly. Let’s try to execute this test so that we see the “Hello!” fail message. Right click the test in the project view and select “Run ‘GameTest'”

  3. It is the first time we run a PHPUnit test in this project, so we have to configure the environment a bit. Click “Fix” and choose to load PHPUnit from the composer autoloader (vendor/autoload.php in your project). This will also enable us to use our own source code without explicit include statements or writing our own autoloader.

  4. Then click “Run”. Now you should see this:

  5. Let’s make the test pass
    <?php
    namespace Katas\Bowling;
    class GameTest extends \PHPUnit_Framework_TestCase
    {
        public function testTest()
        {
            $this->assertTrue(true);
        }
    }

    Now that we have configured how to run it, there’s a “run” button on the top right. Click it:

    This is the result:

  6. OK, we’ve seen a failing and a passing test, but did not test anything yet. The next thing we are testing is if we can instantiate our Game class (we don’t need the testTest method anymore, delete it).
    <?php
    namespace Katas\Bowling;
    class GameTest extends \PHPUnit_Framework_TestCase
    {
        public function testGameCanBeInstantiated()
        {
            $game = new Game();
            $this->assertInstanceOf(Game::class, $game);
        }
    }
  7. Running the test shows an error (of course, the class Game does not exist yet):

  8. Create the file src/Bowling/Game.php as follows:
    <?php
    namespace Katas\Bowling;
    class Game
    {
    }
    
  9. Run the test again, it should pass. If not, double check the autoload settings in composer.json. If you make changes to them, you need to update the autoloader using composer dump-autoload on the command line

    If the command “composer” is not found, enter the full path to composer.phar instead.

Congratulations, you are now ready to write your first real test!

<?php
namespace Katas\Bowling;
class GameTest extends \PHPUnit_Framework_TestCase
{
    public function testGutterGame()
    {
        $game = new Game();
        for ($i = 0;  $i < 20; ++$i) {
            $game->roll(0);
        }
        $this->assertEquals(0, $game->score());
    }
}

From here on you should be able to follow the steps in the Bowling Game PowerPoint. The Java code can be written very similar in PHP.