Merge branch '504-fix-random-tests' into 'master'

Add seeds to data fixtures, to avoid random failures in tests

Closes #504

See merge request Chill-Projet/chill-bundles!970
This commit is contained in:
2026-03-09 13:00:30 +00:00
16 changed files with 78 additions and 41 deletions

View File

@@ -0,0 +1,7 @@
kind: DX
body: Add seeds in DataFixtures and in some tests to avoid random test failures
time: 2026-03-09T13:17:10.915852317+01:00
custom:
Issue: "504"
MR: "970"
SchemaChange: No schema change

View File

@@ -8,5 +8,6 @@ when@dev: &dev
- 'file'
- 'md5'
- 'sha1'
seed: 1234567890
when@test: *dev

View File

@@ -33,6 +33,7 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface
public function __construct(private readonly EntityManagerInterface $em)
{
mt_srand(123456789);
$this->faker = FakerFactory::create('fr_FR');
}
@@ -48,7 +49,7 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface
->findAll();
foreach ($persons as $person) {
$activityNbr = random_int(0, 3);
$activityNbr = mt_rand(0, 3);
for ($i = 0; $i < $activityNbr; ++$i) {
$activity = $this->newRandomActivity($person);
@@ -73,7 +74,7 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface
// ->setAttendee($this->faker->boolean())
for ($i = 0; random_int(0, 4) > $i; ++$i) {
for ($i = 0; mt_rand(0, 4) > $i; ++$i) {
$reason = $this->getRandomActivityReason();
if (null !== $reason) {

View File

@@ -21,7 +21,10 @@ use Doctrine\Persistence\ObjectManager;
class LoadAsideActivity extends Fixture implements DependentFixtureInterface
{
public function __construct(private readonly UserRepository $userRepository) {}
public function __construct(private readonly UserRepository $userRepository)
{
mt_srand(123456789);
}
public function getDependencies(): array
{
@@ -47,7 +50,7 @@ class LoadAsideActivity extends Fixture implements DependentFixtureInterface
$this->getReference('aside_activity_category_0', AsideActivityCategory::class)
)
->setDate((new \DateTimeImmutable('today'))
->sub(new \DateInterval('P'.\random_int(1, 100).'D')));
->sub(new \DateInterval('P'.\mt_rand(1, 100).'D')));
$manager->persist($activity);
}

View File

@@ -41,6 +41,7 @@ class LoadOption extends AbstractFixture implements OrderedFixtureInterface
public function __construct()
{
mt_srand(123456789);
$this->fakerFr = \Faker\Factory::create('fr_FR');
$this->fakerEn = \Faker\Factory::create('en_EN');
$this->fakerNl = \Faker\Factory::create('nl_NL');
@@ -104,7 +105,7 @@ class LoadOption extends AbstractFixture implements OrderedFixtureInterface
$manager->persist($parent);
// Load children
$expected_nb_children = random_int(10, 50);
$expected_nb_children = mt_rand(10, 50);
for ($i = 0; $i < $expected_nb_children; ++$i) {
$companyName = $this->fakerFr->company;
@@ -144,7 +145,7 @@ class LoadOption extends AbstractFixture implements OrderedFixtureInterface
$manager->persist($parent);
// Load children
$expected_nb_children = random_int(10, 50);
$expected_nb_children = mt_rand(10, 50);
for ($i = 0; $i < $expected_nb_children; ++$i) {
$manager->persist($this->createChildOption($parent, [

View File

@@ -34,6 +34,7 @@ class LoadParticipation extends AbstractFixture implements OrderedFixtureInterfa
public function __construct()
{
mt_srand(123456789);
$this->faker = \Faker\Factory::create('fr_FR');
}
@@ -45,7 +46,7 @@ class LoadParticipation extends AbstractFixture implements OrderedFixtureInterfa
for ($i = 0; $i < $expectedNumber; ++$i) {
$event = (new Event())
->setDate($this->faker->dateTimeBetween('-2 years', '+6 months'))
->setName($this->faker->words(random_int(2, 4), true))
->setName($this->faker->words(mt_rand(2, 4), true))
->setType($this->getReference(LoadEventTypes::$refs[array_rand(LoadEventTypes::$refs)], EventType::class))
->setCenter($center)
->setCircle(
@@ -78,7 +79,7 @@ class LoadParticipation extends AbstractFixture implements OrderedFixtureInterfa
/** @var Person $person */
foreach ($people as $person) {
$nb = random_int(0, 3);
$nb = mt_rand(0, 3);
for ($i = 0; $i < $nb; ++$i) {
$event = $events[array_rand($events)];

View File

@@ -31,6 +31,7 @@ class LoadAddressReferences extends AbstractFixture implements ContainerAwareInt
public function __construct()
{
mt_srand(123456789);
$this->faker = \Faker\Factory::create('fr_FR');
}
@@ -67,7 +68,7 @@ class LoadAddressReferences extends AbstractFixture implements ContainerAwareInt
$ar->setRefId($this->faker->numerify('ref-id-######'));
$ar->setStreet($this->faker->streetName);
$ar->setStreetNumber((string) random_int(0, 199));
$ar->setStreetNumber((string) mt_rand(0, 199));
$ar->setPoint($this->getRandomPoint());
$ar->setPostcode($this->getReference(
LoadPostalCodes::$refs[array_rand(LoadPostalCodes::$refs)],
@@ -88,8 +89,8 @@ class LoadAddressReferences extends AbstractFixture implements ContainerAwareInt
{
$lonBrussels = 4.35243;
$latBrussels = 50.84676;
$lon = $lonBrussels + 0.01 * random_int(-5, 5);
$lat = $latBrussels + 0.01 * random_int(-5, 5);
$lon = $lonBrussels + 0.01 * mt_rand(-5, 5);
$lat = $latBrussels + 0.01 * mt_rand(-5, 5);
return Point::fromLonLat($lon, $lat);
}

View File

@@ -34,7 +34,7 @@ trait RandomPersonHelperTrait
return $qb
->select('p')
->setMaxResults(1)
->setFirstResult(\random_int(0, $this->nbOfPersons))
->setFirstResult(\mt_rand(0, $this->nbOfPersons))
->getQuery()
->getSingleResult();
}

View File

@@ -37,7 +37,9 @@ class LoadCustomFields extends AbstractFixture implements OrderedFixtureInterfac
private readonly EntityManagerInterface $entityManager,
private readonly CustomFieldChoice $customFieldChoice,
private readonly CustomFieldText $customFieldText,
) {}
) {
mt_srand(123456789);
}
// put your code here
public function getOrder(): int
@@ -78,12 +80,12 @@ class LoadCustomFields extends AbstractFixture implements OrderedFixtureInterfac
// select a set of people and add data
foreach ($personIds as $id) {
// add info on 1 person on 2
if (1 === random_int(0, 1)) {
if (1 === mt_rand(0, 1)) {
/** @var Person $person */
$person = $manager->getRepository(Person::class)->find($id);
$person->setCFData([
'remarques' => $this->createCustomFieldText()
->serialize($faker->text(random_int(150, 250)), $this->cfText),
->serialize($faker->text(mt_rand(150, 250)), $this->cfText),
'document-d-identite' => $this->createCustomFieldChoice()
->serialize([$choices[array_rand($choices)]], $this->cfChoice),
]);

View File

@@ -36,6 +36,7 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
public function __construct(private readonly MembersEditorFactory $editorFactory, private readonly EntityManagerInterface $em)
{
mt_srand(123456789);
$this->loader = new NativeLoader();
}
@@ -72,12 +73,12 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
private function addAddressToHousehold(Household $household, \DateTimeImmutable $date, ObjectManager $manager)
{
if (\random_int(0, 10) > 8) {
if (\mt_rand(0, 10) > 8) {
// 20% of household without address
return;
}
$nb = \random_int(1, 6);
$nb = \mt_rand(1, 6);
$i = 0;
@@ -85,15 +86,15 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
$address = $this->createAddress();
$address->setValidFrom(\DateTime::createFromImmutable($date));
if (\random_int(0, 20) < 1) {
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
if (\mt_rand(0, 20) < 1) {
$date = $date->add(new \DateInterval('P'.\mt_rand(8, 52).'W'));
$address->setValidTo(\DateTime::createFromImmutable($date));
}
$household->addAddress($address);
$manager->persist($address);
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
$date = $date->add(new \DateInterval('P'.\mt_rand(8, 52).'W'));
++$i;
}
}
@@ -127,7 +128,7 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
$k = 0;
foreach ($this->getRandomPersons(1, 3) as $person) {
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$date = $startDate->add(new \DateInterval('P'.\mt_rand(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::ADULT, Position::class);
$movement->addMovement($date, $person, $position, 0 === $k, 'self generated');
@@ -136,7 +137,7 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
// load children
foreach ($this->getRandomPersons(0, 3) as $person) {
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$date = $startDate->add(new \DateInterval('P'.\mt_rand(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::CHILD, Position::class);
$movement->addMovement($date, $person, $position, 0 === $k, 'self generated');
@@ -145,7 +146,7 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
// load children out
foreach ($this->getRandomPersons(0, 2) as $person) {
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$date = $startDate->add(new \DateInterval('P'.\mt_rand(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::CHILD_OUT, Position::class);
$movement->addMovement($date, $person, $position, 0 === $k, 'self generated');
@@ -169,7 +170,7 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
{
$persons = [];
$nb = \random_int($min, $max);
$nb = \mt_rand($min, $max);
for ($i = 0; $i < $nb; ++$i) {
$personId = \array_pop($this->personIds)['id'];

View File

@@ -240,6 +240,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
protected UserRepository $userRepository,
protected GenderRepository $genderRepository,
) {
mt_srand(123456789);
$this->faker = Factory::create('fr_FR');
$this->faker->addProvider($this);
$this->loader = new NativeLoader($this->faker);
@@ -273,7 +274,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
$this->cacheCountries = $this->countryRepository->findAll();
}
if (\random_int(0, 100) > $nullPercentage) {
if (\mt_rand(0, 100) > $nullPercentage) {
return null;
}
@@ -289,7 +290,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
$this->cacheGenders = $this->genderRepository->findByActiveOrdered();
}
if (\random_int(0, 100) > $nullPercentage) {
if (\mt_rand(0, 100) > $nullPercentage) {
return null;
}
@@ -307,7 +308,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
$this->cacheMaritalStatuses = $this->maritalStatusRepository->findAll();
}
if (\random_int(0, 100) > $nullPercentage) {
if (\mt_rand(0, 100) > $nullPercentage) {
return null;
}
@@ -352,7 +353,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
$accompanyingPeriod = new AccompanyingPeriod(
(new \DateTime())
->sub(
new \DateInterval('P'.\random_int(0, 180).'D')
new \DateInterval('P'.\mt_rand(0, 180).'D')
)
);
$accompanyingPeriod->setCreatedBy($this->getRandomUser())
@@ -360,7 +361,7 @@ class LoadPeople extends AbstractFixture implements ContainerAwareInterface, Ord
$person->addAccompanyingPeriod($accompanyingPeriod);
$accompanyingPeriod->addSocialIssue($this->getRandomSocialIssue());
if (\random_int(0, 10) > 3) {
if (\mt_rand(0, 10) > 3) {
// always add social scope:
$accompanyingPeriod->addScope($this->getReference('scope_social', Scope::class));
$origin = $this->getReference(LoadAccompanyingPeriodOrigin::ACCOMPANYING_PERIOD_ORIGIN, AccompanyingPeriod\Origin::class);

View File

@@ -25,7 +25,10 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
{
use PersonRandomHelper;
public function __construct(private readonly EntityManagerInterface $em) {}
public function __construct(private readonly EntityManagerInterface $em)
{
mt_srand(123456789);
}
public function getDependencies(): array
{
@@ -47,8 +50,8 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
->setFromPerson($this->getRandomPerson($this->em))
->setToPerson($this->getRandomPerson($this->em))
->setRelation($this->getReference(LoadRelations::RELATION_KEY.
random_int(0, \count(LoadRelations::RELATIONS) - 1), Relation::class))
->setReverse((bool) random_int(0, 1))
mt_rand(0, \count(LoadRelations::RELATIONS) - 1), Relation::class))
->setReverse((bool) mt_rand(0, 1))
->setCreatedBy($user)
->setUpdatedBy($user)
->setCreatedAt($date)

View File

@@ -55,6 +55,9 @@ final class RelationshipApiControllerTest extends WebTestCase
public static function personProvider(): array
{
// fix a seed to avoid random errors
mt_srand(1234588755);
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$personIdHavingRelation = $em->createQueryBuilder()
@@ -116,6 +119,9 @@ final class RelationshipApiControllerTest extends WebTestCase
public static function relationProvider(): array
{
// fix a seed to avoid random errors
mt_srand(1234588755);
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$personIdWithoutRelations = $em->createQueryBuilder()
@@ -144,6 +150,8 @@ final class RelationshipApiControllerTest extends WebTestCase
->findAll();
}
return self::$relations[\array_rand(self::$relations)];
$keys = array_keys(self::$relations);
return self::$relations[mt_rand(0, \count($keys) - 1)];
}
}

View File

@@ -22,6 +22,11 @@ use Doctrine\Persistence\ObjectManager;
*/
class LoadCustomField extends AbstractFixture implements OrderedFixtureInterface
{
public function __construct()
{
mt_srand(123456789);
}
public function getOrder(): int
{
return 15001;
@@ -67,15 +72,15 @@ class LoadCustomField extends AbstractFixture implements OrderedFixtureInterface
];
for ($i = 0; 25 >= $i; ++$i) {
$cFType = $cFTypes[random_int(0, \count($cFTypes) - 1)];
$cFType = $cFTypes[mt_rand(0, \count($cFTypes) - 1)];
$customField = (new CustomField())
->setSlug("cf_report_{$i}")
->setType($cFType['type'])
->setOptions($cFType['options'])
->setName(['fr' => "CustomField {$i}"])
->setOrdering(random_int(0, 1000) / 1000)
->setCustomFieldsGroup($this->getReference('cf_group_report_'.random_int(0, 3), CustomFieldsGroup::class));
->setOrdering(mt_rand(0, 1000) / 1000)
->setCustomFieldsGroup($this->getReference('cf_group_report_'.mt_rand(0, 3), CustomFieldsGroup::class));
$manager->persist($customField);
}

View File

@@ -35,6 +35,7 @@ final class LoadReports extends AbstractFixture implements OrderedFixtureInterfa
public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
mt_srand(123456789);
$this->faker = FakerFactory::create('fr_FR');
}
@@ -83,7 +84,7 @@ final class LoadReports extends AbstractFixture implements OrderedFixtureInterfa
$report = (new Report())
->setPerson($person)
->setCFGroup(
random_int(0, 10) > 5 ?
mt_rand(0, 10) > 5 ?
$this->getReference('cf_group_report_logement', CustomFieldsGroup::class) :
$this->getReference('cf_group_report_education', CustomFieldsGroup::class)
)
@@ -106,7 +107,7 @@ final class LoadReports extends AbstractFixture implements OrderedFixtureInterfa
// set date. 30% of the dates are 2015-05-01
$expectedDate = new \DateTime('2015-01-05');
if (random_int(0, 100) < 30) {
if (mt_rand(0, 100) < 30) {
$report->setDate($expectedDate);
} else {
$report->setDate($this->faker->dateTimeBetween('-1 year', 'now')
@@ -150,7 +151,7 @@ final class LoadReports extends AbstractFixture implements OrderedFixtureInterfa
$selectedPeople = [];
foreach ($people as $person) {
if (random_int(0, 100) < $percentage) {
if (mt_rand(0, 100) < $percentage) {
$selectedPeople[] = $person;
}
}
@@ -178,7 +179,7 @@ final class LoadReports extends AbstractFixture implements OrderedFixtureInterfa
$picked = [];
if ($multiple) {
$numberSelected = random_int(1, \count($choices) - 1);
$numberSelected = mt_rand(1, \count($choices) - 1);
for ($i = 0; $i < $numberSelected; ++$i) {
$picked[] = $this->pickChoice($choices);

View File

@@ -30,6 +30,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
public function __construct()
{
mt_srand(123456789);
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
@@ -68,7 +69,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
static fn ($a) => $a['ref'],
LoadCenters::$centers
);
$number = random_int(1, \count($references));
$number = mt_rand(1, \count($references));
if (1 === $number) {
yield $this->getReference($references[array_rand($references)], Center::class);