keboola/datadir-tests
Composer 安装命令:
composer require keboola/datadir-tests
包简介
Tool for functional testing of Keboola Connection components
README 文档
README
Usage
Require this package in you component
composer require keboola/datadir-tests`
In the tests folder create a directory structure mimicking the directory structure in production:
/path/to/tests
└─test-name
├─expected-code
├─expected-stdout (optional)
├─expected-stderr (optional)
├─expected
│ └─data
│ └─out
│ ├─files
│ └─tables
├─source
│ └─data
│ └─in
│ ├─files
│ └─tables
└─config.json
Note: expected-stdout and expected-stderr
are compared with real output using assertStringMatchesFormat method,
so you can use placeholders.
Then create empty /path/to/tests/DatadirTest that extends Keboola\DatadirTests\DatadirTestCase:
<?php declare(strict_types=1); namespace MyComponent\Tests; use Keboola\DatadirTests\DatadirTestCase; class DatadirTest extends DatadirTestCase { }
Run it using
vendor/bin/phpunit /path/to/tests/DatadirTest.php`
The script then executes /code/src/run.php with KBC_DATADIR set to the test directory. There can be any number of test directories and the script automatically discovers them using DatadirTestsFromDirectoryProvider. You can supply your own provider that implements DatadirTestsProviderInterface. It needs to return array of arrays (!) of DatadirTestSpecificationInterface instances.
When the expected-code file is present, the return code of execution is checked. The file contains a single number number - the execution code, allowed values are 0, 1, 2.
What is DatadirTestSpecificationInterface?
DatadirTestSpecificationInterface contains all the information you need to create and assert a datadir test:
getSourceDatadirDirectory(): ?string: returns the directory that initializes the test. You should prepareconfig.jsonand potentially also/in/filesor/in/tablescontents. That directory is mirrored to the temporary directory that the test is ran in.nullmeans just barebones directory structure is created and you need to createconfig.json, etc. in the temporary directory yourself.getExpectedReturnCode(): ?int: expected exit code,nullmeans "non-zero"getExpectedStdout(): ?string: if supplied, whole stdout output is checked against supplied valuegetExpectedStderr(): ?string: if supplied, whole stderr output is checked against supplied valuegetExpectedOutDirectory(): ?string: if supplied, the temporary directory'soutdirectory is compared with this directory after the component is ran. Any differences result in test failure.
Custom test
Just add a test method and reuse the existing helper methods in AbstractDatadirTestCase.
public function testInvalidFile(): void { // create specification manually $specification = new DatadirTestSpecification( __DIR__ . '/columns-auto/source/data', 0, null, null, __DIR__ . '/columns-auto/expected/data/out' ); // create temporary directory $tempDatadir = $this->getTempDatadir($specification); // modify temporary directory however you see fit file_put_contents($tempDatadir->getTmpFolder() . '/config.json', '{"parameters": []}'); // run the script $process = $this->runScript($tempDatadir->getTmpFolder()); // assert specification $this->assertMatchesSpecification($specification, $process, $tempDatadir->getTmpFolder()); }
Environment variables
- In
config.jsonyou can use placeholders for environment variables. - Format is
%env(TYPE:VAR_NAME)%, eg.%env(int:DB_PORT)%. - All string values in
config.jsonthat match specified format are replaced. - It is implemented in
AbstractDatadirTestCase::modifyConfigJsonContent. - Allowed types are
string,int,float,bool. - See example in the test.
Functionality adjustments
- To modify temp data dir before execution, you can extend the method
AbstractDatadirTestCase::setUpDatadir.
Development
Clone this repository and init the workspace with following command:
git clone https://github.com/keboola/datadir-tests
cd datadir-tests
docker-compose run --rm dev /bin/bash
$ composer install
$ composer ci
License
MIT licensed, see LICENSE file.
keboola/datadir-tests 适用场景与选型建议
keboola/datadir-tests 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68.78k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 keboola/datadir-tests 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 keboola/datadir-tests 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 68.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 20
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-16