fkupper/module-dynamic-snapshots
Composer 安装命令:
composer require fkupper/module-dynamic-snapshots
包简介
Dynamic content snapshot testing for Codeception
关键字:
README 文档
README
This is a module that can be used together with Codeception to test Snapshots with dynamic data.
Installation
Using composer:
composer require "fkupper/module-dynamic-snapshots"
On your codeception.yml file, add:
extensions: commands: - Fkupper\Command\GenerateDynamicSnapshot
Usage
Creating Snapshots
New snapshots cna be created using codeception client with the custom command in this package. Eg:
php ./vendor/bin/codecept generate:dynamicsnapshot Acceptance Products
Fetching dynamic snapshot data
Similar to vanilla Codeception snapshots, the DynamicSnapshots classes will fetch data using the method fetchDynamicData. So, in your snapshots you will have to implement this method:
class FooSnapshot extends DynamicSnapshot { /** * @var Tester */ protected $tester; public function fetchDynamicData() { // fetch snapshot from a helper method or snomething and return return $this->tester->fetchDataFromSomewhere(); } }
Substitutions
This is the main feature of this package. When dealing with variable data in snapshots, they can be replaced with placeholders and replaced back in runtime everytime the snapshot tests are executed.
For example, if you want to test a XML API response containing static data and variable data:
<xml> <appVersion value="v8.8.9" /> <someOtherNonDynamicData value="foo" /> <bar value="baz" /> </xml>
The property "appVersion" can change anytime and to avoid updating it every time, use setSubstitutions method to replace it with a string (or a value that can be casted to string) of your choice:
class FooSnapshot extends DynamicSnapshot { /** * @var Tester */ protected $tester; public function fetchDynamicData() { $this->setSubstitutions( // $this->tester->getAppVersion() returns "v8.8.9" 'app_version' => $this->tester->getAppVersion() ); // fetch snapshot from a helper method or snomething and return return $this->tester->fetchDataFromXml(); } }
The first time the dynamic snapshot test is executed, a snapshot data file will be created like:
<xml> <appVersion value="[snapshot_app_version]" /> <someOtherNonDynamicData value="foo" /> <bar value="baz" /> </xml>
Note that placeholders are wrapped in [ ], and from now on, whenever the app version changes, the snapshot will not break or require an update.
Using the dynamic snapshot classes in tests
Please refer to Codepcetion's standard Snapshot documentation.
Custom placeholder wrappers
By default, placeholders are wrapped in brackets [ ], but it is possible to change what character or sequence of charaters it should use.
For example, if brackets are sensible part of your snapshot data, you can change it to something else using setWrappers:
class FooSnapshot extends DynamicSnapshot { protected $tester; public function __constructor(Tester $I) { $this->tester = $I; $this->setWrappers('{', '}'); // $this->setWrappers('(', ')'); // $this->setWrappers('<', '>'); } }
Ignoring parts of the snapshot data
If your snapshot have variable data that cannot be tested or that you just want to igore, it is possible to provide a list of regular expression patterns that will be removed from the data when asserting.
For example, in the snapshot data below, the current timestamp in the favicon href property have to be ignored:
<html> <head> <!-- the favicon will always have the current timestamp suffix --> <link rel="shortcut icon" href="/favicon.ico?v=1588930951"> </head> </html>
So you can ignore this line by calling setIgnoredLinesPatterns from your snapshot object:
$this->setIgnoredLinesPatterns(['/^.*favicon.*$/m']);
Handling space sequences
Sometimes your snapshot data can have variable amounts of space characters in sequence that change out of your control.
To toggle your snapshot behavior to ignore these spaces or not, use shouldAllowSpaceSequences(true|false). Setting it to true will compact these space sequences to a single space character.
Note that the default is true, so these sequences will never be reduced to one space if you do not specify them to.
Handling trailing spaces
If you want to clear every line of your snapshot data of trailing spaces, use shouldAllowTrailingSpaces(false).
Note that the default is true, so trailing spaces in your snapshot data will never be removed.
fkupper/module-dynamic-snapshots 适用场景与选型建议
fkupper/module-dynamic-snapshots 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 186.19k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「snapshot」 「codeception」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fkupper/module-dynamic-snapshots 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fkupper/module-dynamic-snapshots 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fkupper/module-dynamic-snapshots 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Connection 'Db' codeception module to 'Yii2' module database settings
Mailtrap module for Codeception
PDF or Image generation from a Url or HTML page
Captures website screenshot and converts website to PDF using Screenshot machine - online website screenshot generator and website to PDF converter.
Guzzle Snapshots Middleware
A PHP package that takes a snapshot of your application and allows you to retrieve it later to help with debugging.
统计信息
- 总下载量: 186.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-03