1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\Tests\OrmFunctionalTestCase;
class CustomRepositoryTest extends OrmFunctionalTestCase
{
use VerifyDeprecations;
protected function setUp(): void
{
parent::setUp();
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9533');
$this->createSchemaForModels(MinimalEntity::class, OtherMinimalEntity::class);
}
public function testMinimalRepository(): void
{
$this->_em->persist(new MinimalEntity('foo'));
$this->_em->persist(new MinimalEntity('bar'));
$this->_em->flush();
$this->_em->clear();
$repository = $this->_em->getRepository(MinimalEntity::class);
self::assertInstanceOf(MinimalRepository::class, $repository);
self::assertSame('foo', $repository->find('foo')->id);
}
public function testMinimalDefaultRepository(): void
{
$this->_em->getConfiguration()->setDefaultRepositoryClassName(MinimalRepository::class);
$this->_em->persist(new OtherMinimalEntity('foo'));
$this->_em->persist(new OtherMinimalEntity('bar'));
$this->_em->flush();
$this->_em->clear();
$repository = $this->_em->getRepository(OtherMinimalEntity::class);
self::assertInstanceOf(MinimalRepository::class, $repository);
self::assertSame('foo', $repository->find('foo')->id);
}
}
/** @ORM\Entity(repositoryClass="MinimalRepository") */
class MinimalEntity
{
/**
* @ORM\Column
* @ORM\Id
*
* @var string
*/
public $id;
public function __construct(string $id)
{
$this->id = $id;
}
}
/** @ORM\Entity */
class OtherMinimalEntity
{
/**
* @ORM\Column
* @ORM\Id
*
* @var string
*/
public $id;
public function __construct(string $id)
{
$this->id = $id;
}
}
/**
* @template TEntity of object
* @implements ObjectRepository<TEntity>
*/
class MinimalRepository implements ObjectRepository
{
/** @var EntityManagerInterface */
private $em;
/** @var ClassMetadata<TEntity> */
private $class;
/** @psalm-param ClassMetadata<TEntity> $class */
public function __construct(EntityManagerInterface $em, ClassMetadata $class)
{
$this->em = $em;
$this->class = $class;
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return $this->em->find($this->class->name, $id);
}
/**
* {@inheritdoc}
*/
public function findAll(): array
{
return $this->findBy([]);
}
/**
* {@inheritdoc}
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
$persister = $this->em->getUnitOfWork()->getEntityPersister($this->class->name);
return $persister->loadAll($criteria, $orderBy, $limit, $offset);
}
/**
* {@inheritdoc}
*/
public function findOneBy(array $criteria)
{
$persister = $this->em->getUnitOfWork()->getEntityPersister($this->class->name);
return $persister->load($criteria, null, null, [], null, 1);
}
public function getClassName(): string
{
return $this->class->name;
}
}
|