|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Unit\Entity; |
| 15 | + |
| 16 | +use Codeception\Test\Unit; |
| 17 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 18 | +use Doctrine\ORM\Mapping\Driver\AttributeDriver; |
| 19 | +use Doctrine\Persistence\Mapping\RuntimeReflectionService; |
| 20 | +use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue; |
| 21 | + |
| 22 | +/** |
| 23 | + * The ORM metadata must match the schema created by the Installer, otherwise a |
| 24 | + * doctrine schema update regenerates the queue table without the auto increment |
| 25 | + * on `id` (every row is inserted with id 0, so `INSERT IGNORE` keeps only one |
| 26 | + * row) and without the unique index the queue deduplication relies on. |
| 27 | + * |
| 28 | + * @see https://github.com/pimcore/generic-data-index-bundle/issues/432 |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +final class IndexQueueTest extends Unit |
| 33 | +{ |
| 34 | + public function testIdUsesGeneratedValue(): void |
| 35 | + { |
| 36 | + $metadata = $this->loadClassMetadata(); |
| 37 | + |
| 38 | + $this->assertNotSame( |
| 39 | + ClassMetadata::GENERATOR_TYPE_NONE, |
| 40 | + $metadata->generatorType, |
| 41 | + 'IndexQueue::$id must declare #[ORM\GeneratedValue] so schema updates keep the auto increment' |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public function testElementIdTypeUniqueConstraintIsDeclared(): void |
| 46 | + { |
| 47 | + $metadata = $this->loadClassMetadata(); |
| 48 | + |
| 49 | + $uniqueConstraints = $metadata->table['uniqueConstraints'] ?? []; |
| 50 | + $constraintName = IndexQueue::TABLE . '_element_id_type'; |
| 51 | + |
| 52 | + $this->assertArrayHasKey( |
| 53 | + $constraintName, |
| 54 | + $uniqueConstraints, |
| 55 | + 'IndexQueue must declare the unique constraint on (elementId, elementType) the queue deduplication relies on' |
| 56 | + ); |
| 57 | + $this->assertSame( |
| 58 | + ['elementId', 'elementType'], |
| 59 | + $uniqueConstraints[$constraintName]['columns'] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + private function loadClassMetadata(): ClassMetadata |
| 64 | + { |
| 65 | + $metadata = new ClassMetadata(IndexQueue::class); |
| 66 | + $metadata->initializeReflection(new RuntimeReflectionService()); |
| 67 | + |
| 68 | + (new AttributeDriver([]))->loadMetadataForClass(IndexQueue::class, $metadata); |
| 69 | + |
| 70 | + return $metadata; |
| 71 | + } |
| 72 | +} |
0 commit comments