monadial/phpunit-docrunner
Composer 安装命令:
composer require monadial/phpunit-docrunner
包简介
Rust-style doc tests for PHP - run code examples in docblocks as PHPUnit tests
README 文档
README
Rust-style doc tests for PHP. Write executable code examples in your docblocks or as PHP attributes, and run them as PHPUnit tests.
Requirements
- PHP 8.4+
- PHPUnit 12.0+
Installation
composer require monadial/phpunit-docrunner
Usage
Write PHP code blocks in docblocks or use #[DocTest] attributes:
class Calculator { /** * ```php * $result = Calculator::add(2, 3); * self::assertEquals(5, $result); * ``` */ public static function add(int $a, int $b): int { return $a + $b; } }
use Monadial\DocRunner\Attribute\DocTest; class Calculator { #[DocTest(<<<'PHP' $result = Calculator::add(2, 3); self::assertEquals(5, $result); PHP)] public static function add(int $a, int $b): int { return $a + $b; } }
Run:
vendor/bin/doctest run --directory=src
Directives
| Directive | Docblock | Attribute | Behavior |
|---|---|---|---|
| (default) | ```php |
#[DocTest(code)] |
Assertions must pass |
should_panic |
```php should_panic |
Directive::ShouldPanic |
Passes if code throws |
ignore |
```php ignore |
Directive::Ignore |
Not generated |
no_run |
```php no_run |
Directive::NoRun |
Syntax-checked only |
CLI Commands
vendor/bin/doctest run [--directory=src] [--no-cache] [--phpunit-binary=...] vendor/bin/doctest generate [--directory=src] [--no-cache] vendor/bin/doctest lint [--directory=src] [--no-cache]
- run (default) — generate tests and run them with PHPUnit
- generate — generate test files without running
- lint — generate and syntax-check without running
All commands accept --directory (repeatable), --config, and --no-cache.
PHPUnit Extension
Add to phpunit.xml for automatic integration:
<extensions> <bootstrap class="Monadial\DocRunner\Integration\PHPUnit\DocTestExtension"> <parameter name="directories" value="src/" /> <parameter name="cache-dir" value=".doctest-cache/" /> </bootstrap> </extensions> <testsuites> <testsuite name="DocTests"> <directory>.doctest-cache/generated</directory> </testsuite> </testsuites>
The extension generates tests during bootstrap and warns if the generated directory isn't in your test suite config.
Configuration
Optional doctest.xml for project defaults:
<?xml version="1.0"?> <doctest> <directories> <directory>src/</directory> </directories> <exclude> <pattern>*Test.php</pattern> </exclude> <cache-dir>.doctest-cache/</cache-dir> <bootstrap>vendor/autoload.php</bootstrap> </doctest>
How It Works
Source files are parsed via AST (nikic/php-parser) — no files are require-ed. Code blocks are extracted from docblocks and #[DocTest] attributes, then generated as PHPUnit TestCase classes. Each source file produces one test class; each code block becomes one test method. Namespace and imports carry over from the source file.
Generated tests are cached by content hash. Unchanged files are skipped on subsequent runs.
License
MIT
monadial/phpunit-docrunner 适用场景与选型建议
monadial/phpunit-docrunner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 monadial/phpunit-docrunner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 monadial/phpunit-docrunner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-15