Skip to content

Commit

Permalink
Fix: Do not use deprecated MT_RAND_PHP constant on PHP 8.3 (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz authored Jan 2, 2024
1 parent 93bcd9e commit 77cec4d
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 5 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
include:
- experimental: true
php-version: "8.3"
- "8.3"

runs-on: "ubuntu-latest"

Expand Down Expand Up @@ -57,4 +55,3 @@ jobs:

- name: "Run tests"
run: "./vendor/bin/phpunit --colors=always"
continue-on-error: "${{ matrix.experimental }}"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed polish license plates (#685)
- Stopped using `static` in callables in `Provider\pt_BR\PhoneNumber` (#785)
- Fixed incorrect female name (#794)
- Stopped using the deprecated `MT_RAND_PHP` constant to seed the random generator on PHP 8.3 (#844)

## [2023-06-12, v1.23.0](https://github.com/FakerPHP/Faker/compare/v1.22.0..v1.23.0)

Expand Down
14 changes: 13 additions & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,22 @@ public function seed($seed = null)
if ($seed === null) {
mt_srand();
} else {
mt_srand((int) $seed, MT_RAND_PHP);
mt_srand((int) $seed, self::mode());
}
}

/**
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.random
*/
private static function mode(): int
{
if (PHP_VERSION_ID < 80300) {
return MT_RAND_PHP;
}

return MT_RAND_MT19937;
}

public function format($format, $arguments = [])
{
return call_user_func_array($this->getFormatter($format), $arguments);
Expand Down
48 changes: 48 additions & 0 deletions test/Faker/Core/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ protected function setUp(): void
$this->extension = $this->faker->ext(DateTimeExtension::class);
}

/**
* @requires PHP < 8.3
*/
public function testDateTime(): void
{
$dateTime = $this->extension->dateTime('2005-10-19T14:12:00');
Expand All @@ -32,6 +35,9 @@ public function testDateTime(): void
self::assertEquals(new \DateTime('1990-09-29T12:12:53'), $dateTime);
}

/**
* @requires PHP < 8.3
*/
public function testDateTimeWithTimezone(): void
{
$dateTime = $this->extension->dateTime('2021-09-05T15:10:00', 'America/Los_Angeles');
Expand All @@ -41,6 +47,9 @@ public function testDateTimeWithTimezone(): void
self::assertEquals(new \DateTimeZone('America/Los_Angeles'), $dateTime->getTimezone());
}

/**
* @requires PHP < 8.3
*/
public function testDateTimeAD(): void
{
$dateTime = $this->extension->dateTimeAD('2012-04-12T19:22:23');
Expand All @@ -49,6 +58,9 @@ public function testDateTimeAD(): void
self::assertEquals(new \DateTime('1166-06-01T17:43:42'), $dateTime);
}

/**
* @requires PHP < 8.3
*/
public function testDateTimeBetween(): void
{
$dateTime = $this->extension->dateTimeBetween('1998-12-18T11:23:40', '2004-09-15T22:10:45');
Expand All @@ -63,6 +75,9 @@ public function testDateTimeBetweenShouldThrowIfFromIsNotAnteriorToUntil(): void
$this->extension->dateTimeBetween('2004-09-15T22:10:45', '1998-12-18T11:23:40');
}

/**
* @requires PHP < 8.3
*/
public function testDateTimeInInterval(): void
{
$dateTime = $this->extension->dateTimeInInterval('1999-07-16T17:30:12', '+2 years');
Expand Down Expand Up @@ -120,6 +135,9 @@ public function testDateTimeThisCentury(): void
self::assertLessThanOrEqual(new \DateTime('now'), $dateTime);
}

/**
* @requires PHP < 8.3
*/
public function testDate(): void
{
$date = $this->extension->date('Y-m-d', '2102-11-12T14:45:29');
Expand All @@ -128,6 +146,9 @@ public function testDate(): void
self::assertEquals('2046-12-26', $date);
}

/**
* @requires PHP < 8.3
*/
public function testTime(): void
{
$time = $this->extension->time('H:i:s', '1978-06-27T09:43:21');
Expand All @@ -136,6 +157,9 @@ public function testTime(): void
self::assertEquals('21:59:44', $time);
}

/**
* @requires PHP < 8.3
*/
public function testUnixTime(): void
{
$unixTime = $this->extension->unixTime('1993-08-29T15:10:00');
Expand All @@ -144,6 +168,9 @@ public function testUnixTime(): void
self::assertEquals(432630664, $unixTime);
}

/**
* @requires PHP < 8.3
*/
public function testUnitTimeWithNumericUntil(): void
{
$unixTime = $this->extension->unixTime(1643830258);
Expand All @@ -152,6 +179,9 @@ public function testUnitTimeWithNumericUntil(): void
self::assertEquals(952499510, $unixTime);
}

/**
* @requires PHP < 8.3
*/
public function testIso8601(): void
{
$iso8601 = $this->extension->iso8601('1993-07-11T15:10:00');
Expand All @@ -170,6 +200,9 @@ public function testAmPm(): void
self::assertContains($amPm, ['am', 'pm']);
}

/**
* @requires PHP < 8.3
*/
public function testDayOfMonth(): void
{
$dayOfMonth = $this->extension->dayOfMonth('2001-04-29T15:10:12');
Expand All @@ -178,6 +211,9 @@ public function testDayOfMonth(): void
self::assertEquals('25', $dayOfMonth);
}

/**
* @requires PHP < 8.3
*/
public function testDayOfWeek(): void
{
$dayOfWeek = $this->extension->dayOfWeek('2021-12-12T15:10:00');
Expand All @@ -186,6 +222,9 @@ public function testDayOfWeek(): void
self::assertEquals('Monday', $dayOfWeek);
}

/**
* @requires PHP < 8.3
*/
public function testMonth(): void
{
$month = $this->extension->month('2021-05-23T15:10:00');
Expand All @@ -194,6 +233,9 @@ public function testMonth(): void
self::assertEquals('10', $month);
}

/**
* @requires PHP < 8.3
*/
public function testMonthName(): void
{
$monthName = $this->extension->monthName('2021-06-06T15:10:00');
Expand All @@ -202,6 +244,9 @@ public function testMonthName(): void
self::assertEquals('October', $monthName);
}

/**
* @requires PHP < 8.3
*/
public function testYear(): void
{
$year = $this->extension->year('2021-09-12T15:10:00');
Expand All @@ -210,6 +255,9 @@ public function testYear(): void
self::assertEquals('1999', $year);
}

/**
* @requires PHP < 8.3
*/
public function testCentury(): void
{
$century = $this->extension->century();
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/Core/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function testUuidReturnsUuid(): void
self::assertTrue($this->isUuid($uuid));
}

/**
* @requires PHP < 8.3
*/
public function testUuidExpectedSeed(): void
{
$instance = new Uuid();
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public function testUniqueReturnsDifferentUniqueGeneratorWhenResetIsTrue(): void
self::assertNotSame($uniqueGenerator, $generator->unique(true));
}

/**
* @requires PHP < 8.3
*/
public function testUniqueReturnsUniqueGeneratorThatGeneratesUniqueValues(): void
{
$words = [
Expand Down
2 changes: 2 additions & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ public function testOptionalAllowsChainingProviderCallRandomlyReturnNull(): void

/**
* @see https://github.com/fzaninotto/Faker/issues/265
*
* @requires PHP < 8.3
*/
public function testOptionalPercentageAndWeight(): void
{
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/Provider/BiasedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function testUnbiased(): void
}
}

/**
* @requires PHP < 8.3
*/
public function testLinearHigh(): void
{
$this->performFake(['\Faker\Provider\Biased', 'linearHigh']);
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/Provider/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function testUuidReturnsUuid(): void
self::assertTrue($this->isUuid($uuid));
}

/**
* @requires PHP < 8.3
*/
public function testUuidExpectedSeed(): void
{
if (pack('L', 0x6162797A) == pack('N', 0x6162797A)) {
Expand Down
6 changes: 6 additions & 0 deletions test/Faker/Provider/en_GB/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function testModulus97AlgorithmWithInvalidArgument(): void
$this->faker->calculateModulus97(123);
}

/**
* @requires PHP < 8.3
*/
public function testVat(): void
{
$this->assertDefaultVatFormat($this->faker->vat());
Expand All @@ -37,6 +40,9 @@ private function assertDefaultVatFormat($number): void
self::assertEquals(1, preg_match('/^GB[\d]{3} [\d]{4} [\d]{2}$/', $number));
}

/**
* @requires PHP < 8.3
*/
public function testVatBranchType(): void
{
$number = $this->faker->vat(Company::VAT_TYPE_BRANCH);
Expand Down
2 changes: 2 additions & 0 deletions test/Faker/Provider/fi_FI/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function provideSeedAndExpectedReturn()
}

/**
* @requires PHP < 8.3
*
* @dataProvider provideSeedAndExpectedReturn
*/
public function testPersonalIdentityNumberUsesBirthDateIfProvided($seed, $birthdate, $expected): void
Expand Down
6 changes: 6 additions & 0 deletions test/Faker/Provider/fr_FR/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ public function testPostcode(): void
self::assertMatchesRegularExpression('@^\d{5}$@', $postcode);
}

/**
* @requires PHP < 8.3
*/
public function testSecondaryAddress(): void
{
self::assertEquals('Étage 007', $this->faker->secondaryAddress());
self::assertEquals('Bât. 932', $this->faker->secondaryAddress());
}

/**
* @requires PHP < 8.3
*/
public function testRegion(): void
{
self::assertEquals('Occitanie', $this->faker->region());
Expand Down
6 changes: 6 additions & 0 deletions test/Faker/Provider/fr_FR/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
*/
final class ColorTest extends TestCase
{
/**
* @requires PHP < 8.3
*/
public function testColorName(): void
{
self::assertEquals('Mandarine', $this->faker->colorName());
self::assertEquals('Acajou', $this->faker->colorName());
}

/**
* @requires PHP < 8.3
*/
public function testSafeColorName(): void
{
self::assertEquals('bleu', $this->faker->safeColorName());
Expand Down
3 changes: 3 additions & 0 deletions test/Faker/Provider/hu_HU/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
final class PersonTest extends TestCase
{
/**
* @requires PHP < 8.3
*/
public function testValidMariedFemaleLastnames(): void
{
self::assertEquals('Báró Vassné Zsóka', $this->faker->name('female'));
Expand Down
6 changes: 6 additions & 0 deletions test/Faker/Provider/ja_JP/InternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
*/
final class InternetTest extends TestCase
{
/**
* @requires PHP < 8.3
*/
public function testUserName(): void
{
self::assertEquals('akira72', $this->faker->userName);
}

/**
* @requires PHP < 8.3
*/
public function testDomainName(): void
{
self::assertEquals('nakajima.com', $this->faker->domainName);
Expand Down
15 changes: 15 additions & 0 deletions test/Faker/Provider/ja_JP/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,41 @@
*/
final class PersonTest extends TestCase
{
/**
* @requires PHP < 8.3
*/
public function testKanaNameMaleReturns(): void
{
self::assertEquals('アオタ ミノル', $this->faker->kanaName('male'));
}

/**
* @requires PHP < 8.3
*/
public function testKanaNameFemaleReturns(): void
{
self::assertEquals('アオタ ミキ', $this->faker->kanaName('female'));
}

/**
* @requires PHP < 8.3
*/
public function testFirstKanaNameMaleReturns(): void
{
self::assertEquals('ヒデキ', $this->faker->firstKanaName('male'));
}

/**
* @requires PHP < 8.3
*/
public function testFirstKanaNameFemaleReturns(): void
{
self::assertEquals('マアヤ', $this->faker->firstKanaName('female'));
}

/**
* @requires PHP < 8.3
*/
public function testLastKanaNameReturnsNakajima(): void
{
self::assertEquals('ナカジマ', $this->faker->lastKanaName);
Expand Down
Loading

0 comments on commit 77cec4d

Please sign in to comment.