File: IdentifierFlattenerEnumIdTest.php

package info (click to toggle)
doctrine 2.14.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,612 kB
  • sloc: php: 113,660; xml: 4,630; makefile: 28; sh: 14
file content (86 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Utility;

use Doctrine\Tests\Models\Enums\Suit;
use Doctrine\Tests\Models\Enums\TypedCardEnumCompositeId;
use Doctrine\Tests\Models\Enums\TypedCardEnumId;
use Doctrine\Tests\Models\Enums\Unit;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
 * Test the IdentifierFlattener utility class
 *
 * @requires PHP 8.1
 * @covers \Doctrine\ORM\Utility\IdentifierFlattener
 */
class IdentifierFlattenerEnumIdTest extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->createSchemaForModels(
            TypedCardEnumId::class,
            TypedCardEnumCompositeId::class
        );
    }

    /** @group utilities */
    public function testFlattenIdentifierWithEnumId(): void
    {
        $typedCardEnumIdEntity       = new TypedCardEnumId();
        $typedCardEnumIdEntity->suit = Suit::Clubs;

        $this->_em->persist($typedCardEnumIdEntity);
        $this->_em->flush();
        $this->_em->clear();

        $findTypedCardEnumIdEntityNotFound = $this->_em->getRepository(TypedCardEnumId::class)->find(Suit::Diamonds);

        self::assertNull($findTypedCardEnumIdEntityNotFound, 'Search by non-persisted Enum ID does not work');

        $findTypedCardEnumIdEntity = $this->_em->getRepository(TypedCardEnumId::class)->find(Suit::Clubs);

        self::assertNotNull($findTypedCardEnumIdEntity, 'Search by Enum ID does not work');

        $class = $this->_em->getClassMetadata(TypedCardEnumId::class);

        $id = $class->getIdentifierValues($findTypedCardEnumIdEntity);

        self::assertCount(1, $id, 'We should have 1 identifier');

        self::assertEquals(Suit::Clubs, $findTypedCardEnumIdEntity->suit);
    }

    /** @group utilities */
    public function testFlattenIdentifierWithCompositeEnumId(): void
    {
        $typedCardEnumCompositeIdEntity       = new TypedCardEnumCompositeId();
        $typedCardEnumCompositeIdEntity->suit = Suit::Clubs;
        $typedCardEnumCompositeIdEntity->unit = Unit::Gram;

        $this->_em->persist($typedCardEnumCompositeIdEntity);
        $this->_em->flush();
        $this->_em->clear();

        $findTypedCardEnumCompositeIdEntityNotFound = $this->_em->getRepository(TypedCardEnumCompositeId::class)->find(['suit' => Suit::Diamonds, 'unit' => Unit::Gram]);

        self::assertNull($findTypedCardEnumCompositeIdEntityNotFound, 'Search by non-persisted composite Enum ID does not work');

        $findTypedCardEnumCompositeIdEntity = $this->_em->getRepository(TypedCardEnumCompositeId::class)->find(['suit' => Suit::Clubs, 'unit' => Unit::Gram]);

        self::assertNotNull($findTypedCardEnumCompositeIdEntity, 'Search by composite Enum ID does not work');

        $class = $this->_em->getClassMetadata(TypedCardEnumCompositeId::class);

        $id = $class->getIdentifierValues($findTypedCardEnumCompositeIdEntity);

        self::assertCount(2, $id, 'We should have 2 identifiers');

        self::assertEquals(Suit::Clubs, $findTypedCardEnumCompositeIdEntity->suit);
        self::assertEquals(Unit::Gram, $findTypedCardEnumCompositeIdEntity->unit);
    }
}