|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the doctrine-orm-refetch package. |
| 7 | + * |
| 8 | + * (c) E-commit <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ecommit\DoctrineOrmRefetch\Tests; |
| 15 | + |
| 16 | +use Doctrine\ORM\EntityManagerInterface; |
| 17 | +use Doctrine\ORM\QueryBuilder; |
| 18 | +use Ecommit\DoctrineOrmRefetch\Exception\SnapshotNotDoneException; |
| 19 | +use Ecommit\DoctrineOrmRefetch\SnapshotManager; |
| 20 | +use Ecommit\DoctrineOrmRefetch\Tests\App\Doctrine; |
| 21 | +use Ecommit\DoctrineOrmRefetch\Tests\App\Entity\Author; |
| 22 | +use Ecommit\DoctrineOrmRefetch\Tests\App\Entity\Book; |
| 23 | + |
| 24 | +class SnapshotManagerTest extends AbstractTest |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var EntityManagerInterface |
| 28 | + */ |
| 29 | + protected $em; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var SnapshotManager |
| 33 | + */ |
| 34 | + protected $snapshotManager; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->em = Doctrine::getEntityManager(); |
| 39 | + $this->em->getConnection()->beginTransaction(); |
| 40 | + $this->snapshotManager = SnapshotManager::create($this->em); |
| 41 | + } |
| 42 | + |
| 43 | + protected function tearDown(): void |
| 44 | + { |
| 45 | + $this->em->getConnection()->rollBack(); |
| 46 | + $this->em->clear(); |
| 47 | + } |
| 48 | + |
| 49 | + public function testCreate(): void |
| 50 | + { |
| 51 | + $refetchManager = SnapshotManager::create($this->em); |
| 52 | + $this->assertEquals(SnapshotManager::class, \get_class($refetchManager)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testGetEntityManager(): void |
| 56 | + { |
| 57 | + $this->assertSame($this->em, $this->snapshotManager->getEntityManager()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testOneObjectWithoutClear(): void |
| 61 | + { |
| 62 | + /** @var Book $book */ |
| 63 | + $book = $this->em->getRepository(Book::class)->find(6); |
| 64 | + $this->assertNotNull($book); |
| 65 | + |
| 66 | + $this->snapshotManager->snapshot(); |
| 67 | + |
| 68 | + $this->checkUnitOfWork(2, [$book, $book->getCategory()]); |
| 69 | + $this->assertEquals(6, $book->getBookId()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testOneObjectWithClearOutsideSnapshot(): void |
| 73 | + { |
| 74 | + $this->snapshotManager->snapshot(); |
| 75 | + |
| 76 | + /** @var Book $book */ |
| 77 | + $book = $this->em->getRepository(Book::class)->find(6); |
| 78 | + $this->assertNotNull($book); |
| 79 | + |
| 80 | + $this->snapshotManager->clear(); |
| 81 | + |
| 82 | + $this->checkUnitOfWork(0, []); |
| 83 | + } |
| 84 | + |
| 85 | + public function testOneObjectWithClearInsideSnapshot(): void |
| 86 | + { |
| 87 | + /** @var Book $book */ |
| 88 | + $book = $this->em->getRepository(Book::class)->find(6); |
| 89 | + $this->assertNotNull($book); |
| 90 | + |
| 91 | + $this->snapshotManager->snapshot(); |
| 92 | + $this->snapshotManager->clear(); |
| 93 | + |
| 94 | + $this->checkUnitOfWork(2, [$book, $book->getCategory()]); |
| 95 | + $this->assertEquals(6, $book->getBookId()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testOneObjectWithClearAndCollection(): void |
| 99 | + { |
| 100 | + /** @var Book $book */ |
| 101 | + $book = $this->em->getRepository(Book::class)->find(9); |
| 102 | + $this->assertNotNull($book); |
| 103 | + |
| 104 | + $this->snapshotManager->snapshot(); |
| 105 | + |
| 106 | + $authors = $book->getAuthors()->getIterator(); |
| 107 | + $this->checkUnitOfWork(4, [$book, $book->getCategory()]); |
| 108 | + |
| 109 | + $this->snapshotManager->clear(); |
| 110 | + |
| 111 | + $this->checkUnitOfWork(2, [$book, $book->getCategory()]); |
| 112 | + } |
| 113 | + |
| 114 | + public function testInIterate(): void |
| 115 | + { |
| 116 | + $entityManager = $this->em; |
| 117 | + |
| 118 | + // Example in README.md - Beginning |
| 119 | + |
| 120 | + $snapshotManager = SnapshotManager::create($entityManager); |
| 121 | + |
| 122 | + $author = $entityManager->getRepository(Author::class)->find(1); |
| 123 | + $this->assertNotNull($author); // Remove this line in Example |
| 124 | + |
| 125 | + $snapshotManager->snapshot(); |
| 126 | + |
| 127 | + $queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b'); |
| 128 | + $queryBuilder->select('b') |
| 129 | + ->andWhere('b.bookId != :bookId') |
| 130 | + ->setParameter('bookId', 7); |
| 131 | + $iterableResult = $queryBuilder->getQuery()->iterate(); |
| 132 | + |
| 133 | + $i = 0; |
| 134 | + foreach ($iterableResult as $row) { |
| 135 | + ++$i; |
| 136 | + /** @var Book $book */ |
| 137 | + $book = current($row); |
| 138 | + |
| 139 | + if (!$book->getAuthors()->contains($author)) { |
| 140 | + $book->addAuthor($author); |
| 141 | + } |
| 142 | + |
| 143 | + if (0 === $i % 2) { |
| 144 | + // $author and $book are managed |
| 145 | + $entityManager->flush(); |
| 146 | + $snapshotManager->clear(); // Detach all entities attached since the snapshot |
| 147 | + // Only $author is managed |
| 148 | + |
| 149 | + $this->checkUnitOfWork(1, [$author]); // Remove this line in Example |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + $entityManager->flush(); |
| 154 | + $snapshotManager->clear(); |
| 155 | + |
| 156 | + // Example in README.md - End |
| 157 | + |
| 158 | + /** @var QueryBuilder $queryBuilder */ |
| 159 | + $queryBuilder = $this->em->getRepository(Book::class)->createQueryBuilder('b'); |
| 160 | + $queryBuilder->select('count(b) as nbre') |
| 161 | + ->leftJoin('b.authors', 'a') |
| 162 | + ->andWhere('a.authorId = :authorId') |
| 163 | + ->setParameter('authorId', 1); |
| 164 | + $count = $queryBuilder->getQuery()->getSingleScalarResult(); |
| 165 | + |
| 166 | + $this->assertEquals(9, $count); |
| 167 | + } |
| 168 | + |
| 169 | + public function testSnapshotNotDone(): void |
| 170 | + { |
| 171 | + /** @var Book $book */ |
| 172 | + $book = $this->em->getRepository(Book::class)->find(6); |
| 173 | + $this->assertNotNull($book); |
| 174 | + |
| 175 | + $this->expectException(SnapshotNotDoneException::class); |
| 176 | + $this->expectDeprecationMessage('The snapshot was not done'); |
| 177 | + |
| 178 | + $this->snapshotManager->clear(); |
| 179 | + } |
| 180 | +} |
0 commit comments