Skip to main content

Quality Assurance on PHP projects - PHPUnit part 4

     In 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->assertFalse($this->_ttt->play(0, 0, $playerX));
        $this->assertFalse($this->_ttt->play(0, 1, $playerO));
        $this->assertFalse($this->_ttt->play(1, 1, $playerX));
        $this->assertFalse($this->_ttt->play(2, 2, $playerO));
        $this->assertFalse($this->_ttt->play(1, 0, $playerX));
        $this->assertFalse($this->_ttt->play(2, 0, $playerO));
        $this->assertTrue($this->_ttt->play(1, 2, $playerX));
    }
Visual this result looks like the following grid:
 X | O |   
 X | X | X 
 O |   | O 
But most importantly our tests are still green, giving us that wonderful feeling of achievement.

Let's finish up our tests so we can see no one can play any further after we've got a winner. PHPUnit provides a nice anotation we can use for this: depends. So now we can test that the game is stopped, depending on our test "testGameCanBePlayed".
For this to work, we need to return our game (in this case $this->_ttt). Just add the following line at the bottom of the test class "testGameCanBePlayed".
    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, $playerO));
        $this->assertFalse($this->_ttt->play(1, 1, $playerX));
        $this->assertFalse($this->_ttt->play(2, 2, $playerO));
        $this->assertFalse($this->_ttt->play(1, 0, $playerX));
        $this->assertFalse($this->_ttt->play(2, 0, $playerO));
        $this->assertTrue($this->_ttt->play(1, 2, $playerX));
        return $this->_ttt;
    }
And the next step is simple:
    /**
     * @depends testGameCanBePlayed
     * @expectedException RuntimeException
     */
    public function testGameStopsAfterWinning($game)
    {
        $playerO = $game->getPlayers()->seek(1)->current();
        $game->play(2,1, $playerO);
    }
In order for this test to succeed we need to add a couple of things to our game:
  • a status property that will tell us if there's a winner
  • a setter method to flip the flag once a winner is detected
  • a checking method to verify a winner is not yet detected
  • modify our play method so it throws a RuntimeException when we try to play when a winner exists.
<?php
...

class Tictactoe
{
    ...

    /**
     * Status indicator to say there's a winner or not
     * 
     * @var bool
     */
    protected $_winner = false;

    ...

    /**
     * Sets a flag to indicate this game has a winner
     * 
     * @param bool $flag
     * @return Tictactoe
     */
    public function setWinner($flag = true)
    {
        $this->_winner = $flag;
        return $this;
    }
    /**
     * Checks if the game has a winner
     * 
     * @return bool
     */
    public function hasWinner()
    {
        return $this->_winner;
    }
    /**
     * Plays the game and returns TRUE if a player has become a winner, FALSE
     * if the player is not (yet) a winner.
     * 
     * @param int $row
     * @param int $column
     * @param Player $player
     * @return bool
     * @throws RuntimeException
     */
    public function play($row, $column, Player $player)
    {
        if ($this->hasWinner()) {
            throw new RuntimeException('Game already has a winner');
        }
        $this->getGrid()->setSymbol($row, $column, $player->getSymbol());
        return $this->isWinner($player);
    }
    /**
     * Returns TRUE if a player has become a winner, FALSE if not.
     * 
     * @param Player $player
     * @return bool
     */
    public function isWinner(Player $player)
    {
        if ($this->getGrid()->inRow($player->getSymbol())) {
            $this->setWinner();
            return true;
        }
        if ($this->getGrid()->inColumn($player->getSymbol())) {
            $this->setWinner();
            return true;
        }
        if ($this->getGrid()->inDiagonal($player->getSymbol())) {
            $this->setWinner();
            return true;
        }
        return false;
    }
}
That's it. We covered the main purpose of the game and we did it semi test driven. We can start playing a cute little game of Tictactoe and covered a couple important features of PHPUnit. I also showed that it's not a bad thing if you modify parts of your tests to achieve new or other specifications.

The full game source code can be found on my github account (https://github.com/DragonBe/tictactoe), and as you go over the log, you can follow along with these series as well. When playing with the source code, you might think about testing edge cases: playing 9 rounds and no winner, try to place a symbol off the grid, place twice a symbol on the grid of a single player (cheating), and so on.

Another thing could be that you turn this little game into an online game. What are the things you need to concider in that situation. Are our tests still valid or do we need to modify our architecture and our tests to support that kind of game playing?

Since it's on GitHub, you can fork it and send me a pull request once you have an edge case figured out.

Comments

Popular posts from this blog

PHP 7 and Apache on macOS Sierra

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install. The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands. I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial. APACHE AND PHP WITH HOMEBREW I’ve made this according to the installation instructions given on GetGrav. The installation procedures These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4. Install Xcode command line tools (if not done yet)xcode-select --install Install Homebrew/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" Set up for in…

Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second… With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path. Our challenge One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis. Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem. Warning: session_start(): Failed to read session data: redis (path: tcp://127.0.0.1:6379) Searching the internet for this error, we didn’t got many hits that could point us into a dire…

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…