Quality Assurance on PHP projects - PHPUnit part 2
Note: This article was originally published at Planet PHP
on 22 August 2011.
A A A I hope everyone enjoyed my first article on unit testing with phpunit where I started writing a few tests that would guide us building our little game of tictactoe. Today I'm going start with turning these tests into working code and adjusting our tests to have a clear separation of responsibility. Since we already know what the code should produce, we only have to work out the details.Our tests tell us we have four classes:
- Tictactoe: the main class that is responsible for the game and how it should be played
- Grid: is the class that's responsible for setting up the playing grid
- Players: a collection class containing both player objects
- Player: the class defining a single player
public function testGameGridIsSetAtStart()
{
$grid = $this-_ttt-getGrid();
$this-assertInstanceOf('Grid', $grid);
$this-assertEquals(3, count($grid-getRows()));
foreach ($grid-getRows() as $row) {
$this-assertInternalType('array', $row);
$this-assertEquals(3, count($row));
$this-assertNull($row[0]);
$this-assertNull($row[1]);
$this-assertNull($row[2]);
}
} What can we learn from this test?
- Grid is part of our main TicTacToe class
- has a method that fetches rows of type array
- and each row has columns as an array
- Grid class will instantiate a grid with 3 rows and 3 columns
- When calling "getRows()" we retrieve an array of 3 columns
- Each field in the grid will have a null value as a default
- A Player must have a way to set his "symbol" on a specific position on the grid
- Must have a way to verify if a given symbol exists in either a horizontal, a vertical or a diagonal row
/**
* TicTacToe
*
* A simple game that's played with two players, each taking a turn by marking
* a field in a grid of 3 x 3 with either an X or an O (one symbol per player).
* Winner is the one who has 3 identical symbols in a single horizontal,
* vertical or diagonal row.
*
* @package Tictactoe
* @license "Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)"
* @link http://creativecommons.org/licenses/by-sa/3.0/
*/
/**
* Grid
*
* This Grid class is responsible to set up and maintain the playing field
*
* @package Tictactoe
* @category Tictactoe
*
*/
class Grid
{
/**
* Constant to define the number of rows
* @var int
*/
const ROWS = 3;
/**
* Constant to define the number of colomns
* @var int
*/
const COLS = 3;
/**
* Container for all rows and columns
*
* @var array
*/
protected $_rows = array ();
/**
* Constructor for this Grid class that will set up our grid with 3 rows
* and 3 columns while setting the value of each field to NULL
*/
public function __construct()
{
for ($i = 0; $i $columns = array ();
for ($j = 0; $j $columns[$j] = null;
}
$this-addRow($columns);
}
}
/**
* Adds a row to the grid, requiring an array of 3 fields representing
* the columns
*
* @param array $row
* @return Grid
*/
public function addRow(array $row)
{
$this-_rows[] = $row;
return $this;
}
/**
* Retrieves all rows from this grid, including an array for the columns
* for each row
*
* @return array
*/
public function getRows()
{
return $this-_rows;
}
/**
* Sets the symbol for each field
*
* @param int $row The position of the field in the row
* @param int $column The position of the field in the column
* @param string $symbol
*/
public function setSymbol($row, $column, $symbol)
{
$this-_rows[$row][$column] = $symbol;
return $th
Truncated by Planet PHP, read more at the original (another 13924 bytes)


