Skip to content

Commit

Permalink
Tests: Fix implicitly nullable parameters in Tests_HtmlApi_WpHtmlProc…
Browse files Browse the repository at this point in the history
…essorComments.

PHP 8.4 deprecates implicitly nullable parameters, i.e. typed parameters with a `null` default value, which are not explicitly declared as nullable.

The `Tests_HtmlApi_WpHtmlProcessorComments` test class contains one problematic parameter in the `test_comment_processing()` method declaration.

While this could be fixed by adding the nullability operator, the type declarations in the test method is removed instead, including other type declarations for this method and the second test method, which were not affected by the deprecation.

The reason for this is quite straight-forward: using type declarations in tests is bad practice and inhibits defense-in-depth type testing.

Using type declarations in tests prevents being able to test the "code under test" with unexpected input types as the values with unexpected (scalar) types will be juggled to the expected type between the data provider and the test method and the _real_ data value would therefore never reach the method under test.

The knock-on effects of this are:
* That the input handling of the "code under test" can not be safeguarded, whether this input handling is done via in-function type checking or via a type declaration in the "code under test".
* That if such "unexpected data type" tests are added to the data provider, they will silently pass (due to the type being juggled before reaching the "code under test"), giving a false sense of security, while in actual fact, these data sets would not be testing anything at all and if, for instance, the type declaration in the "code under test" would be removed, these tests would still pass, while by rights they should start failing.

Also note that this problem would only be exacerbated if the file would be put under `strict_types`.

Ref: https://wiki.php.net/rfc/deprecate-implicitly-nullable-types

Follow-up to [58734].

Props jrf.
See #62061.

git-svn-id: https://develop.svn.wordpress.org/trunk@59052 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 18, 2024
1 parent 91f8e77 commit 8fc4a00
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/phpunit/tests/html-api/wpHtmlProcessorComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Tests_HtmlApi_WpHtmlProcessorComments extends WP_UnitTestCase {
*
* @dataProvider data_comments
*/
public function test_comment_processing( string $html, string $expected_comment_type, string $expected_modifiable_text, string $expected_tag = null ) {
public function test_comment_processing( $html, $expected_comment_type, $expected_modifiable_text, $expected_tag = null ) {
$processor = WP_HTML_Processor::create_fragment( $html );
$processor->next_token();

Expand Down Expand Up @@ -53,7 +53,7 @@ public static function data_comments() {
*
* @dataProvider data_funky_comments
*/
public function test_funky_comment( string $html, string $expected_modifiable_text ) {
public function test_funky_comment( $html, $expected_modifiable_text ) {
$processor = WP_HTML_Processor::create_fragment( $html );
$processor->next_token();

Expand Down

0 comments on commit 8fc4a00

Please sign in to comment.