spatie/pest-expectations
Composer 安装命令:
composer require spatie/pest-expectations
包简介
A collection of handy custom Pest customisations
README 文档
README
This repo contains custom expectations to be used in a Pest test suite.
It also contains various helpers to make testing with Pest easier. Imagine, you only want to run a test on GitHub Actions. You can use the whenGitHubActions helper to do so.
it('can only run well on github actions', function () { // your test })->whenGitHubActions();
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/pest-expectations
Usage
Once installed, you can use the custom expectations and helpers provided by this package.
Expectations
toPassWith
This expectation can be used to test if an invokable validation rule works correctly.
In this example, the $value will be given to YourValidationRule. The expectation will pass if your rule passed for the given value.
expect(new YourValidationRule())->toPassWith($value);
You can expect the your validation not to pass for the given value, by using Pest's not().
expect(new YourValidationRule()->not()->toPassWith($value);
toFailWith
This expectation can be used to test if an invokable validation rule did not pass for a given value.
In this example, the $value will be given to YourValidationRule. The expectation will pass if your rule did not pass for the given value.
expect(new YourValidationRule())->toFailWith($value);
Optionally, you can also pass a message as the second argument. The expectation will pass is the validation rule return the given $message.
expect(new YourValidationRule())->toFailWith($value, 'This value is not valid.');
toBeModel
Expect that a value is a model an equal to the passed model.
expect($model)->toBeModel($anotherModel);
The expectation will only pass if both models are Eloquent models of the same class, with the same key.
toBeArrayOf
Expect that a value is an array of the specified value.
expect(User::all())->toBeArrayOf(User::class);
The specified value may be a class name or a class instance, or one of the following string values:
'string''int''float''bool''scalar''array''object''null'
expect([1, 2])->toBeArrayOf('int'); expect([true, false])->toBeArrayOf('bool'); expect(['foo', 1, false])->toBeArrayOf('scalar');
toBeScheduled
Expect that a value is a scheduled job, command or invokable class. The timezone parameter, if passed, will also check that the specified timezone was defined.
expect(MyJob::class)->toBeScheduled('0 * * * *', timezone: 'Europe/Paris');
Optionally, you may pass a callback that accepts an Illuminate\Console\Scheduling\Event or Illuminate\Console\Scheduling\CallbackEvent instance, so you can run any assertion needed:
expect(MyArtisanCommand::class)->toBeScheduled(function (Event $event) { expect($event->getExpression())->toBe('0 0 * * *'); expect($event->getSummaryForDisplay())->toBe('Foo'); });
toHaveJsonApiPagination
Expect that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.
$response = $this->get('/'); expect($response)->toHaveJsonApiPagination();
toBeInRange
Expect that a value is in the specified range. The range is inclusive, meaning that the start and end values are included in the range.
The first argument is the start of the range, and the second argument is the end of the range.
expect(5)->toBeInRange(1, 10); // passes expect(11)->toBeInRange(1, 10); // fails
toBeCloseTo
Expect that a value is close to the specified value. The first argument is the expected value, and the second argument is the deviation.
The deviation is the maximum difference between the expected value and the actual value.
expect(5)->toBeCloseTo(4.89, deviation: 0.1); // fails expect(5)->toBeCloseTo(4.90, deviation: 0.1); // passes expect(5)->toBeCloseTo(5.1, deviation: 0.1); // passes expect(5)->toBeCloseTo(5.11, deviation: 0.1); // fails
Helpers
This package offers various helpers that you can tack on any test. Here's an example of the whenGitHubActions helper. When tacked on to a test, the test will be skipped unless you're running it on GitHub Actions.
it('can only run well on github actions', function () { // your test })->whenGitHubActions();
To use the helpers, you should call registerSpatiePestHelpers() in your Pest.php file.
These helpers are provided by this package:
whenConfig($configKey): only run the test when the given Laravel config key is setwhenEnvVar($envVarName): only run the test when the given environment variable is setwhenWindows: the test will be skipped unless running on WindowswhenMac: the test will be skipped unless running on macOSwhenLinux: the test will be skipped unless running on LinuxwhenGitHubActions(): the test will be skipped unless running on GitHub ActionsskipOnGitHubActions(): the test will be skipped when running on GitHub ActionswhenPhpVersion($version): the test will be skipped unless running on the given PHP version, or higher. You can pass a version like8or8.1.whenIpv6Available: the test will be skipped unless IPv6 is available
Assertions
This package also provides a set of custom assertions that you can use in your tests.
In your TestCase class, you can use the CustomAssertions trait and call the registerCustomAssertions method in the setUp method.
class TestCase extends \Illuminate\Foundation\Testing\TestCase { use CustomAssertions; protected function setUp(): void { parent::setUp(); $this->registerCustomAssertions(); }
assertHasJsonApiPagination
Assert that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.
$this ->get('/') ->assertHasJsonApiPagination();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/pest-expectations 适用场景与选型建议
spatie/pest-expectations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125.71k 次下载、GitHub Stars 达 78, 最近一次更新时间为 2023 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「pest-expectations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/pest-expectations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/pest-expectations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/pest-expectations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 125.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 78
- 点击次数: 21
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-15