定制 texthtml/doctest 二次开发

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

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

texthtml/doctest

Composer 安装命令:

composer create-project texthtml/doctest

包简介

Test code examples in PHP comments

README 文档

README

Test your docblock code examples.

doctest looks for php examples in your functions, methods classes & interfaces comments and execute them to ensure they are correct.

How to write examples

The simplest way is to add codeblocks in your comments, and use assert() to check for invariants. If, when executing the example there is an exception, the example will be marked as failed.

/**
 * Compute the factorial of a non-negative $n
 *
 * ```php
 * assert(factorial(0) === 1);
 * assert(factorial(5) === 120);
 * ```
 */
function factorial(int $n): int {
    if ($n === 0) {
        return 1;
    }

    if ($n < 0) {
        throw new \InvalidArgumentException("unexpected negative integer: $n");
    }

    return $n * factorial($n - 1);
}

Assertions

One easy way to throw exceptions when something is unexpected is to use assert(). But to make it nicer to write assertions and have better error messages automatically, you can call the assertion helpers from webmozart/assert with the self::assert* shortcut:

self::assertEq(factorial(0), 1);
self::assertEq(factorial(5), 120);

Testing exceptions

Sometimes we want to show that some code will throw an exception, we can do that by adding an inline comment anywhere in the example with the following format: // @throws [exception class] [exception message]

// @throws InvalidArgumentException unexpected negative integer: -10
factorial(-10);

Note: code after the exception won't be executed so any in-code assertions coming after the first exception won't be verified.

Testing output

By default doctest will make an example fail if it produce any output unless it's documented with one or more @prints inline comments anywhere in the example with the following format: `// @prints [text]. That's to make sure that any output produced is expected.

echo factorial(6); // @prints 720
echo factorial(10);
// the @prints annotations can be anywhere, only their order is important, not their exact positions
// @prints 3628800

Installation

composer req --dev texthtml/doctest

Usage

Simply run the doctest command and it will look for examples to test in your src/ folder. Call doctest --help for customizing where to look for examples and other options.

$ ./bin/doctest examples/factorial.php
 [OK] factorial#1 (examples/factorial.php:7)
 [OK] factorial#2 (examples/factorial.php:14)
 [ERROR] factorial#3 (examples/factorial.php:20)
         Expected output to be "". Got: "720"
 [OK] factorial#4 (examples/factorial.php:25)
 [ERROR] factorial#5 (examples/factorial.php:30)
         unexpected negative integer: -10
 [OK] factorial#6 (examples/factorial.php:35)
 [ERROR] factorial#7 (examples/factorial.php:43)
         Expected a value equal to 3628890. Got: 3628800
 [OK] Number of success: 4
 [ERROR] Number of failures: 3

TODO

  • Junit output

texthtml/doctest 适用场景与选型建议

texthtml/doctest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 31
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2022-07-16