mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-02 11:03:42 +00:00
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.
This commit is contained in:
30
src/Bundle/ChillMainBundle/Test/RandomUserTrait.php
Normal file
30
src/Bundle/ChillMainBundle/Test/RandomUserTrait.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user