PHP Unit testing

PHP Unit testing is based on the works of Sebastian Bergmann.
Documentation of what can be tested you can find here.

You can place your test in the folder 'tests'.

In this folder you will find a file named 'ApiTestCase.php'. Don't remove this file otherwise it won't work.

The structure of a test is a follows:

sample:

<?php
define('INC_ROOT', dirname(__FILE__, 2));
require INC_ROOT . '/vendor/autoload.php';
require INC_ROOT . '/tests/ApiTestCase.php';

class FirstTest extends ApiTestCase
{
    public function testJoburg()
    {
        $this->request('GET', '/server/joburg', [], '/server/index.php');

        $this->assertThatResponseHasStatus(200);
        $this->assertThatResponseHasContentType('text/html; charset=UTF-8');
        $this->assertContains('jojo', $this->response->getBody()->__toString());
    }
}

How to run a test

You can run a test by using the command line in the terminal and change the folder to the root of your server. This is the same folder where the 'test' folder is in.

Now enter (sample):

./vendor/bin/phpunit ./test/TestToRun.php

When everything is fine, the output will look like:

PHPUnit 5.4.8 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 2.65 seconds, Memory: 6.00MB

OK (1 test, 3 assertions)
Last update: Tue, 13 Sep 2022 14:32:15