承接 bound1ess/essence 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

bound1ess/essence

Composer 安装命令:

composer require --dev bound1ess/essence

包简介

Highly opinionated PHP assertion framework providing clean and flexible BDD style API.

README 文档

README

Essence is a very flexible BDD style assertion framework for PHP that fits into existing PHPUnit projects nicely.

Installation

composer require --dev bound1ess/essence

The Idea

In most PHP testing frameworks, you are tied to concrete matcher names (e.g., assertEqual, shouldHaveType). I don't like that. That's why I created Essence.

Usage

In order to run a matcher you need to specify it in the query string. So what is a query string? Have a look:

this("someValue")->should_have_length_of(10); # => "someValue should have length of 10"
expect(123)->toBeAbove(120); # => "expect 123 to be above 120"

$elements = [1, 2, 3, 4, 5];
these($elements)->values->should_contain(5); # => "these elements should contain a value '5'"

expect(null)->not()->to()->beNull(); => "expect NULL not to be NULL"

Yes, Essence is smart enough to handle all these cases just as you would expect it to do. So, how do you build a query string (or assertion)?

  1. Decide if you need to configure a matcher you plan to use. As for now there are two only matchers that can be used in configuration mode - ValuesMatcher and KeysMatcher.
  2. Determine what matcher you will need to use to get the job done. Is it ThrowMatcher, or, say, RespondMatcher?
  3. Add some links to make the assertion readable.
  4. Choose an appropriate entry point (expect, this, these etc).
  5. Pass a proper value and arguments.
  6. If you want to, add ->go() to immediately perform validation. I'll tell you more about that later.

Configuration

First of all, Essence leverages the singleton pattern to persist all its important data during the runtime. It means that this expression will always be equal to true:

spl_object_hash(essence()) == spl_object_hash(essence());

You can configure Essence by using configure method:

essence()->configure(function($config) {
    return array_merge($config, [
        "exception" => "Your\Custom\AssertionException",
    ]);
});

Available configuration options:

Name Possible value
exception fully qualified class name (as a string)
implicit_validation a boolean value (true or false)
links an array (won't be merged automatically)
matchers an associative array name => aliases (won't be merged automatically)

Explicit and implicit, validateAll and PHPUnit extension

If you don't want to write ->validate() or ->go() every time, you can enable implicit validation:

essence()->configure(function() {
    return [
        "implicit_validation" => true,
    ];
});

It will validate the last (previous) assertion when you create a new one. Or, even better, just use the PHPUnit extension as shown below:

class MyTestCase extends Essence\Extensions\PhpunitExtension
{

    // Your assertions here.
}

It'll do the job for you, no need to configure anything or call go/validate.

Verbose mode

This line of code will throw an Essence\Exceptions\AssertionException by default:

expect(10)->to_be_equal_to(15)->validate(); // You can also use "go" instead of "validate".

However, if you pass true to validate/go, Essence will dump all important data and just exit.

expect(10)->to_be_equal_to(15)->validate(true);
vendor/bin/phpunit
# ........
Value: 10
Arguments:
  #1: 15

Cheatsheet

Entry points

  • essence
  • it
  • that
  • this
  • these
  • those

Links

  • of
  • have
  • be
  • at
  • to

Matchers

Name Aliases
TypeMatcher an, a, type
ContainMatcher contain, include
PositiveMatcher ok, fine
TrueMatcher true
FalseMatcher false
NullMatcher null
EmptyMatcher empty
EqualMatcher equal
LikeMatcher like
AboveMatcher above
BelowMatcher below
LeastMatcher least
MostMatcher most
WithinMatcher within
LengthMatcher length
MatchMatcher match
KeysMatcher key, keys
ValuesMatcher value, values
ThrowMatcher throw, raise
RespondMatcher respond
CloseMatcher close

License

The MIT License (MIT).

Development

The Makefile contains all sorts of useful tasks.

Running tests

make run-tests

Creating code coverage report

make coverage-report coverage-report-server

Building documentation

make build-docs docs-server

bound1ess/essence 适用场景与选型建议

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

它主要适用于以下技术方向: 「framework」 「testing」 「BDD」 「assertion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 65
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-09