sassnowski/pest-plugin-contract-tests
Composer 安装命令:
composer require --dev sassnowski/pest-plugin-contract-tests
包简介
A plugin to define an implement contract tests in Pest
README 文档
README
This plugin provides a way to easily define and implement contract tests in Pest. To learn more about what contract tests are and when you might use them, check out this blog post I wrote.
Installation
Install the plugin via composer:
composer require sassnowski/pest-plugin-contract-tests
Usage
To define a new contract test suite, you may use the contractTest function provided by the plugin.
// tests/Contracts/FilesystemContract.php <?php use function Sassnowski\PestContractTests\contractTest; contractTest(Filesystem::class, function (Closure $getInstance) { it('can save a file for the first time', function () use ($getInstance) { /** @var Filesystem $fs */ $fs = $getInstance(); expect($fs->fileExists('::filename::'))->toBeFalse(); $fs->storeFile( new MockFile('::contents::'), '::filename::', ); expect($fs->fileExists('::filename::'))->toBeTrue(); }); it('throws an exception if a file with the same name already exists', function () use ($getInstance) { /** @var Filesystem $fs */ $fs = $getInstance(); $fs->storeFile( new MockFile('::contents::'), '::filename::' ); // Trying to store another file with the same name should blow up. $fs->storeFile( new MockFile('::contents::'), '::filename::' ); })->throws( FileAlreadyExistsException::class, "The file '::filename::' already exists" ); });
The contractTest method takes two parameters:
- The name of the contract test. While this can be any string, it is recommended to use the fully-qualified class name (FQCN) of the interface described by the contract test.
- A closure that defines the actual test cases for the contract. Everything inside this closure works exactly like
vanilla Pest. So, you can use all goodies you're used to like higher-order tests or the
throwsmethod to check for exceptions.
Calling contractTests doesn't actually register any tests it. All it does is make the contract tests available to be
used inside other test files. To actually register the tests for a particular class, you may use the implementsContract
function.
// tests/InMemoryFilesystemTest.php <?php use function Sassnowski\PestContractTests\implementsContract; implementsContract(Filesystem::class, fn () => new InMemoryFilesystem());
The implementsContract function takes the name of the contract test, as well as a factory function which needs to
return an instance of the class you want to test. This factory function is the $getInstance parameter that gets passed
to the second parameter of the contractTest function.
This will now register all test cases that were registered inside the contract name for the InMemoryFilesystem class.
$ vendor/bin/pest
PASS Tests\InMemoryFilesystemTest
✓ it can save a file for the first time 0.12s
✓ it throws an exception if a file with the same name al… 0.08s
Credits
License
MIT
sassnowski/pest-plugin-contract-tests 适用场景与选型建议
sassnowski/pest-plugin-contract-tests 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 255 次下载、GitHub Stars 达 6, 最近一次更新时间为 2022 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「testing」 「plugin」 「test」 「unit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sassnowski/pest-plugin-contract-tests 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sassnowski/pest-plugin-contract-tests 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sassnowski/pest-plugin-contract-tests 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
Alfabank REST API integration
The testing extension of TYPO3voilà.
Auto-generate dummy data with realistic relationships per Model-like config classes
统计信息
- 总下载量: 255
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-20