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!
Prerequisites
- PhpStorm IDE
- Local PHP installation (preferrably PHP 7)
Create kata project
- Create a new project of type “PHP Empty Project”
- 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
- 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"] } } }
- Go to the menu Tools > Composer > Add dependency…. Type
phpunit/phpunit
and select the latest non-dev version:
- 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:
- 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!'); } }
- 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'”
- 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 explicitinclude
statements or writing our own autoloader.
- Then click “Run”. Now you should see this:
- 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:
- 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 thetestTest
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); } }
- Running the test shows an error (of course, the class
Game
does not exist yet):
- Create the file
src/Bowling/Game.php
as follows:<?php namespace Katas\Bowling; class Game { }
- 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 usingcomposer dump-autoload
on the command line
If the command “composer” is not found, enter the full path tocomposer.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.