|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Persisters; |
| 6 | + |
| 7 | +use Doctrine\DBAL\DriverManager; |
| 8 | +use Doctrine\DBAL\Types\Type as DbalType; |
| 9 | +use Doctrine\ORM\EntityManager; |
| 10 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 11 | +use Doctrine\ORM\ORMSetup; |
| 12 | +use Doctrine\ORM\Tools\SchemaTool; |
| 13 | +use Doctrine\ORM\Tools\SchemaValidator; |
| 14 | +use Doctrine\Tests\Mocks\EntityManagerMock; |
| 15 | +use Doctrine\Tests\Models\BinaryPrimaryKey\BinaryIdType; |
| 16 | +use Doctrine\Tests\Models\BinaryPrimaryKey\Category; |
| 17 | +use Doctrine\Tests\OrmTestCase; |
| 18 | + |
| 19 | +final class BinaryIdPersisterTest extends OrmTestCase |
| 20 | +{ |
| 21 | + private EntityManager|null $entityManager = null; |
| 22 | + |
| 23 | + public function testOneToManyWithEagerFetchMode(): void |
| 24 | + { |
| 25 | + $entityManager = $this->createEntityManager(); |
| 26 | + |
| 27 | + $this->createDummyBlogData($entityManager, 3); |
| 28 | + |
| 29 | + $categories = $entityManager->createQueryBuilder() |
| 30 | + ->select('category') |
| 31 | + ->from(Category::class, 'category') |
| 32 | + ->getQuery() |
| 33 | + ->setFetchMode(Category::class, 'children', ClassMetadata::FETCH_EAGER) |
| 34 | + ->getResult(); |
| 35 | + |
| 36 | + self::assertCount(3, $categories); |
| 37 | + } |
| 38 | + |
| 39 | + private function createDummyBlogData( |
| 40 | + EntityManager $entityManager, |
| 41 | + int $categoryCount = 1, |
| 42 | + int $categoryParentsCount = 0, |
| 43 | + ): void { |
| 44 | + for ($h = 0; $h < $categoryCount; $h++) { |
| 45 | + $categoryParent = null; |
| 46 | + |
| 47 | + for ($i = 0; $i < $categoryParentsCount; $i++) { |
| 48 | + $categoryParent = new Category('CategoryParent#' . $i, $categoryParent); |
| 49 | + $entityManager->persist($categoryParent); |
| 50 | + } |
| 51 | + |
| 52 | + $category = new Category('Category#' . $h, $categoryParent); |
| 53 | + $entityManager->persist($category); |
| 54 | + } |
| 55 | + |
| 56 | + $entityManager->flush(); |
| 57 | + $entityManager->clear(); |
| 58 | + } |
| 59 | + |
| 60 | + private function createEntityManager(): EntityManager |
| 61 | + { |
| 62 | + if ($this->entityManager !== null) { |
| 63 | + return $this->entityManager; |
| 64 | + } |
| 65 | + |
| 66 | + $config = ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/../../Models/BinaryPrimaryKey'], isDevMode: true); |
| 67 | + |
| 68 | + if (! DbalType::hasType(BinaryIdType::NAME)) { |
| 69 | + DbalType::addType(BinaryIdType::NAME, BinaryIdType::class); |
| 70 | + } |
| 71 | + |
| 72 | + $connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $config); |
| 73 | + $entityManager = new EntityManagerMock($connection, $config); |
| 74 | + |
| 75 | + $schemaTool = new SchemaTool($entityManager); |
| 76 | + $schemaTool->createSchema($entityManager->getMetadataFactory()->getAllMetadata()); |
| 77 | + |
| 78 | + $schemaValidator = new SchemaValidator($entityManager); |
| 79 | + $schemaValidator->validateMapping(); |
| 80 | + |
| 81 | + $this->entityManager = $entityManager; |
| 82 | + |
| 83 | + return $entityManager; |
| 84 | + } |
| 85 | +} |
0 commit comments