philiagus/dataprovider
Composer 安装命令:
composer require philiagus/dataprovider
包简介
DataProvider for PHP Unit testing
关键字:
README 文档
README
A data provider to easily test type assertion cases.
Is it tested?
Tested on the following PHP Version: PHP8.0 - PHP8.3
100% test covered. Test coverage generated on PHP8.3
How do I get it?
The code is available via composer composer require philiagus/dataprovider.
If you are only using this code in the context of testing, adding the package to require-dev using the --dev option is recommended.
See the composer documentation for more information on that.
Why do I need it?
Sometimes a method is defined to take a mixed set of arguments, such as integer|float.
A unit test should then make sure, that no other type of value is accepted. That is to say, that any string for example will cause a defined Exception or result in a defined behaviour.
In PHPUnit this is most times done with a dataProvider.
use Philiagus\DataProvider\DataProvider; use PHPUnit\Framework\TestCase; class MyClassTest extends TestCase { public function provideCases(): array { // create a dataprovider only providing things that are neither string nor integer $provider = new DataProvider(~(DataProvider::TYPE_INTEGER | DataProvider::TYPE_STRING)); return $provider->provide(); } /** * @param mixed $value * @dataProvider provideCases */ public function testMethod($value): void { $this->expectException(\InvalidArgumentException::class); MyClass::method($value); } }
What can it do?
The constructor argument
In most cases we only want to use certain types as arguments for the test. That's why the argument type has been elevated to the highest level, being provided right when creating the DataProvider.
For ease of use some things that PHP treats as the same have been split into individual cases.
These constants are a bitmask, so you can bit-operator them together, such as DataProvider::TYPE_BOOLEAN | DataProvider::TYPE_FLOAT to get boolean and float cases.
The DataProvider::TYPE_ALL contains all cases and is the default of the DataProvider.
| Constant | What it does |
|---|---|
DataProvider::TYPE_BOOLEAN |
Provides the two fundamental bool cases true and false |
DataProvider::TYPE_FLOAT |
Provides positive and negative float values but excludes INF, -INF and NAN |
DataProvider::TYPE_INTEGER |
Provides positive and negative integer values (including 0) |
DataProvider::TYPE_OBJECT |
Provides various objects, such as \stdClass, \Exception and \DateTime |
DataProvider::TYPE_RESOURCE |
Provides a resource, if it is able to. As what provides a resource might change with future versions of PHP this currently uses STDIN as a resource and excludes it from the test list if STDIN is not a resource in your current environment. |
DataProvider::TYPE_STRING |
Provides various strings, including empty string |
DataProvider::TYPE_NULL |
Provides null as case |
DataProvider::TYPE_NAN |
Provides NAN as a case |
DataProvider::TYPE_INFINITE |
Provides both INF and -INF |
DataProvider::TYPE_ARRAY_EMPTY |
Provides an empty array |
DataProvider::TYPE_ARRAY_LIST |
Provides multiple arrays with a single and multiple elements, with a sequential index, starting at 0 |
DataProvider::TYPE_ARRAY_MAP_INTEGER |
Provides multiple arrays with single and multiple elements, with not sequential indizies or index not starting at 0 |
DataProvider::TYPE_ARRAY_MAP_STRING |
Provides multiple arrays with single and multiple elements, each having string keys |
DataProvider::TYPE_ARRAY_MAP_INTEGER_STRING |
Provides arrays with mixed keys (so string and integer keys) |
There are also some compound constants
| Constant | What it includes |
|---|---|
DataProvider::TYPE_ARRAY_MAP |
|
DataProvider::TYPE_ARRAY |
|
DataProvider::TYPE_SCALAR |
|
The methods filter, map and addCase
The method filter(\Closure $filter) allows you to define a filter. The closure will receive a single argument. If the closure returns true, the case will be left in the list of cases. If it returns false, the case will be filtered out.
The method map(\Closure $map) allows to alter and map the values provided. The closure receives the individual value as single argument. Return the altered value. The name of the case will not be altered.
The method addCase(string $name, $value) allows you to add your own case.
These methods are applied in the same order that you apply them to the DataProvider. So first calling filter, then map and then addCase will only map on the filtered values and the added case will not be filtered nor mapped.
The static methods isEqual and isSame
The isEqual($a, $b) and isSame($a, $b) return true if the two values are equal/same following the rules of the DataProvider.
Normally, NAN != NAN, as these two things are not the same - obviously. But in the context of this DataProvider it is not uncommon to desire equality between two NAN.
An example:
use Philiagus\DataProvider\DataProvider; use PHPUnit\Framework\TestCase; class MyClass { public static function wrap($value): array { return [$value]; } } class MyClassTest extends TestCase { public function provideCases(): array { // create a dataprovider only providing things that are neither string nor integer $provider = new DataProvider(~(DataProvider::TYPE_INTEGER | DataProvider::TYPE_STRING)); return $provider->provide(); } /** * @param mixed $value * @dataProvider provideCases */ public function testMethod($value): void { $expected = [$value]; $result = MyClass::wrap($value); self::assertSame($expected, $result); // this would only work for not NAN self::assertTrue(DataProvider::isSame($expected, $result)); // this works, as for the DataProvider NAN === NAN } }
WARNING: Neither isEquals nor isSame currently provide recursion safety and will thus break with max recursion level reached. This might be added in the future, as soon as DataProvider requires PHP >= 7.4.
Specific rules of isEquals
For the purpose of this comparison the following values are equal if:
- NAN
- NAN == NAN
- arrays
- have the same keys no matter the order
- the values of every key are equal following these rules
- objects
- of the same class
- have the same property names
- properties with the same name contain equal value following these rules
- integer, float, string, null, resources
- follow normal == rules
Specific rules of isSame
For the purpose of this comparison the following values are equal if:
- NAN
- NAN == NAN
- arrays
- have the same keys in the same order
- the values of every key are the same following these rules
- objects, integer, float, string, null, resources
- follow normal === rules
philiagus/dataprovider 适用场景与选型建议
philiagus/dataprovider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.83k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「dataprovider」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiagus/dataprovider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiagus/dataprovider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiagus/dataprovider 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CalendarView widget for Yii 2 Framework.
Extension for Yii2 to have caching for ActiveDataProviders.
ListView widget improved to use Masonry (http://masonry.desandro.com/)
CsvDataProvider implements a data provider based on a csv
CalendarView widget for Yii 2 Framework.
A Yii2 data provider that transpose data into a table format
统计信息
- 总下载量: 8.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-16