Skip to content

Commit

Permalink
PHPUnit tests: fix deprecation notices on PHPUnit 10
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl authored and solardiz committed Sep 12, 2024
1 parent 95c8c1d commit b92823e
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions tests/unit/PasswordHashEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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.
*
Expand All @@ -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.
Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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(
Expand Down

0 comments on commit b92823e

Please sign in to comment.