PHP code example of xp-forge / assert

1. Go to this page and download the library: Download xp-forge/assert 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-forge / assert example snippets


use unittest\assert\{Assert, All};

class ExampleTest {

  #[@test]
  public function succeeds() {
    Assert::that(['The Dude'])->hasSize(1)->contains('The Dude');
  }

  #[@test]
  public function fails() {
    All::of(function() { 
      Assert::that('localhost')->startsWith('www');
      Assert::that('example.com')->endsWith('.org');
    });
  }
}

$tim= new Person(6100, 'Tim Tailor');
Assert::that($tim)->extracting('name')->isEqualTo('Tim Tailor');

$tim= new Person(6100, 'Tim Tailor');
Assert::that($tim)->extracting(function($p) { return $p->name(); })->isEqualTo('Tim Tailor');

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting('age')->isEqualTo(42);

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting(['name', 'age'])->isEqualTo(['Test', 42]);

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting(['identifier' => 'id'])->isEqualTo(['identifier' => 6100]);

$people= [new Person(1, 'Queen'), new Person(2, 'King')];
Assert::that($people)->extracting('name')->isEqualTo(['Queen', 'King']);