PHP code example of xp-framework / unittest

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

    

xp-framework / unittest example snippets


use unittest\{Assert, Test};

class CalculatorTest {

  #[Test]
  public function addition() {
    Assert::equals(2, 1 + 1);
  }
}

public abstract class unittest.Assert {
  public static void equals(var $expected, var $actual, string $error)
  public static void notEquals(var $expected, var $actual, string $error)
  public static void true(var $actual, string $error)
  public static void false(var $actual, string $error)
  public static void null(var $actual, string $error)
  public static void instance(string|lang.Type $type, var $actual, string $error)
  public static void throws(string|lang.Type $type, callable $block)
}

use unittest\{Assert, Before, After, Test};

class CalculatorTest {
  private $fixture;

  #[Before]
  public function newFixture() {
    $this->fixture= new Calculator();
  }

  #[After]
  public function cleanUp() {
    unset($this->fixture);
  }

  #[Test]
  public function addition() {
    Assert::equals(2, $this->fixture->add(1, 1));
  }
}

use lang\IllegalArgumentException;
use unittest\{Test, Expect};

class CalculatorTest {

  #[Test, Expect(IllegalArgumentException::class)]
  public function cannot_divide_by_zero() {
    (new Calculator())->divide(1, 0);
  }
}

use unittest\{Test, Ignore};

class EncodingTest {

  #[Test, Ignore('Does not work with all iconv implementations')]
  public function transliteration() {
    /* ... */
  }
}

use lang\IllegalArgumentException;
use unittest\{Test, Expect, Values};

class CalculatorTest {

  #[Test, Expect(IllegalArgumentException::class), Values([1, 0, -1])]
  public function cannot_divide_by_zero($dividend) {
    (new Calculator())->divide($dividend, 0);
  }
}

use unittest\actions\{IsPlatform, VerifyThat};
use unittest\{Test, Action};

class FileSystemTest {

  #[Test, Action(eval: 'new IsPlatform("!WIN")')]
  public function not_run_on_windows() {
    // ...
  }

  #[Test, Action(eval: 'new VerifyThat(fn() => file_exists("/\$Recycle.Bin");')]
  public function run_when_recycle_bin_exists() {
    // ...
  }
}