定制 php-kitchen/code-specs 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

php-kitchen/code-specs

Composer 安装命令:

composer require php-kitchen/code-specs

包简介

BDD extension for PHPUnit that allows to write tests as a specifications in a human-readable format.

README 文档

README

Build Status Code Coverage Code Quality Latest Stable Version Monthly Downloads Total Downloads License

NOTICE: Library is under refactoring for release of V5.0.0 and docs reflect the new style of specs

Code Specs isn't just another tests library - it's the way you design your solutions. The Specs Way.

Code Specs is built as a PHPUnit plugin(with Codeception support as well) for writing BDD style Unit tests in a specification way using human-readable format.

Goal of Code Specs is to add a bunch of cool tools for unit testing and show a way of representing unit tests as a behavior specifications that describes specific class behavior in variety of use-cases.

The min hero of Code Specs that does the magic is Tester. Tester represents an actor who ensures you code pass specifications(yes, like in Codeception - this library expired by Codeception). See iy by yourself:

namespace Specs\Unit;

use PHPKitchen\CodeSpecs\Base\Specification;
use PHPKitchen\CodeSpecs\Actor\I;

/**
 * Specification of {@link IncomeCalculator}
 *
 * @author Dima Kolodko <prowwid@gmail.com>
 */
class IncomeCalculatorTest extends Specification {
    private const EXPECTED_TAX_FOR_FIRST_LEVEL_TAX_RULE = 4500;
    private const EXPECTED_TAX_FOR_SECOND_LEVEL_TAX_RULE = 7200;
    private const EXPECTED_TAX_FOR_THIRD_LEVEL_TAX_RULE = 30000;
    private const INCOME_AFTER_APPLYING_FIRST_LEVEL_TAX_RULE = 300000;

    /**
     * @test
     */
    public function calculateTaxBehavior() {
        $clientsPayments = []; // dummy variable, just for example
        $hoursSpentWorking = 160; // dummy variable, just for example
        $service = new IncomeCalculator($clientsPayments, $hoursSpentWorking);
        I::describe('income tax calculations');

        I::expect('for income less that 50 000 calculator use 10% tax rule', function () use ($service) {
            I::lookAt('first level income tax')
                ->seeNumber($service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_FIRST_LEVEL_TAX_RULE);
        });

        I::expect('for income between 50 000 and 100 000 calculator use 12% tax rule', function () use ($service) {
            I::lookAt('second level income tax')
                ->seeNumber($service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_SECOND_LEVEL_TAX_RULE);
        });

        I::expect('for income more than 100 000 calculator use 20% tax rule', function () use ($service) {
            I::lookAt('second level income tax')
                ->seeNumber($service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_THIRD_LEVEL_TAX_RULE);
        });
    }

    /**
     * @test
     */
    public function calculateWithTaxBehavior() {
        $clientsPayments = []; // dummy variable, just for example
        $hoursSpentWorking = 160; // dummy variable, just for example
        $service = new IncomeCalculator($clientsPayments, $hoursSpentWorking);

        I::describe('Net income calculation');
        I::expect('calculator calculates income with tax using 10% tax rule for income less that 50 000');

        I::lookAt('income tax')
            ->seeNumber($service->calculateWithTax())
            ->isNotEmpty()
            ->isEqualTo(self::INCOME_AFTER_APPLYING_FIRST_LEVEL_TAX_RULE);


    }
}

CodeSpecs also decorates errors output so, for example, if "IncomeCalculator" service from example above will incorrectly calculate income the error output will include following message(example of output in PHPStorm):

picture alt

Requirements

PHP >= 7.4 is required.

PHPUnit >= 9.1 is required.

Getting Started

Run the following command to add CodeSpecs to your project's composer.json. See Packagist for specific versions.

composer require --dev php-kitchen/code-specs

Or you can copy this library from:

Then you can use CodeSpecs in your test simply extending from Specification class. Example:

use PHPKitchen\CodeSpecs\Base\Specification;

class YourTest extends Specification {

    /**
     * @test
     */
    public function myMethodBehavior() {
        I::lookAt('my dummy variable')
            ->seeBool(true)
            ->isFalse();
    }
}

Note: If you want to use CodeSpecs with Codeception read Codeception integration guide

For additional information and guides go to the project documentation See changes log for information about changes in releases and update guide for information about upgrading to a next major version.

Contributing

If you want to ask any questions, suggest improvements or just to talk with community and developers, join our server at Discord Read organization contributing rules for additional information.

Spreading the Word

Acknowledging or citing CodeSpecs is as important as direct contributions.

If you are using CodeSpecs as part of an OpenSource project, a way to acknowledge it is to use a special badge in your README: Tested By

If your code is hosted at GitHub, you can place the following in your README.md file to get the badge:

[![CodeSpecs](https://img.shields.io/badge/Tested_By-CodeSpecs-brightgreen.svg?style=flat)](https://github.com/php-kitchen/code-specs)

or use regular HTML:

<a href="https://github.com/php-kitchen/code-specs"><img src="https://img.shields.io/badge/Tested_By-CodeSpecs-brightgreen.svg" alt="Tested By"></a>

php-kitchen/code-specs 适用场景与选型建议

php-kitchen/code-specs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 977 次下载、GitHub Stars 达 8, 最近一次更新时间为 2017 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 php-kitchen/code-specs 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 977
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 4
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

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