BDD风格的单元测试框架

dev-master 2014-08-22 13:46 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:46:45 UTC


README

Phew 是一个类似于 Jasmine 的 PHP 测试框架。目前它处于探索阶段。

本项目的目标是让测试的编写尽可能简单,就像在 Jasmine 中一样。

Build Status

代码示例

<?php
describe('Phew', function () {
    it('should be easy to write PHP tests', function () {
        expect('writing PHP tests')->not->toBe('difficult');
    });
});

安装

强烈建议使用 Composer 安装 Phew - http://getcomposer.org/

运行以下命令

$ composer global require janvoracek/phew:dev-master

尝试运行 phew。如果命令未找到,您需要将 $HOME/.composer/vendor/bin(Mac)或 %APPDATA%/Composer/bin(Win)添加到您的 PATH 中。

规范

Phew 被设计成尽可能接近 Jasmine。最大的差异是由 PHP 和 JS 之间的差异引起的

  • 不同的对象操作符。JS 使用 "点" (.),PHP 使用 "箭头" (->)。
  • 闭包的 use 语句。这使得测试看起来不太美观。请见谅 :)

匹配器

目前只提供了一套基本的匹配器

严格的相等匹配器

  • toBe
  • toBeNull
  • toBeTrue
  • toBeFalse

松散的相等匹配器

  • toEqual
  • toBeEmpty
  • toBeTruthy
  • toBeFalsy

类型匹配器

  • toBeA
  • toBeAn(toBeA 的别名)
  • toBeInstanceOf(toBeA 的别名)
  • toImplement(toBeA 的别名)

字符串匹配器

  • toStartsWith
  • toMatch

自定义匹配器

添加自定义匹配器非常简单。您的匹配器需要实现 Phew\Matchers\Matcher 接口,并且您需要注册它。

示例匹配器

class GreaterThanMatcher implements Matcher {

    /** @var number */
    private $minimum;
    /** @var number */
    private $actual;

    public function __construct($minimum = null) {
        $this->minimum = $minimum;
    }

    public function matches($actual) {
        $this->actual = $actual;
        return $actual > $this->minimum;
    }

    public function getFailureMessage() {
        return "Expected {$this->actual} to be greater than {$this->minimum}";
    }

    public function getNegativeFailureMessage() {
        return "Expected {$this->actual} not to be greater than {$this->minimum}";
    }
}

注册

\Phew\Expectations\Expectation::addMatcher('toBeGreaterThan', 'GreaterThanMatcher');

版权(c)2013 Jan Voracek。本软件许可协议为 MIT 许可证。