PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Quality Assurance on PHP projects - PHPUnit part 4

Note: This article was originally published at Planet PHP on 4 September 2011.
Planet PHP
A A AIn parts one, two and three we focussed on writing tests for a game of tic-tac-toe, with in parts two and three we optimized our tests so they focus on the functionality of the individual parts Grid and Player, with a collection class Players to handle Player objects.

In this fourth part we should focus on playing the game. Let's go over the steps again:

  • each player chooses a symbol
  • for each turn (max 9 turns)
    • a player places his symbol on the grid
    • if 3 symbols appear in a single row (horizontal, vertical or diagonal)
      • player is a winner
So this is a simple and a straightforward rule we can turn into code. But let's look at the tests we've written in part 1 to see if they're still valid.
public function testGameCanBePlayed()
{
$playerX = $this-_ttt-getPlayers()-seek(0)-current();
$playerO = $this-_ttt-getPlayers()-seek(1)-current();

$this-assertFalse($this-_ttt-play(0, 0, $playerX));
$this-assertFalse($this-_ttt-play(0, 1, $playerX));
$this-assertTrue($this-_ttt-play(0, 2, $playerX));

$this-_ttt-setGrid(new Grid());

$this-assertFalse($this-_ttt-play(0, 0, $playerX));
$this-assertFalse($this-_ttt-play(1, 0, $playerX));
$this-assertTrue($this-_ttt-play(2, 0, $playerX));

$this-_ttt-setGrid(new Grid());

$this-assertFalse($this-_ttt-play(0, 0, $playerX));
$this-assertFalse($this-_ttt-play(1, 1, $playerX));
$this-assertTrue($this-_ttt-play(2, 2, $playerX));

$this-_ttt-setGrid(new Grid());

$this-assertFalse($this-_ttt-play(0, 2, $playerX));
$this-assertFalse($this-_ttt-play(1, 1, $playerX));
$this-assertTrue($this-_ttt-play(2, 0, $playerX));
}As you see, we only tested the functionality to see if we can have a winner when 3 identical symbols appear in a single row horizontal, vertical or diagonal. We can make a couple of comments on this test:
  • this test is named wrong as it doesn't test the gameplay functionality
  • this test should be branched out into different tests for each row
  • this test doesn't test turn-by-turn game play
In other words, we need to clean this up and come up with better tests! Let's focus on tearing this test into 3 separate tests, testing just a single direction. Again, we can use a dataProvider method for this.
public function rowColProvider()
{
return array (
array (array (array (0,0), array (0,1), array (0,2))),
array (array (array (0,0), array (1,0), array (2,0))),
array (array (array (0,0), array (1,1), array (2,2))),
array (array (array (0,2), array (1,1), array (2,0))),
);
}
/**
* @dataProvider rowColProvider
*/
public function testGameplayCanDetectWinner($rowCols)
{
$player = $this-_ttt-getPlayers()-seek(0)-current();
$this-assertFalse($this-_ttt-play($rowCols[0][0], $rowCols[0][1], $player));
$this-assertFalse($this-_ttt-play($rowCols[1][0], $rowCols[1][1], $player));
$this-assertTrue($this-_ttt-play($rowCols[2][0], $rowCols[2][1], $player));
}Now we know that with each turn, the return value will indicate if we have a winner (TRUE) or not (FALSE). But we still need to see if we can have the same result when playing with two players. As you see with the last line, the third entry gives us a positive result.

But we still don't have a gameplay going! We need to have two players enter the arena and each player playing turn by turn. So how do we approach this? Well, the easiest way for now is to create a test that does just that. The benefit here is we can decide which player is going to win the game.
public function testGameCanBePlayed()
{
$playerX = $this-_ttt-getPlayers()-seek(0)-current();
$playerO = $this-_ttt-getPlayers()-seek(1)-current();
$this-

Truncated by Planet PHP, read more at the original (another 6836 bytes)