webmozarts/strict-phpunit
Composer 安装命令:
composer require webmozarts/strict-phpunit
包简介
Enables type-safe comparisons of objects in PHPUnit
README 文档
README
Caution
This repository is read-only. To submit any changes, check out [php-libraries].
Enables type-safe comparisons of objects in PHPUnit.
Problem
PHPUnit has a very powerful comparison system that helps you to compare objects with expected values:
class ValueObject { public ?string $property; public function __construct(?string $property) { $this->property = $property; } } $actual = new ValueObject('foo!'); self::assertEquals(new ValueObject('foo'), $actual); // => fails with a very helpful error message
This comparison system will give you a meaningful exception that guides you precisely to the problem that caused the assertion to fail. Strings are furthermore diffed so that you see exactly which character of the string causes a mismatch.
PHPUnit compares each scalar property of an object with relaxed types. It is a
little more intelligent than using just == under the hood, but still that
will not always provide the results you want:
var_dump('Hi' == true); // => true self::assertEquals(new ValueObject('Hi'), new ValueObject(true)); // => fails var_dump('' == null); // => true self::assertEquals(new ValueObject(''), new ValueObject(null)); // => succeeds
Solution
This extension enables a comparator for scalar values that fights this problem.
With this extension, whenever PHPUnit finds a scalar value during
assertEquals() (even recursively within objects or arrays), it will compare
the value with ===.
Objects are still not checked for identity, hence you can still construct example objects to compare against.
Error messages stay meaningful.
self::assertEquals(new ValueObject(''), new ValueObject(null)); // => fails with a meaningful error self::assertEquals(new ValueObject('foo!'), new ValueObject('foo')); // => fails with a meaningful error self::assertEquals(new ValueObject('foo!'), new ValueObject('foo!')); // => succeeds
Installation
The extension can be installed with Composer:
composer require --dev webmozarts/strict-phpunit
Add the extension to your phpunit.xml.dist file to enable it:
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"> <!-- ... --> <extensions> <bootstrap class="Webmozarts\StrictPHPUnit\StrictPHPUnitExtension"/> </extensions> <!-- ... --> </phpunit>
Authors
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Note that this repository is a subtree-split of a monorepo and hence read only. PRs will be ported to the (internal) monorepo.
License
All contents of this package are licensed under the MIT license.
webmozarts/strict-phpunit 适用场景与选型建议
webmozarts/strict-phpunit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320.42k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2021 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webmozarts/strict-phpunit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webmozarts/strict-phpunit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 320.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 11
- 依赖项目数: 4
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-07