sanmai/phpunit-legacy-adapter
Composer 安装命令:
composer require sanmai/phpunit-legacy-adapter
包简介
PHPUnit Legacy Versions Adapter
README 文档
README
As you're here, you are probably well aware that PHPUnit 8+ requires common template methods
like setUp() or tearDown() to have a void return type declaration, which methods naturally break anything below PHP 7.1.
Although it is not a big deal to automatically update your code to use these return type declaration with help from the likes of PHP-CS-Fixer or Rector, it might become a problem if, for whatever unfortunate (but, hopefully, lucrative) reasons, you have to ensure your code is working under PHP 7.0 or PHP 5.6, all the while wanting using the best world can give you in the more-less recent versions of PHPUnit.
In this case, you'll have two problems. One, newer versions of PHPUnit do not have old assertions, but you can find a way around this, and another,
as mentioned, newer versions of PHPUnit require void return type declarations for the convenient template methods, and then you're stuck because
rewriting tests to work without these template methods is a major pain and might be impossible even. And then this small library comes to save your day!
composer require --dev sanmai/phpunit-legacy-adapter:"^6.4 || ^8.2.1"
How to use
First, update your tests to extend from \LegacyPHPUnit\TestCase instead of \PHPUnit\Framework\TestCase:
- class MyTest extends \PHPUnit\Framework\TestCase + class MyTest extends \LegacyPHPUnit\TestCase
Then, where you had to use setUp(): void template method, use doSetUp() method, omitting all any any return types in a fully backward-compatible way.
- protected function setUp(): void + protected function doSetUp()
There are similar replacements for most other template method:
- public static function setUpBeforeClass(): void + public static function doSetUpBeforeClass()
- public static function tearDownAfterClass(): void + public static function doTearDownAfterClass()
- protected function setUp(): void + protected function doSetUp()
- protected function tearDown(): void + protected function doTearDown()
- protected function assertPreConditions(): void + protected function doAssertPreConditions()
- protected function assertPostConditions(): void + protected function doAssertPostConditions()
Reference
| Method | Replacement |
|---|---|
setUpBeforeClass(): void |
doSetUpBeforeClass() |
tearDownAfterClass(): void |
doTearDownAfterClass() |
setUp(): void |
doSetUp() |
tearDown(): void |
doTearDown() |
assertPreConditions(): void |
doAssertPreConditions() |
assertPostConditions(): void |
doAssertPostConditions() |
Supported versions
- 6.x version branch supports PHPUnit 4, PHPUnit 5, and PHPUnit 6.
- 8.x version branch supports PHPUnit 7, PHPUnit 8, and PHPUnit 9.
Future versions will likely follow the same pattern.
What this library does not do
Although this library solves the most annoying part of the problem, there are other parts the library was not designed to cover. For example:
- Some versions of PHPUnit allow
assertContainsto be used with strings, while other do not. - In some versions one method is called
expectExceptionMessageRegExp, while in others the same method is calledexpectExceptionMessageMatches. - And so on and on.
There are polyfills for these changed methods (see below), but it should not be a big deal to write an ad hoc polyfill just for the methods you need. E.g.:
public function __call($method, $args) { if ($method === 'assertStringContainsString') { $this->assertContains(...$args); } if ($method === 'assertIsBool') { $this->assertTrue(\is_bool($args[0])); } if ($method === 'expectExceptionMessageRegExp') { $this->expectExceptionMessageMatches(...$args); } throw new \InvalidArgumentException(); }
If there are several modular (and not) multi-version polyfills for these, and other methods:
- One of the most used is one that comes with Symfony's PHPUnit Bridge. You can check the source here. Note that it does not support inheritance (you'll have to import the trait into every class).
- There's
yoast/phpunit-polyfills. - There's
phpunitgoodpractices/polyfill.
sanmai/phpunit-legacy-adapter 适用场景与选型建议
sanmai/phpunit-legacy-adapter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98.03k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sanmai/phpunit-legacy-adapter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sanmai/phpunit-legacy-adapter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 98.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 40
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-08-22