PHP code example of kodhe / test

1. Go to this page and download the library: Download kodhe/test 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/ */

    

kodhe / test example snippets


$this->load->library('unit_test');

// Simple equality test
echo $this->unit->run(1 + 1, 2, 'Addition Test');

// Type checking
echo $this->unit->run('hello', 'is_string', 'String Type Check');

// With notes
echo $this->unit->run(true, true, 'Boolean Test', 'This should pass');

// Enable strict mode
$this->unit->use_strict(true);
echo $this->unit->run("1", 1, 'Strict Comparison'); // Will fail

// Disable testing
$this->unit->active(false);

// Custom template
$this->unit->set_template('<table>{rows}</table>');

// Get raw results
$results = $this->unit->result();

// Reset
$this->unit->reset();

use Kodhe\Test\Runner\TestRunner;
use Kodhe\Test\Result\TestResultCollection;
use Kodhe\Test\Reporters\DefaultReporter;

$runner = new TestRunner();
$result = $runner->run(
    test: 42,
    expected: 42,
    testName: 'Answer to Everything',
    strict: false,
    file: __FILE__,
    line: __LINE__,
    notes: ''
);

$collection = new TestResultCollection();
$collection->add($result);

$reporter = new DefaultReporter();
echo $reporter->report($collection);

use Kodhe\Test\Contracts\AssertionInterface;
use Kodhe\Test\Result\AssertionResult;
use Kodhe\Test\ValueObjects\TestStatus;

class IsUuidAssertion implements AssertionInterface
{
    public function execute($test, $expected, bool $strict): AssertionResult
    {
        $passed = preg_match(
            '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i',
            $test
        ) === 1;
        
        return new AssertionResult(
            'UUID Test',
            $expected,
            $test,
            'uuid',
            new TestStatus($passed ? TestStatus::PASS : TestStatus::FAIL),
            $passed ? '' : 'Invalid UUID format'
        );
    }
    
    public function supports($expected): bool
    {
        return $expected === 'is_uuid';
    }
    
    public function getName(): string
    {
        return 'uuid';
    }
}

// Register custom assertion
$registry = new AssertionRegistry();
$registry->register('is_uuid', new IsUuidAssertion());

// Old code continues to work
$this->load->library('unit_test');
echo $this->unit->run($test, $expected, 'Test Name');

// Use internal components directly
$runner = new TestRunner();
// ... more control over test execution