Download the PHP package lucatume/wp-snaphot-assertions without Composer
On this page you can find all versions of the php package lucatume/wp-snaphot-assertions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lucatume/wp-snaphot-assertions
More information about lucatume/wp-snaphot-assertions
Files in lucatume/wp-snaphot-assertions
Package wp-snaphot-assertions
Short Description A set of utilities to support snapshot testing of WordPress code.
License GPL-2.0
Homepage https://github.com/lucatume/wp-snaphot-assertions
Informations about the package wp-snaphot-assertions
WordPress Snapshot Assertions
Snapshot testing of WordPress code for PHPUnit, based on the PHPUnit Snapshot Assertions package by Spatie.
Installation
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:
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):
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
:
When HTML attributes contain truly unique or time-dependent values, those attributes can be excluded completely using the WPHtmlOutputDriver::setTimeDependentAttributes
method.
All versions of wp-snaphot-assertions with dependencies
electrolinux/phpquery Version ^0.9.6
php Version ^7.3|^7.4|^8.0
spatie/phpunit-snapshot-assertions Version ^4.2