buckets/tests/Concerns/CreatesUsers.php

30 lines
794 B
PHP

<?php
namespace App\Tests\Concerns;
use App\Entity\User;
/**
* Persists throwaway User owners for tests that need a Scenario owner but don't
* care about the user itself. Emails are made unique per test class + call so
* the same suite can mint many owners without colliding on the unique email
* constraint. The consuming test must expose the EntityManager as $this->em.
*/
trait CreatesUsers
{
private function persistOwner(): User
{
static $counter = 0;
++$counter;
$prefix = strtolower(str_replace('\\', '-', static::class));
$owner = new User();
$owner->setEmail(\sprintf('%s-%d@example.com', $prefix, $counter));
$owner->setPassword('irrelevant-hash');
$this->em->persist($owner);
return $owner;
}
}