Files
chill-bundles/src/Bundle/ChillMainBundle/Test/RandomUserTrait.php
Julien Fastré 277e4fa490 Add RandomUserTrait to retrieve random user for testing
- Implemented `getRandomUser` method to fetch a random user using `EntityManagerInterface`.
- Added support for random user retrieval by querying `User` entity repository.
2026-03-31 17:32:36 +02:00

31 lines
746 B
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
trait RandomUserTrait
{
public function getRandomUser(EntityManagerInterface $em): User
{
$userRepository = $em->getRepository(User::class);
$count = $userRepository->count([]);
$random = mt_rand(0, $count - 1);
return $em->createQuery('SELECT u FROM '.User::class.' u ')
->setMaxResults(1)
->setFirstResult($random)
->getSingleResult();
}
}