Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Do not use deprecated MT_RAND_PHP constant on PHP 8.3 #845

Open
wants to merge 4 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
include:
- experimental: true
php-version: "8.3"
- "8.3"

runs-on: "ubuntu-latest"

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Removed functionality for populating ORM entities and models (#764)
- Added a PHP version support policy (#752)
- Stopped using `static` in callables in `Provider\pt_BR\PhoneNumber` (#785)
- Stopped using the deprecated `MT_RAND_PHP` constant to seed the random generator on PHP 8.3 (#845)

## [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/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,22 @@
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;

Check warning on line 703 in src/Generator.php

View check run for this annotation

Codecov / codecov/patch

src/Generator.php#L703

Added line #L703 was not covered by tests
}

public function format($format, $arguments = [])
{
return call_user_func_array($this->getFormatter($format), $arguments);
Expand Down
48 changes: 48 additions & 0 deletions test/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/Core/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function testUuidReturnsUuid(): void
self::assertTrue($this->isUuid($uuid));
}

/**
* @requires PHP < 8.3
*/
public function testUuidExpectedSeed(): void
{
$instance = new Uuid(new Number());
Expand Down
2 changes: 1 addition & 1 deletion test/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public function word(): string
$uniqueGenerator->word(),
];

self::assertEquals($words, $generatedWords);
self::assertEqualsCanonicalizing($words, $generatedWords);
}

public function testUniqueReturnsUniqueGeneratorThatThrowsWhenItCanNotGenerateUniqueValuesAnymore(): void
Expand Down
2 changes: 2 additions & 0 deletions test/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ public function testOptionalAllowsChainingProviderCallRandomlyReturnNull(): void
}

/**
* @requires PHP < 8.3
*
* @see https://github.com/fzaninotto/Faker/issues/265
*/
public function testOptionalPercentageAndWeight(): void
Expand Down
3 changes: 3 additions & 0 deletions test/Provider/BiasedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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/Provider/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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/Provider/en_GB/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function testModulus97AlgorithmWithInvalidArgument(): void
$this->faker->calculateModulus97(123);
}

/**
* @requires PHP < 8.3
*/
public function testVat(): void
{
$this->assertDefaultVatFormat($this->faker->vat());
Expand All @@ -39,6 +42,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/Provider/fi_FI/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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/Provider/fr_FR/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,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/Provider/fr_FR/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,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/Provider/hu_HU/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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/Provider/ja_JP/InternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,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/Provider/ja_JP/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,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