<?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();
    }
}
