codeception/domain-assert 问题修复 & 功能扩展

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

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

codeception/domain-assert

Composer 安装命令:

composer require codeception/domain-assert

包简介

Custom assertions for PHPUnit and Codeception

README 文档

README

Assertion library for PHPUnit or Codeception powered by Symfony Expression Language.

Pitch

This tiny library helps you to create domain-specific assertions in tests:

Instead of:

$this->assertTrue($user->isValid(), 'user is valid');

use

$this->assertUserIsValid($user);

Why?

See how test fails in first example:

user is valid
Failed asserting that false is true.

And how test fails in second example:

Failed asserting that `user.isValid()`.
[user]: User Object &000000005689696e000000004066036e (
    'role' => 'guest'
)

What makes more sense to you? In second example you get the business logic behind the assertion as well as values passed into it. That's why if you have business logic in your project domain-assert is your choice.

How To Use It

Install this package:

composer require codeception/domain-assert --dev

Create a trait with a custom assertion. We recommend using traits as you can reuse them accross different test cases.

use Codeception\DomainRule;

trait CustomAssertion
{
    public function assertValidUser(User $user)
    {
        $this->assertThat(
            ['user' => $user], 
            new DomainRule('user and user.isValid()')
        );
    }
}

In this example we check that $user exists and $user->isValid() return true;

That's all! Now inject this trait to TestCases and use it.

class UserTest extends \PHPUnit\Framework\TestCase
{
    use CustomAssertion;
}

Defining Business Rules

This library uses Expression Language to define domain rules for assertions.

Let's define a rule to check if we have enough products in the stock:

stock and product.getStock() == stock and product.getAmount() > amount

We have 3 items here: product, stock, and amount which is a number of items we need. Let's create assertion according to this rule:

public function assertEnoughProductsInStock(Stock stock, Product product, amount)
{
    $this->assertThat([
            'product' => $product,
            'stock' => $stock, 
            'amount' => $amount
        ], 
        new DomainRule('stock and product.getStock() == stock and product.getAmount() > amount')
    );
}

Now it can be used inside your tests:

$product = new Product('iPhone');
$stock->addProduct($product);
$stock->addProduct($product);
$stock->addProduct($product);
$this->assertEnoughProductsInStock($stock, $product, 2);

Advanced Concepts

  • Instead of $this->assertThat you can call static version of this method: PHPUnit\Framework\Assert::assertThat
  • Codeception\DomainRule extends PHPUnit\Framework\Constraint.
  • Codeception\DomainRule uses Symfony\Component\ExpressionLanguage\ExpressionLanguage
  • Expression Language can be extended by calling $domainRule->getLanguage()
  • assertThat can receive first parameter as scalar value. In this case it will be treated as expected inside an expression:
public function assertIsGreaterThanMinimal()
{
    $this->assertThat(
        $minimalPrice,
        new DomainRule('expected > 1000')
    );    
}

License

Open-source software licensed under the MIT License. © Codeception PHP Testing Framework

codeception/domain-assert 适用场景与选型建议

codeception/domain-assert 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 146.15k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2017 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 codeception/domain-assert 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 146.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 17
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-12