PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Quality Assurance on PHP projects - PHPUnit part 3

Note: This article was originally published at Planet PHP on 21 April 7000.
Planet PHP
Time for the third part on unit testing with phpunit in my Quality Assurance on PHP projects series. In part one we started writing unit tests for a simple game of tic-tac-toe. In part two we started converting our unit tests into actual code and moved our general unit test code for grids into a Grid focussed unit test. In this part, we're looking at how we can optimize the tests for our players.

When we look at our initial tests for players, we only focussed on two things: each player has a symbol and players are managed within a collection class.
public function testGamePlayersAreSetAtStart()
{
$players = $this-_ttt-getPlayers();
$this-assertInstanceOf('Players', $players);
$this-assertEquals(2, count($players));
$this-assertEquals(Player::PLAYER_X, $players-seek(0)-current()-getSymbol());
$this-assertEquals(Player::PLAYER_O, $players-seek(1)-current()-getSymbol());
}As you can imagine, this is a very minimalistic test that cannot cope with all player related issues. We did have two classes to handle all player related issues and thus we need to move our test out of our TictactoeTest class.

A quick look at the functionality of a player should give us more fuel to creat better unit tests. Bytaking a new look at the functionality of a single player, we can optimize our tests. Since we already have some working code, let's take a quick look at the Player class to figure out what is being expected.
/**
* 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/
*/
/**
* Player
*
* This Player class is responsible to set up and maintain a single player
*
* @package Tictactoe
* @category Tictactoe
*
*/
class Player
{
/**
* Defines the symbol X for a player to choose
*
* @var string
*/
const PLAYER_X = 'X';
/**
* Defines the symbol X for a player to choose
*
* @var string
*/
const PLAYER_O = 'O';
/**
* The symbol a player chooses
*
* @var string
*/
protected $_symbol;
/**
* Constructor for a single player, with an optional symbol (X or O) to
* define the player's playing symbol
*
* @param string $symbol
*/
public function __construct($symbol = null)
{
if (null !== $symbol) {
$this-setSymbol($symbol);
}
}
/**
* Sets a symbol for this Player
*
* @param string $symbol
* @return Player
*/
public function setSymbol($symbol)
{
$this-_symbol = (string) $symbol;
return $this;
}
/**
* Retrieves the chosen symbol from this Player
*
* @return string
*/
public function getSymbol()
{
return $this-_symbol;
}
}As you can see, we only care about the symbol of the player. Apparently this is more than enough for now. But when we check our main TictactoeTest we see no specific tests for our Player class. About time to do something about it.
class PlayerTest extends PHPUnit_Framework_TestCase
{
protected $_player;
protected function setUp()
{
$this-_player = new Player();
parent::setUp();
}
protected function tearDown()
{
$this-_player = null;
parent::tearDown();
}
public function testPlayerHasNoSymbolAtStartup()
{
$this-assertNull($this-_player-getSymbol());
}
public function goodSymbolProvider()
{
return array (
array ('O'),
array ('X'),
);
}
/**
* @dataProvider goodSymbolProvider
*/
public function testPlayerCanSe

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