Tina4 Testing

Tina4 has a built in testing system, to assist with test driven development. It utilizes both composer scripts and phpDoc Comments as part of the testing system.

Declaring a test

Test declarations are included in the phpDoc Comments, and can be included above a function, class or class method. It includes two parts, the test declaration and then a list of tests. A test has two parts, the condition and then the message displayed, should there be a failure.

/**
* @tests
* assert 1 === 1, This will always be true
* assert 2 === 2, This will also, always be true
*/
Synopsis of a test
 /**
 *
 * @tests
 * Test declaration  brackets infer calling numberAdd      $a       $b           eval    , Message to show when broken
 * assert            (                                     5,       5          )  ===  10, 5 + 5 should be 10 
 **/
 function numberAdd($a, $b) {
 
   return $a + $b;
 } 

Running the tests

Tests can be run from the terminal in your IDE, either using composer scripts, or a php command. The required composer scripts are built into your composer.json file when you ran the tina4 initialize. The two lines in your composer.json file of importance are

..
"test": "@tina4 tests:run",
"test:verbose": "@tina4 tests:verbose",
..

The tests can be called using either of these calls, with verbose giving a result for each test, as opposed to just a result for the set of tests under a @tests.

composer test
composer test:verbose

Grouping tests

Tests can be grouped to help simplify test results, especially when focussing in on an area of code under development. Test groups are declared by a comma delimited list

..
* @tests Test1, another test, "Main group"
..

and then called by appending one or more to the tests call

composer test another test, Test1

This will return all the tests declared with either another test or Test1.

Formulating a basic test

So the formulation of a basic test is made up of

  1. The assert, to determine this is the start of a test.
  2. The (1,2) essentially is running the function with the variables given and then returning a value
  3. The returned value is then tested against the condition thus 3 === 3 so the test will pass.
  /**
   * Function to add two numbers together
   * @param $num1
   * @param $num2
   * @return mixed
   * @tests document
   *   assert (1, 2) === 3, The return can be tested with simple operators
   */
  function addNumbers($num1, $num2){
      return $num1 + $num2;
  }

So using verbose tests this will produce

composer test:verbose

BEGINNING OF TESTS
=====================================================
Testing Function addnumbers
# 1 addnumbers: Passed ((1, 2) == "3") 100%
=====================================================
END OF TESTS

Testing operators

The following operators are available for use, and behave as expected in php

 ==, ===, !=, !==, <=, >=, <, >
Powered by ComboStrap