yuraplohov/test-data-provider
Composer 安装命令:
composer require yuraplohov/test-data-provider
包简介
Providing test data for autotests from php files with returned arrays, and from any text files
README 文档
README
The package provides data for autotests from php-files with returned arrays, and from any text files (json, xml, txt...). The package works with PHPUnit, Codeception and Pest frameworks.
Installation
composer require yuraplohov/test-data-provider
Usage
The package contains one class - Yuraplohov\TestDataProvider\Provider. This class has four public methods:
- basePath(string $basePath): self - sets the base path to the directory with data.
- getPHPUnitCases(array $caseDirs): array - retrieves an array of test cases in PHPUnit framework format.
- getCodeceptionCases(array $caseDirs): array - retrieves an array of test cases in Codeception framework format.
- get(string $path): mixed - retrieves data from a directory, file, or array element in a php-file.
Php files with data for all these methods should be like this:
<?php return [ // some large array ];
Also, all three methods can work with any text files (json, xml, txt...). The content of such files is returned as a string without any transformations.
The 'tests' directory structure for getPHPUnitCases() and getCodeceptionCases() methods could be:
- tests
- Example_Test
- ExampleTets.php
- data
- case_1
- input.json
- expected.php
- case_2
- input.json
- expected.php
- case_1
- Example_Test
If the base path is not specified, the 'data' directory must be at the same level as your test class. Directories with cases are placed in the 'data' directory. You can name case directories whatever you want, but the 'data' directory must always be named like this if you don't specify any base path with basePath().
Provider::getPHPUnitCases(array $caseDirs): array
/** * @test * @dataProvider someProvider */ public function it_gets_some_result(array $case) { $sut = new SomeClass; $this->assertEquals($case['expected'], $sut->someMethod($case['input'])); } /** * @return array */ public function someProvider(): array { return (new Provider)->getPHPUnitCases([ 'case_1', 'case_2', ]); }
Provider::getCodeceptionCases(array $caseDirs): array
/** * @param UnitTester $I * @param \Codeception\Example $example * @dataProvider someProvider */ public function it_gets_some_result(UnitTester $I, \Codeception\Example $example) { $sut = new SomeClass; $I->assertEquals($example['expected'], $sut->someMethod($example['input'])); } /** * @return array */ protected function someProvider() { return (new Provider)->getCodeceptionCases([ 'case_1', 'case_2', ]); }
Provider::get(string $path): mixed
This method can be used with any framework and with any structure of the 'data' directory.
Tests directory structure example:
- tests
- Example_Test
- ExampleTets.php
- data
- service
- input.json
- settings.php
- expected.php
- service
- Example_Test
Examples of method calling for this structure:
(new Provider)->get('service'); // retrieves array with all content of the 'data/service' directory. (new Provider)->get('service/input'); // retrieves content of the file 'data/service/input.json'. (new Provider)->get('expected'); // retrieves array from the 'data/expected.php' file. (new Provider)->get('service/settings.foo.bar'); // retrieves value of the 'bar' array element. // foo.bar - array elements hierarchy in the 'settings.php' file
Usage in PHPUnit:
/** @test */ public function it_gets_some_result() { $dp = new Provider; $sut = new SomeClass($dp->get('service/settings')); $this->assertEquals($dp->get('expected'), $sut->someMethod($dp->get('service/input'))); }
Testing
./vendor/bin/phpunit
License
The MIT License (MIT).
yuraplohov/test-data-provider 适用场景与选型建议
yuraplohov/test-data-provider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.4k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「codeception」 「unit」 「tests」 「laravel」 「test data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yuraplohov/test-data-provider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yuraplohov/test-data-provider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yuraplohov/test-data-provider 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Connection 'Db' codeception module to 'Yii2' module database settings
Mailtrap module for Codeception
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
Symfony bundle for unigen test generator
统计信息
- 总下载量: 2.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-19