Skip to content

Commit

Permalink
Tests: expand TestListener tests [3]
Browse files Browse the repository at this point in the history
Add tests with different types of PHP errors to verify that the `add_error()` method is called in all these cases.
  • Loading branch information
jrfnl committed Sep 7, 2024
1 parent 4862d37 commit b5a378e
Show file tree
Hide file tree
Showing 20 changed files with 520 additions and 4 deletions.
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

<!-- Exclude some rules which are irrelevant.
The code in this package is not run in the context of a WordPress plugin. -->
<exclude name="WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set"/>
<exclude name="WordPress.DB"/>
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_var_export"/>
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.system_calls_popen"/>
Expand Down
32 changes: 32 additions & 0 deletions tests/TestListeners/Fixtures/GenerateEWarning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

/**
* Fixture to test the test listener.
*
* PHP < 8.0: E_WARNING | The magic method __set() must have public visibility and cannot be static in ...
* PHP 8.0+: E_WARNING | The magic method Foo::__set() must have public visibility in ...
*/
class GenerateEWarning {

/**
* Docblock.
*
* @var array<string, mixed>
*/
private $collection = [];

/**
* Docblock.
*
* @phpcs:disable PHPCompatibility.FunctionDeclarations.NonStaticMagicMethods.__setMethodVisibility -- This is intentional.
*
* @param string $name Property name.
* @param mixed $value Property value.
*/

Check failure on line 27 in tests/TestListeners/Fixtures/GenerateEWarning.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Missing @return tag in function comment
private function __set( $name, $value ) {
$this->collection[ $name ] = $value;
}
}
// phpcs:enable
46 changes: 46 additions & 0 deletions tests/TestListeners/Fixtures/PHPDeprecation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPDeprecation extends TestCase {

/**
* Test resulting in a PHP deprecation notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
/*
* Trigger PHP features which were deprecated in various PHP versions.
* phpcs:disable Squiz.PHP.Eval.Discouraged -- See inline comments for the reasons.
*/
switch ( \PHP_MAJOR_VERSION ) {
case 5:
// Assigning the return value of new by reference is deprecated.
// Wrapping this in an `eval()` to prevent it being seen as a file parse error in PHP 7/8.
eval( '$obj = & new \ReflectionObject();' );
break;

case 7:
// password_hash(): Use of the 'salt' option to password_hash is deprecated.
\password_hash( 'foo', \PASSWORD_DEFAULT, [ 'salt' => 'usesomesillystringforsalt' ] );
break;

case 8:
// Optional parameter $optional declared before required parameter $required.
// Wrapping this in an `eval()` as the deprecation is thrown at compiled time, not runtime.
eval( 'function intentionallyWrong($optional = true, $required) {}' );

break;
}
// phpcs:enable
}
}
48 changes: 48 additions & 0 deletions tests/TestListeners/Fixtures/PHPDeprecationPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class PHPDeprecationPHPUnitGte7 extends TestCase {

/**
* Test resulting in a PHP deprecation notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
/*
* Trigger PHP features which were deprecated in various PHP versions.
* phpcs:disable Squiz.PHP.Eval.Discouraged -- See inline comments for the reasons.
*/
switch ( \PHP_MAJOR_VERSION ) {
case 5:
// Assigning the return value of new by reference is deprecated.
// Wrapping this in an `eval()` to prevent it being seen as a file parse error in PHP 7/8.
eval( '$obj = & new \ReflectionObject();' );
break;

case 7:
// password_hash(): Use of the 'salt' option to password_hash is deprecated.
\password_hash( 'foo', \PASSWORD_DEFAULT, [ 'salt' => 'usesomesillystringforsalt' ] );
break;

case 8:
// Optional parameter $optional declared before required parameter $required.
// Wrapping this in an `eval()` as the deprecation is thrown at compiled time, not runtime.
eval( 'function intentionallyWrong($optional = true, $required) {}' );

break;
}
// phpcs:enable
}
}
36 changes: 36 additions & 0 deletions tests/TestListeners/Fixtures/PHPError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use Generator;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPError extends TestCase {

/**
* Test resulting in a PHP error.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
// Trigger PHP features which will throw errors in various PHP versions.
switch ( \PHP_MAJOR_VERSION ) {
case 5:
// The "Generator" class is reserved for internal use.
new Generator();
break;

case 7:
case 8:
// strpos() expects at least 2 parameters, 1 given.
\strpos( 'bar' );
break;
}
}
}
38 changes: 38 additions & 0 deletions tests/TestListeners/Fixtures/PHPErrorPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use Generator;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class PHPErrorPHPUnitGte7 extends TestCase {

/**
* Test resulting in a PHP error.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
// Trigger PHP features which will throw errors in various PHP versions.
switch ( \PHP_MAJOR_VERSION ) {
case 5:
// The "Generator" class is reserved for internal use.
new Generator();
break;

case 7:
case 8:
// strpos() expects at least 2 parameters, 1 given.
\strpos( 'bar' );
break;
}
}
}
24 changes: 24 additions & 0 deletions tests/TestListeners/Fixtures/PHPNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPNotice extends TestCase {

/**
* Test resulting in a PHP notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
// Triggers a notice in all supported PHP versions.
\date_default_timezone_set( 'unknown' );
}
}
26 changes: 26 additions & 0 deletions tests/TestListeners/Fixtures/PHPNoticePHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class PHPNoticePHPUnitGte7 extends TestCase {

/**
* Test resulting in a PHP notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
// Triggers a notice in all supported PHP versions.
\date_default_timezone_set( 'unknown' );
}
}
23 changes: 23 additions & 0 deletions tests/TestListeners/Fixtures/PHPUserDeprecation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPUserDeprecation extends TestCase {

/**
* Test resulting in a PHP deprecation notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
\trigger_error( 'Deprecated', \E_USER_DEPRECATED );
}
}
25 changes: 25 additions & 0 deletions tests/TestListeners/Fixtures/PHPUserDeprecationPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class PHPUserDeprecationPHPUnitGte7 extends TestCase {

/**
* Test resulting in a PHP deprecation notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
\trigger_error( 'Deprecated', \E_USER_DEPRECATED );
}
}
23 changes: 23 additions & 0 deletions tests/TestListeners/Fixtures/PHPUserError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPUserError extends TestCase {

/**
* Test resulting in a PHP error.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
\trigger_error( 'Error', \E_USER_ERROR );
}
}
25 changes: 25 additions & 0 deletions tests/TestListeners/Fixtures/PHPUserErrorPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class PHPUserErrorPHPUnitGte7 extends TestCase {

/**
* Test resulting in a PHP error.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
\trigger_error( 'Error', \E_USER_ERROR );
}
}
23 changes: 23 additions & 0 deletions tests/TestListeners/Fixtures/PHPUserNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*/
class PHPUserNotice extends TestCase {

/**
* Test resulting in a PHP notice.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function runTest() {
\trigger_error( 'Notice', \E_USER_NOTICE );
}
}
Loading

0 comments on commit b5a378e

Please sign in to comment.