khalyomede/matcha

单元测试库,口感好。

v0.13.2 2018-10-23 21:21 UTC

README

单元测试库,口感好。

PHP from Packagist Packagist Codeship Packagist

Matcha logo

Gif of an example of usage

摘要

安装

在您的项目根目录下

composer require --dev khalyomede/matcha:0.*

示例

示例 1:测试代码是否返回一个字符串

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 2:测试一个值是否为真

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 3:测试代码是否返回 false

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 4:测试表达式的负性

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 5:测试是否已显示消息

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 6:测试变量是否返回期望的类型

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 7:测试字符串格式

require(__DIR__ . '/../vendor/autoload.php');

use function Khalyomede\Style\expect;

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

return run();

示例 8:测试数据库是否可访问

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 9:使控制台报告详细

require(__DIR__ . '/../vendor/autoload.php');

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();

示例 10:在单个文件上使用 matcha 控制台命令

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);
  });
});
$ bin/matcha example/tests/example-10.php

  2018-10-13 18:55:11.628200  ⓘ  Running tests for "abs"
  2018-10-13 18:55:11.630700  ⚐  2 tests completed, 0 tests failed
  2018-10-13 18:55:11.630800  ⚐  tests ran in 0.0015 sec. (+0.0018 sec.)
  2 / 2 ▓▓ 100 %

示例 11:在文件夹上使用 matcha 控制台命令

检查 /example/tests,所有以 .php 结尾的文件。您不受 .test.php 扩展的限制,可以省略它。

$ bin/matcha example/tests/

  2018-10-13 18:58:05.348300  ⓘ  Running tests for "abs"
  2018-10-13 18:58:05.351300  ⓘ  Running tests for "array_sum"
  2018-10-13 18:58:05.351400  ⓘ  Running tests for "count"
  2018-10-13 18:58:05.351500  ⚐  8 tests completed, 0 tests failed
  2018-10-13 18:58:05.351500  ⚐  tests ran in 0.0019 sec. (+0.0022 sec.)
  8 / 8 ▓▓▓▓▓▓▓▓ 100 %

完整示例

此示例旨在向您展示如何将这些函数混合在一起。

require(__DIR__ . '/../vendor/autoload.php');

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();

API

expect

返回一个新的 Expect 实例。

function expect($mixed): Expect
require(__DIR__  '/../vendor/autoload.php');

use function Khalyomede\Expect;

expect( empty('hello world') );
require(__DIR__  '/../vendor/autoload.php');

use function Khalyomede\Expect;

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

not

断言我们期望测试的逆结果。

public function not(): Expect
require(__DIR__ . '/../vendor/autoload.php');

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();

strictly

断言我们期望测试也进行类型测试(这将防止 PHP 在运行测试时执行隐式转换)。

public function strictly(): Expect
require(__DIR__ . '/../vendor/autoload.php');

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();

toBe

断言我们正在测试相等性。

public function toBe(): Expect
require(__DIR__ . '/../vendor/autoload.php');

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();

equalTo

断言我们正在测试对特定值的相等性。

public function equalTo($mixed): Expect
require(__DIR__ . '/../vendor/autoload.php');

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

可用的报告级别为 detailednormal(默认)和 reduced

报告级别可以通过 Khalyomede\ReportLevel 类使用。

use Khalyomede\ReportLevel;

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

鸣谢