You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,28 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses
9
9
10
10
_Nothing yet._
11
11
12
+
## [1.1.0] - 2022-11-17
13
+
14
+
### Added
15
+
* Helper functions for on-the-fly creation of opaque test doubles for unavailable classes for use with Mockery. PR [#40]
16
+
The default Mockery mocks for unavailable classes do not allow for the dynamic property deprecation in PHP 8.2, which can cause tests to error out.
17
+
These helper functions can be used to create test doubles which do allow for setting properties.
18
+
The helper functions can be called from within a test bootstrap or from within a test class.
19
+
For full details about these new functions, why they exist and how to use them, please see [the documentation in the README](https://github.com/Yoast/wp-test-utils#helpers-to-create-test-doubles-for-unavailable-classes).
20
+
21
+
### Changed
22
+
* The [BrainMonkey] dependency has been updated to require [version `^2.6.1`](https://github.com/Brain-WP/BrainMonkey/releases/tag/2.6.1) (was `^2.6.0`). PR [#28]
23
+
* The [PHPUnit Polyfills] dependency has been updated to require [version `^1.0.4`](https://github.com/Yoast/PHPUnit-Polyfills/releases/tag/1.0.4) (was `^1.0.1`). PRs [#28], [#41]
24
+
* Composer: The package will now identify itself as a testing tool. PR [#36]
This library contains a set of utilities for running automated tests for WordPress plugins and themes.
11
11
@@ -16,6 +16,7 @@ This library contains a set of utilities for running automated tests for WordPre
16
16
- [Basic `TestCase` for use with BrainMonkey](#basic-testcase-for-use-with-brainmonkey)
17
17
- [Yoast TestCase for use with BrainMonkey](#yoast-testcase-for-use-with-brainmonkey)
18
18
- [Bootstrap file for use with BrainMonkey](#bootstrap-file-for-use-with-brainmonkey)
19
+
- [Helpers to create test doubles for unavailable classes](#helpers-to-create-test-doubles-for-unavailable-classes)
19
20
-[Utilities for running integration tests with WordPress](#utilities-for-running-integration-tests-with-wordpress)
20
21
-[What these utilities solve](#what-these-utilities-solve)
21
22
-[Basic `TestCase` for WordPress integration tests](#basic-testcase-for-wordpress-integration-tests)
@@ -32,9 +33,9 @@ Requirements
32
33
* PHP 5.6 or higher.
33
34
34
35
The following packages will be automatically required via Composer:
35
-
*[PHPUnit Polyfills] 1.0.1 or higher.
36
+
*[PHPUnit Polyfills] 1.0.4 or higher.
36
37
*[PHPUnit] 5.7 - 9.x.
37
-
*[BrainMonkey] 2.6.0 or higher.
38
+
*[BrainMonkey] 2.6.1 or higher.
38
39
39
40
40
41
Installation
@@ -67,7 +68,8 @@ Features of this `TestCase`:
67
68
The BrainMonkey native functions create stubs which will apply basic HTML escaping if the stubbed function is an escaping function, like `esc_html__()`.<br/>
68
69
The alternative implementations of these functions will create stubs which will return the original value without change. This makes creating tests easier as the `$expected` value does not need to account for the HTML escaping.<br/>
69
70
_Note: the alternative implementation should be used selectively._
70
-
5. Helper functions for setting expectations for generated output.
71
+
5. Helper functions for [setting expectations for generated output](#yoastwptestutilshelpersescapeoutputhelper-trait).
72
+
6. Helper functions for [creating "dummy" test doubles for unavailable classes](#helpers-to-create-test-doubles-for-unavailable-classes).
To tell PHPUnit to use this bootstrap file, use `--bootstrap tests/bootstrap.php` on the command-line or add the `bootstrap="tests/bootstrap.php"` attribute to your `phpunit.xml.dist` file.
169
171
170
172
173
+
#### Helpers to create test doubles for unavailable classes
174
+
175
+
##### Why you may need to create test doubles for unavailable classes
176
+
177
+
Typically a mock for an unavailable class is created using `Mockery::mock()` or `Mockery::mock( 'Unavailable' )`.
178
+
179
+
When either the test or the code under test sets a property on such a mock, this will lead to a ["Creation of dynamic properties is deprecated" notice](https://wiki.php.net/rfc/deprecate_dynamic_properties) on PHP >= 8.2, which can cause tests to error out.
180
+
181
+
If you know for sure the property being set on the mock is a declared property or a property supported via [magic methods](https://www.php.net/oop5.overloading#language.oop5.overloading.members) on the class which is being mocked, the _code under test_ will under normal circumstances never lead to this deprecation notice, but your tests now do.
182
+
183
+
Primarly this is an issue with Mockery. A [question on how to handle this/to add support for this in Mockery itself](https://github.com/mockery/mockery/issues/1197) is open, but in the mean time a work-around is needed (if you're interested, have a read through the Mockery issue for details about alternative solutions and why those don't work as intended).
184
+
185
+
##### How to use the functionality
186
+
187
+
WP Test Utils offers three new utilities to solve this (as of version 1.1.0).
188
+
*`Yoast\WPTestUtils\BrainMonkey\makeDoublesForUnavailableClasses( array $class_names )` for use from within a test bootstrap file.
189
+
*`Yoast\WPTestUtils\BrainMonkey\TestCase::makeDoublesForUnavailableClasses( array $class_names )` and `Yoast\WPTestUtils\BrainMonkey\TestCase::makeDoubleForUnavailableClass( string $class_name )` for use from within a test class.
190
+
191
+
These methods can be used to create one or more "fake" test double classes on the fly, which allow for setting (dynamic) properties.
192
+
These "fake" test double classes are effectively aliases for `stdClass`.
193
+
194
+
These methods are solely intended for classes which are unavailable during the test run and have no effect (at all!) on classes which _are_ available during the test run.
195
+
196
+
For setting expectations on the "fake" test double, use `Mockery::mock( 'FakedClass' )`.
197
+
198
+
**_Implementation example using the bootstrap function:_**
199
+
200
+
You can create the "fake" test doubles for a complete test suite in one go in your test bootstrap file like so:
0 commit comments