~thirdplace/components

ae9aa50678265c88e2858c930572df6ffdd792d3 — Dag 2 years ago 00aa4fd
feat: add xunit test runner
6 files changed, 118 insertions(+), 0 deletions(-)

A bin/tunit
M composer.json
R test/cache.php => tests/cache.php
R test/container.php => tests/container.php
R test/router.php => tests/router.php
R test/url.php => tests/url.php
A bin/tunit => bin/tunit +114 -0
@@ 0,0 1,114 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);

namespace Thirdplace;

function assertNull($value, string $message = null)
{
    if ($value !== null) {
        fail(null, $value, $message);
    }
}

function assertTrue($value, string $message = null)
{
    if ($value !== true) {
        fail(true, $value, $message);
    }
}

function assertFalse($value, string $message = null)
{
    if ($value !== false) {
        fail(false, $value, $message);
    }
}

function assertEquals($expected, $actual, string $message = null)
{
    if ($expected != $actual) {
        fail($expected, $actual, $message);
    }
}

function assertSame($expected, $actual, string $message = null)
{
    if ($expected !== $actual) {
        fail($expected, $actual, $message);
    }
}

function expectException(\Closure $fn, string $expected = \Exception::class, string $message = null)
{
    try {
        $fn();
        fail($expected, '', $message);
    } catch (\Exception $e) {
        if (! $e instanceof $expected) {
            fail($expected, get_class($e), $message);
        }
    }
}

function fail($expected, $actual, string $message = null)
{
    $stackFrame = debug_backtrace()[1];
    printf("\nFAIL at %s line %s\n", $stackFrame['file'], $stackFrame['line']);
    if ($message) {
        printf("Message : %s\n", $message);
    }
    printf("Expected: %s\n", toString($expected));
    printf("Actual:   %s\n", toString($actual));
}

function toString($value)
{
    if ($value === null) {
        return '[null]';
    }
    if (is_bool($value)) {
        return $value ? '[true]' : '[false]';
    }
    if (is_string($value) || method_exists($value, '__toString')) {
        return "'$value'";
    }
    return print_r($value, true);
}

function main($argc, $argv): void
{
    set_error_handler(function ($code, $message, $file, $line) {
        throw new \ErrorException($message, 0, $code, $file, $line);
    });

    $base = $argv[1] ?? './';

    if (!is_dir($base)) {
        exit(sprintf("Folder not found: %s\n", $base));
    }

    $autoloader = "$base/vendor/autoload.php";
    if (is_file($autoloader)) {
        require $autoloader;
    }

    $testsFolder = "$base/tests";
    if (!is_dir($testsFolder)) {
        exit(sprintf("Tests folder not found: %s\n", $testsFolder));
    }

    $tests = glob($testsFolder . '/*.php');
    if (!$tests) {
        exit("Tests folder is empty\n");
    }

    foreach ($tests as $test) {
        require $test;
        print '.';
    }

    print "\n";
}

main($argc, $argv);

M composer.json => composer.json +4 -0
@@ 35,6 35,7 @@
        ]
    },
    "bin": [
        "./bin/tunit"
    ],
    "require": {
        "php" : ">=7.4",


@@ 47,5 48,8 @@
    },
    "require-dev": {
        "thirdplace/tunit": "dev-main"
    },
    "scripts": {
        "test": "./bin/tunit"
    }
}

R test/cache.php => tests/cache.php +0 -0
R test/container.php => tests/container.php +0 -0
R test/router.php => tests/router.php +0 -0
R test/url.php => tests/url.php +0 -0