PHPackages                             lucatume/wp-snaphot-assertions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. lucatume/wp-snaphot-assertions

ActiveLibrary[Testing &amp; Quality](/categories/testing)

lucatume/wp-snaphot-assertions
==============================

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

2.0.0(3y ago)01.1M↑31.5%1[1 issues](https://github.com/lucatume/wp-snapshot-assertions/issues)1GPL-2.0PHPPHP ^7.3|^7.4|^8.0CI failing

Since Mar 10Pushed 3y agoCompare

[ Source](https://github.com/lucatume/wp-snapshot-assertions)[ Packagist](https://packagist.org/packages/lucatume/wp-snaphot-assertions)[ Docs](https://github.com/lucatume/wp-snaphot-assertions)[ RSS](/packages/lucatume-wp-snaphot-assertions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (1)

WordPress Snapshot Assertions
=============================

[](#wordpress-snapshot-assertions)

*Snapshot testing of WordPress code for PHPUnit, based on the [PHPUnit Snapshot Assertions package by Spatie](https://github.com/spatie/phpunit-snapshot-assertions).*

[![Build Status](https://camo.githubusercontent.com/df64ac8b78e7a7f606fd54cad3c47554a50ef47475b43c8a66789de096abbbce/68747470733a2f2f7472617669732d63692e6f72672f6c75636174756d652f77702d736e617073686f742d617373657274696f6e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/lucatume/wp-snapshot-assertions)

Installation
------------

[](#installation)

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

Usage
-----

[](#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):

```

  Foo

```

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);
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 78.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~839 days

Total

3

Last Release

1313d ago

Major Versions

1.1.0 → 2.0.02022-10-14

PHP version history (2 changes)1.0.0PHP &gt;=7.0

2.0.0PHP ^7.3|^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/60442557?v=4)[Abul Tarannum](/maintainers/theAverageDev)[@theaveragedev](https://github.com/theaveragedev)

---

Top Contributors

[![lucatume](https://avatars.githubusercontent.com/u/2749650?v=4)](https://github.com/lucatume "lucatume (22 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (4 commits)")[![estevao90](https://avatars.githubusercontent.com/u/18039589?v=4)](https://github.com/estevao90 "estevao90 (1 commits)")[![Luc45](https://avatars.githubusercontent.com/u/9341686?v=4)](https://github.com/Luc45 "Luc45 (1 commits)")

---

Tags

testingsnapshotwordpress

### Embed Badge

![Health badge](/badges/lucatume-wp-snaphot-assertions/health.svg)

```
[![Health](https://phpackages.com/badges/lucatume-wp-snaphot-assertions/health.svg)](https://phpackages.com/packages/lucatume-wp-snaphot-assertions)
```

###  Alternatives

[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69617.9M510](/packages/spatie-phpunit-snapshot-assertions)[yoast/wp-test-utils

PHPUnit cross-version compatibility layer for testing plugins and themes build for WordPress

632.3M52](/packages/yoast-wp-test-utils)[suffle/snapshot

Snapshot Testing of standalone fusion components

135.1k](/packages/suffle-snapshot)[fahadyousafmahar/slash-login

Say goodbye to repetitively fill / submit login forms during development of your laravel applications. This package allows you to login as any user with just a "slash login" (/login/user\_id)

101.3k](/packages/fahadyousafmahar-slash-login)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
