PHP code example of joefallon / kisstest

1. Go to this page and download the library: Download joefallon/kisstest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joefallon / kisstest example snippets


use JoeFallon\KissTest\UnitTest;

// Pull in the configuration for autoloading, database cleaning, etc.
lliTimespanTests();
new tests\JoeFallon\KissTest\Reporting\SummaryTests();
new tests\JoeFallon\KissTest\Reporting\TestCaseResultTests();
new tests\JoeFallon\KissTest\Reporting\UnitTestResultTests();

// more tests here...

// Display the summary.
UnitTest::getAllUnitTestsSummary();

use JoeFallon\AutoLoader;
use JoeFallon\Database\PdoFactory;

// Define the include paths.
define('BASE_PATH', realpath(dirname(__FILE__).'/../../'));
define('SRC_PATH',  BASE_PATH.'/src');
define('VEND_PATH', BASE_PATH.'/vendor');

// Set the application include paths for autoloading.
set_include_path(get_include_path().':'.SRC_PATH.':'.BASE_PATH);

// Composer autoloading.
DB_PASS, DB_NAME);
$pdo->exec('SET FOREIGN_KEY_CHECKS=0;');
$pdo->exec('TRUNCATE TABLE `gtwy_tests`');
$pdo->exec('TRUNCATE TABLE `join_tests`');
$pdo->exec('SET FOREIGN_KEY_CHECKS=1;');

// It is recommend, but not at all  class under test except with the additional
// namespace 'tests\' prefixed to the front. For example, the class under test
// is in the namespace 'JoeFallon\KissTest\Reporting'. Therefore, the unit test
// class is placed in the namespace 'tests\JoeFallon\KissTest\Reporting'.
namespace tests\JoeFallon\KissTest\Reporting;

// Class Under Test
use JoeFallon\KissTest\Reporting\MilliTimespan;
// Unit Test Base Class
use JoeFallon\KissTest\UnitTest;

class MilliTimespanTests extends UnitTest
{
    public function test_elapsed_time_is_always_zero_if_not_started()
    {
        $timespan = new MilliTimespan();
        $expected = 0;
        usleep(100);
        $actual = $timespan->getElapsedTimeInMilliSec();

        $this->assertEqual($expected, $actual, "", 0.01);
    }

    public function test_elapsed_time_is_positive_if_started()
    {
        $timespan = new MilliTimespan();
        $timespan->startTimer();
        usleep(100);
        $timespan->stopTimer();
        $elapsedTime = $timespan->getElapsedTimeInMilliSec();

        $this->assertFirstGreaterThanSecond($elapsedTime, 0.0);
    }

    // more test cases here...
}

// $first    - First test operand
// $second   - Second test operand
// $failMsg  - Message to display on test failure
// $maxDelta - Maximum allowable absolute difference between two operands
//             when comparing floating point numbers for equality.
// $value    - Test operand when only one exists (i.e. true/false asserts)

assertEqual($first, $second, $failMsg="", $maxDelta=0.0)
assertNotEqual($first, $second, $failMsg="")

assertFirstGreaterThanSecond($first, $second, $failMsg="")
assertFirstGreaterThanOrEqualSecond($first, $second, $failMsg="")

assertFirstLessThanSecond($first, $second, $failMsg="")
assertFirstLessThanOrEqualSecond($first, $second, $failMsg="")

assertTrue($value, $failMsg="")
assertFalse($value, $failMsg="")

namespace tests\Example;

use JoeFallon\Example\ClassUnderTest;
use JoeFallon\KissTest\UnitTest;

class ClassUnderTestTests extends UnitTest
{
    public function test_something()
    {
        $this->notImplementedFail();
    }

    // more test cases here...
}

namespace tests\Example;

use JoeFallon\Example\ClassUnderTest;
use JoeFallon\KissTest\UnitTest;

class ClassUnderTestTests extends UnitTest
{
    public function setUp()
    {
        // perform set up tasks before the start of each test case...
    }

    public function tearDown()
    {
        // perform tear down tasks after each test case is complete...
    }

    // test cases here...
}

namespace tests\Example;

use JoeFallon\Example\ClassUnderTest;
use JoeFallon\KissTest\UnitTest;

class ClassUnderTestTests extends UnitTest
{
    public function test_exception_is_thrown()
    {
        try
        {
            // place test code that should throw exception here...
        }
        catch(Exception $e)
        {
            // An exception was thrown. Yay!
            $this->testPass();
            return;
        }

        $this->testFail();
    }

    // more test cases here...
}

namespace MyProject;

class ProductionClass
{
    public function exampleMethod(DependencyClass $dependency)
    {
        $param1 = 5;
        $param2 = 'abc';
        $result = $dependency->timeConsumingCalculation($param1, $param2);

        return $result;
    }
}

namespace MyProject;

class DependencyClass
{
    public function timeConsumingCalculation($param1, $param2)
    {
        // perform very time consuming calculations...
        return 42;
    }
}

namespace tests\MyProject;

use MyProject\DependencyClass;
use JoeFallon\KissTest\Mock;

class DependencyClassMock extends DependencyClass
{
    public $_mock;

    // Replace the default constructor.
    public function __construct()
    {
        $this->_mock = new Mock();
    }

    public function timeConsumingCalculation($param1, $param2)
    {
        // The contents every public method is replaced with the
        // following.
        $args = array($param1, $param2);
        return $this->_mock->methodCalled('timeConsumingCalculation', $args);
    }
}

namespace tests\MyProject;

use MyProject\DependencyClass;
use JoeFallon\KissTest\Mock;
use JoeFallon\KissTest\UnitTest;

class DependencyClassTests extends UnitTest
{
    public function test_example_method_has_correct_object_interactions()
    {
        // Create the mock and specify how it should behave.
        $mock = new DependencyClassMock();
        $mock->setMethodReturnValue('timeConsumingCalculation', 42);

        // Create the class under test.
        $productionClass = new ProductionClass();
        $result = $productionClass->exampleMethod($mock);

        // Assert the return value matches what is expected.
        $this->assertEqual(42, $result);

        // Assert the method was called exactly once.
        $callCount = $mock->_mock->getMethodCallCount('timeConsumingCalculation');
        $this->assertEqual(1, $callCount);

        // Assert the proper arguments were passed to the method.
        $args = $mock->_mock->getMethodArgs('timeConsumingCalculation', 1);
        $this->assertEqual(5,     $args[0]);
        $this->assertEqual('abc', $args[1]);
    }
}

ProjectDirectory
 |
 +-->src
 |    |
 |    +--> Folder1
 |    |     |
 |    |     +--> CodeFile1.php
 |    |
 |    +--> Folder2
 |          |
 |          +--> CodeFile2.php
 |
 +-->tests
       |
       +--> Folder1
       |     |
       |     +--> CodeFile1Tests.php           <-- unit tests for CodeFile1.php
       |
       +--> Folder2
       |     |
       |     +--> CodeFile2Tests.php           <-- unit tests for CodeFile2.php
       |
       +--> index.php                          <-- test suite definition