PHP code example of khalyomede / matcha

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

    

khalyomede / matcha example snippets




use function Khalyomede\Style\expect;

describe('trim', function() {
  it('should return the same string if the string has no spaces around', function() {
    expect( trim('hello world') )->toBe()->equalTo('hello world');
  });
});

return run();



use function Khalyomede\Style\expect;

describe('empty', function() {
  it('should return true if the string has no characters', function() {
    expect( empty('') )->toBe()->true();
  });
});

return run();



use function Khalyomede\Style\expect;

describe('isset', function() {
  it('should return false if the variable does not exists', function() {
    expect( isset($GLOBALS['php6']) )->toBe()->false();
  });
});

return run();



use function Khalyomede\Style\expect;

describe('array-sum', function() {
  it('should not return null', function() {
    expect( array_sum([1, 2, 3]) )->not()->toBe()->null();
  });
});

return run();



use function Khalyomede\Style\expect;

describe('echo', function() {
  it('it should display the correct message', function() {
    expect(function() {
      echo 'hello world';
    })->toDisplay('hello world');
  });
});

return run();



use function Khalyomede\Style\expect;

describe('empty', function() {
  it('it should return true if an array is empty', function() {
    expect( empty([]) )->toBe()->aBoolean();
  });
});

return run();



use function Khalyomede\Style\expect;

describe('json', function() {
  it('should be a valid json string', function() {
    expect('{"hello": "world"}')->toBe()->aString()->inJsonFormat();
  });
});

return run();



use function Khalyomede\Style\expect;

describe('database connectivity', function() {
  it('should be reachable', function() {
    expect([
      'driver' => 'mysql', 
      'host' => 'ensembldb.ensembl.org', 
      'user' => 'anonymous'
    ])->toBe()->aDatabase()->thatIsAccessible();
  });
});

return run();



use function Khalyomede\Style\expect;
use Khalyomede\ReportLevel;

describe('array', function() {
  it('should merge two arrays', function() {
    expect( array_merge([1, 2, 3], [4, 5, 6]) )->toBe()->equalTo([1, 2, 3, 4, 5, 6]);
  });

  it('should diff two array', function() {
    expect( array_count_values([1, 1, 3]) )->toBe()->equalTo([1 => 2, 3 => 1]);
  });

  it('should shuffle an array', function() {
    $array = [1, 2];

    expect( shuffle($array) )->toBe()->anArray();
  });
});

report('detailed');
// or
report(ReportLevel::DETAILED);

return run();

use function Khalyomede\Style\expect;

describe('abs', function() {
  it('it should give the absolute value for a positive value', function() {
    expect(abs(-10 + 2))->toBe()->equalTo(8);
  });

  it('should give the absolute value for a positive value', function() {
    expect(abs(10 + 2))->toBe()->equalTo(12);
  });
});



use function Khalyomede\Style\expect;

describe('trim', function() {
  it('should return a string when triming a string', function() {
    expect(trim('hello world'))->toBe()->aString();
  });

  it('should return a string even if triming null', function() {
    expect(trim(null))->toBe()->aString();
  });

  it('should return the same string when triming a string without spaces around', function() {
    expect(trim('hello world'))->toBe()->equalTo('hello world');
  });

  it('should return the string without spaces around if triming a string with spaces around', function() {
    expect(trim(' hello world '))->toBe()->equalTo('hello world');
  });
});

describe('empty', function() {
  it('should return true if checking null', function() {
    expect(empty(null))->toBe()->strictly()->true();
  });

  it('should return true if checking false', function() {
    expect(empty(false))->toBe()->true();
  });

  it('should return true if checking an empty array', function() {
    expect(empty([]))->toBe()->true();
  });
});

describe('isset', function() {
  it('should return false if a variable is not set', function() {
    expect(isset($php6))->toBe()->false();
  });

  it('should return true if an array is set', function() {
    expect(isset($_GET))->toBe()->true();
  });
});

return run();

function expect($mixed): Expect



use function Khalyomede\Expect;

expect( empty('hello world') );



use function Khalyomede\Expect;

expect(function() {
  throw new Exception('exception manually thrown');
});

public function not(): Expect



use function Khalyomede\Style\expect;

describe('empty', function() {
  it('should not return true if the string is not empty', function() {
    expect( empty('hello world') )->not()->toBe()->true();
  });
});

return run();

public function strictly(): Expect



use function Khalyomede\Style\expect;

describe('int cast', function() {
  it('should return the integer equivalent of the string representation of a number', function() {
    expect((int) '1')->toBe()->strictly()->equalTo(1);
  });
});

return run();

public function toBe(): Expect



use function Khalyomede\Style\expect;

describe('trim', function() {
  it('should return the same string if it has no spaces around', function() {
    expect( trim('hello world') )->toBe()->equalTo('hello world');
  });
});

return run();

public function equalTo($mixed): Expect



use function Khalyomede\Style\expect;

describe('implicit cast', function() {
  it('should implicitly cast the string representation of a number', function() {
    expect('1')->toBe()->equalTo(1)
  });
});

return run();

function report(string $level): void

use Khalyomede\ReportLevel;

ReportLevel::DETAILED;
ReportLevel::NORMAL;
ReportLevel::REDUCED;