Are you aphp 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!

Create a file tests/Bowling/GameTest.php as follows: <?phpnamespace 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 explicit include statements or writing our own autoloader.
Then click “Run”. Now you should see this:
Let’s make the test pass <?phpnamespace 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 the testTest method anymore, delete it). <?phpnamespace 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: <?phpnamespace 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 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!
<?phpnamespace Katas\Bowling;class GameTest extends \PHPUnit_Framework_TestCase{ public function testGutterGame() { $game = new Game(); for ($i = 0; $i < 10; ++$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.