-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: expand TestListener tests [3]
Add tests with different types of PHP errors to verify that the `add_error()` method is called in all these cases.
- Loading branch information
Showing
20 changed files
with
520 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Basic CS and QA checks
|
||
private function __set( $name, $value ) { | ||
$this->collection[ $name ] = $value; | ||
} | ||
} | ||
// phpcs:enable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
tests/TestListeners/Fixtures/PHPDeprecationPHPUnitGte7.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
tests/TestListeners/Fixtures/PHPUserDeprecationPHPUnitGte7.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
Oops, something went wrong.