定制 lucatume/wp-snaphot-assertions 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lucatume/wp-snaphot-assertions

Composer 安装命令:

composer require --dev lucatume/wp-snaphot-assertions

包简介

A set of utilities to support snapshot testing of WordPress code.

README 文档

README

Snapshot testing of WordPress code for PHPUnit, based on the PHPUnit Snapshot Assertions package by Spatie.

Build Status

Installation

composer require lucatume/wp-snaphot-assertions --dev

Usage

Snapshot testing comes very handy when testing the HTML output of some WordPress generated and managed code.
In such instances WordPress will often generate time-dependent values, like nonces, and full URLs, like image sources.
Those environment and time related differences might break a snapshot for the wrong reasons; e.g. the snapshot was generated on one machine (say locally) and ran on another machine where WordPress might be served at another URL and the test will surely run at a different time (say CI).
For that purpose the WPHtmlOutputDriver driver was born:

use Spatie\Snapshots\MatchesSnapshots;
use tad\WP\Snapshots\WPHtmlOutputDriver;

class MySnapshotTest extends \Codeception\TestCase\WPTestCase {
	use MatchesSnapshots;

	/**
	* Test snapshot for render
	*/
	public function test_snapshot_render() {
		// from some environment variable
		$currentWpUrl = getenv('WP_URL');
		$snapshotUrl = 'http://wp.localhost';
		
		$driver = new WPHtmlOutputDriver($currentWpUrl, $snapshotUrl);
		
		$sut = new MyPluginHTMLRenderingClass();
		
		// create a random post and return its post ID
		$postId= $this->factory->post->create();
		
		$renderedHtml = $sut->renderHtmlFor($postId);
		$driver->setTolerableDifferences([$postId]);
		$driver->setTolerableDifferencesPrefixes(['post_', 'post-']);
		$driver->setTolerableDifferencesPostfixes(['-single', '-another-postfix']);
		
		$this->assertMatchesSnapshot($renderedHtml, $driver);
	}
}

By default the driver will lok for time-dependent fields with an id, name or class from a default list (e.g. _wpnonce); you might want to add or modify that list using the WPHtmlOutputDriver::setTimeDependentKeys method.

On the same note, the driver will look for some attributes when looking to replace the snapshot URL with the current URL; you can modify those using the WPHtmlOutputDriver::setUrlAttributes method.

Very often WordPress HTML will contain attributes and strings that will inline post IDs, titles and other fields; in general the comparison of the snapshots should not fail because the random post ID used when the snapshot was generated was 23 but it's, in another test run, 89.

To avoid that use the WPHtmlOutputDriver::setTolerableDifferences method to define what values defined in the current test run should not trigger a failure (see example above); furthermore run-dependent variables could be used to construct id, class, data and other attributes: if you know that the rendered HTML will contain something like this (where 23 is the post ID):

<div class="post-23" data-one="23-postfix" data-two="prefix-23">
  <p>Foo</p>
</div>

You might want to say to the driver that the current post ID is a tolerable difference even when prefixed with prefix- or postfixed with -postfix:

$driver->setTolerableDifferences([$currentPostId]);
$driver->setTolerableDifferencesPrefixes(['prefix-']);
$driver->setTolerableDifferencesPostfixes(['-postfix']);
$this->assertMatchesSnapshot($renderedHtml, $driver);

When HTML attributes contain truly unique or time-dependent values, those attributes can be excluded completely using the WPHtmlOutputDriver::setTimeDependentAttributes method.

// Void all the `data-one` attributes in the HTML document.
$driver->setTimeDependentAttributes(['data-one']);
// Void all the `.container data-two`  and `.container data-three` attributes in the HTML document.
$driver->setTimeDependentAttributes(['data-two', 'data-three'], '.container');
$this->assertMatchesSnapshot($renderedHtml, $driver);

lucatume/wp-snaphot-assertions 适用场景与选型建议

lucatume/wp-snaphot-assertions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.18M 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「wordpress」 「snapshot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lucatume/wp-snaphot-assertions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lucatume/wp-snaphot-assertions 我们能提供哪些服务?
定制开发 / 二次开发

基于 lucatume/wp-snaphot-assertions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.18M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2018-03-10