toast/unit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

toast/unit

Composer 安装命令:

composer require toast/unit

包简介

Super-simple unit testing for PHP7

README 文档

README

Toast is a super-simple testing framework for PHP, inspired by Javascript testing frameworks like Karma, Jasmine etc.

Toast was born out of frustration with existing testing frameworks which (in our view) are needlessly bulky, complex and difficult to setup. Some are also excruciatingly slow (looking at you, PHPUnit).

Features

  • Very fast
  • Assertions using native PHP assert
  • Feature descriptions using DocComments
  • Grouping of related tests

Installation

$ composer require --dev toast/unit

Create a Toast.json config file in the root of your project. It should contain at least a "tests" key pointing to the directory where you've placed your tests. Tests may be placed in (sub)directories; Toast will recurse.

Optionally the config can also contain a "bootstrap" key with an array of files to include prior to running. These can contain your project's setup (e.g. dependency injection logic).

Turn on assertions and configure them to throw AssertionError on failure. See this section in the manual; both values should be set to 1.

Configuration

Place a configuration file in de root of your project (or optionally specify it as a parameter when running Toast). The file supports all formats Kingconf supports; we'll use JSON here.

An example config file:

{
    "tests": "path/to/my/tests"
}

Yep, that's it :)

Usage

$ vendor/bin/toast

That's it :) You may optionally speficy a --filter= parameter containing a regular expression. In that case only tests with matching file names will be run. Filters are case-insensitive and "@" is used as a delimiter.

Other optional flags are:

  • -v for verbose mode
  • -o for output mode (see below)

Writing tests

This couldn't be simpler! Each test(group) is a callable. Toast assumes any callable returning a Generator is a group; other callables are the actual tests. For example:

<?php

/** Description of this group */
return function () : Generator {
    /** Test if true == true */
    yield function () {
        assert(true);
    };
};

You can have as many assertions per test as you like, but typically it is best practice to limit yourself to as little as possible (preferably one) and group related tests using a generator:

<?php

return function () : Generator {
    $obj = new My\Thing\Under\Test;
    /** Test method foo */
    yield function () use ($obj) {
        assert($obj->foo());
    };
    /** Test method bar */
    yield function () use ($obj) {
        assert($obj->bar());
    };
};

Nesting can go as deep as makes sense for your project.

Setup

The test callables are invoked with $this bound to the actual test instance (an instance of Toast\Unit\Test). This exposes a beforeEach method accepting a callable to be called before each test in that group (tests in a nested group do not inherit them). Multiple calls to beforeEach can be made.

<?php

return function () : Generator {
    $this->beforeEach(function () {
        echo "1\n";
    });
    yield function () : Generator {
        $this->beforeEach(function () {
            echo "2\n";
        });
        yield function () { assert(true); };
    };
    yield function () {
        assert(true);
    };
};

In the above example, "1" will get output twice since the beforeEach is valid for the two top-level yields. "2" will be ouptut once since it only applies to the nested yield, which in turn knows nothing about its parent.

You can use beforeEach to e.g. load a database fixture. How you do that is up to you.

Teardown

Similarly, if you need to teardown there is an afterEach method that works the same way.

Cap'n obvious here, but both beforeEach as well as afterEach will only apply to tests getting yielded after the respective calls. This is because PHP halts execution internally inside a generator. For complex tests it might make sense to add before/after callables mid-group, but usually you should just place them at the beginning. If you find yourself needing to add them mid-group, it's usually a sign you should break up your test group into smaller units.

Testing other things than simple assertions

Toast assumes a succesfull test is a passed assertion. But what it you need to test if something throws an exception? Simple:

<?php

//...
yield function () {
    $foo = new Foo;
    $e = null;
    try {
        $foo->bar();
    } catch (FooException $e) {
    }
    assert($e instanceof FooException);
};

Toast also assumes a test does not yield output. So to test if a function actually does yield output, use output buffering:

<?php

//....
yield function () {
    ob_start();
    thisFunctionPrintsSomething();
    assert(ob_end_clean() == "Hello world!");
};

You can optionally specify the -o parameter when invoking Toast to turn this feature off. This eases development since you can more easily var_dump stuff until you get a working test.

Detecting a Toast run

Toast sets an environment variable TOAST your code can check for, e.g. to know which database you want to use (development or test):

<?php

if (getenv("TOAST")) {
    $db = new PDO('mysql:test');
} else {
    $db = new PDO('mysql:dev');
}

There is also a (unique) TOAST_CLIENT set you can use to identify a particular run of Toast. This would be useful if e.g. your tested feature stores something somewhere you would need to be able to identify.

toast/unit 适用场景与选型建议

toast/unit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 965 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 toast/unit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 toast/unit 我们能提供哪些服务?
定制开发 / 二次开发

基于 toast/unit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 965
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 39
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-02