From c83b7e53a6cd93f5e9f921e2aa371d092e73a8be Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 12 Sep 2024 09:49:28 +0200 Subject: [PATCH] PHPUnit tests: fix deprecation notices on PHPUnit 10 PHPUnit 11 will be stricter about the data passed from data providers to test methods. * The amount of parameters passed has to match the expected number of parameters exactly. * If the data sets use keys, the parameter names have to match the keys as used in each data set (inner array). PHPUnit 10 warns about these changes via deprecation notices. As the success and fail tests use the same input and to avoid duplication, I've added some interim data provider methods to prevent the deprecation notices and allow for update to PHPUnit 11, while still avoiding data duplication. --- tests/unit/PasswordHashEndToEndTest.php | 60 ++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/tests/unit/PasswordHashEndToEndTest.php b/tests/unit/PasswordHashEndToEndTest.php index 77acfb4..ecf111d 100644 --- a/tests/unit/PasswordHashEndToEndTest.php +++ b/tests/unit/PasswordHashEndToEndTest.php @@ -16,7 +16,7 @@ final class PasswordHashEndToEndTest extends TestCase { * Test using the stronger but system-specific hashes, with a possible fallback to * the weaker portable hashes. * - * @dataProvider dataSets + * @dataProvider dataSetsSuccess * * @param string $input The text to hash and compare with. * @@ -33,7 +33,7 @@ public function testStrongerSystemSpecificHashSuccess($input) { * Test using the stronger but system-specific hashes, with a possible fallback to * the weaker portable hashes. * - * @dataProvider dataSets + * @dataProvider dataSetsFail * * @param string $input The text to hash. * @param string $compare The text to compare the hash with. @@ -50,7 +50,7 @@ public function testStrongerSystemSpecificHashFail($input, $compare) { /** * Test using the weaker portable hashes. * - * @dataProvider dataSets + * @dataProvider dataSetsSuccess * * @param string $input The text to hash and compare with. * @@ -67,7 +67,7 @@ public function testWeakerPortableHashSuccess($input) { /** * Test using the weaker portable hashes. * - * @dataProvider dataSets + * @dataProvider dataSetsFail * * @param string $input The text to hash. * @param string $compare The text to compare the hash with. @@ -87,6 +87,30 @@ public function testWeakerPortableHashFail($input, $compare) { * * @return array */ + public static function dataSetsSuccess() { + $data = self::dataSets(); + foreach ($data as $key => $value) { + // The `compare` parameter is only needed for the "fail" tests. + unset($data[$key]['compare']); + } + + return $data; + } + + /** + * Data provider. + * + * @return array + */ + public static function dataSetsFail() { + return self::dataSets(); + } + + /** + * Data provider helper. + * + * @return array + */ public static function dataSets() { return array( 'initial test case' => array( @@ -99,7 +123,7 @@ public static function dataSets() { /** * Test the generated hash is correctly calculated using the weaker portable hashes. * - * @dataProvider dataGeneratedHash + * @dataProvider dataGeneratedHashSuccess * * @param string $expected_hash The expected password hash output. * @param string $input The text to hash and compare with. @@ -112,10 +136,25 @@ public function testGeneratedHashSuccess($expected_hash, $input) { $this->assertTrue($t_hasher->CheckPassword($input, $expected_hash)); } + /** + * Data provider. + * + * @return array + */ + public static function dataGeneratedHashSuccess() { + $data = self::dataGeneratedHash(); + foreach ($data as $key => $value) { + // The `compare` parameter is only needed for the "fail" tests. + unset($data[$key]['compare']); + } + + return $data; + } + /** * Test the generated hash is correctly calculated using the weaker portable hashes. * - * @dataProvider dataGeneratedHash + * @dataProvider dataGeneratedHashFail * * @param string $expected_hash The expected password hash output. * @param string $input Unused. @@ -134,6 +173,15 @@ public function testGeneratedHashFail($expected_hash, $input, $compare) { * * @return array */ + public static function dataGeneratedHashFail() { + return self::dataGeneratedHash(); + } + + /** + * Data provider helper. + * + * @return array + */ public static function dataGeneratedHash() { return array( 'initial test case' => array(