File: OneToManySelfReferentialAssociationTest.php

package info (click to toggle)
doctrine 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,552 kB
  • sloc: php: 108,302; xml: 1,340; makefile: 35; sh: 14
file content (132 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download | duplicates (3)
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
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
use Doctrine\Tests\OrmFunctionalTestCase;

use function strstr;

/**
 * Tests a bidirectional one-to-one association mapping (without inheritance).
 */
class OneToManySelfReferentialAssociationTest extends OrmFunctionalTestCase
{
    private ECommerceCategory $parent;

    private ECommerceCategory $firstChild;

    private ECommerceCategory $secondChild;

    protected function setUp(): void
    {
        $this->useModelSet('ecommerce');

        parent::setUp();

        $this->parent = new ECommerceCategory();
        $this->parent->setName('Programming languages books');
        $this->firstChild = new ECommerceCategory();
        $this->firstChild->setName('Java books');
        $this->secondChild = new ECommerceCategory();
        $this->secondChild->setName('Php books');
    }

    public function testSavesAOneToManyAssociationWithCascadeSaveSet(): void
    {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
        $this->_em->persist($this->parent);

        $this->_em->flush();

        $this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
        $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
    }

    public function testSavesAnEmptyCollection(): void
    {
        $this->_em->persist($this->parent);
        $this->_em->flush();

        self::assertCount(0, $this->parent->getChildren());
    }

    public function testDoesNotSaveAnInverseSideSet(): void
    {
        $this->parent->brokenAddChild($this->firstChild);
        $this->_em->persist($this->parent);
        $this->_em->flush();

        $this->assertForeignKeyIs(null, $this->firstChild);
    }

    public function testRemovesOneToManyAssociation(): void
    {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
        $this->_em->persist($this->parent);

        $this->parent->removeChild($this->firstChild);
        $this->_em->flush();

        $this->assertForeignKeyIs(null, $this->firstChild);
        $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
    }

    public function testEagerLoadsOneToManyAssociation(): void
    {
        $this->createFixture();

        $query  = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
        $result = $query->getResult();
        self::assertCount(1, $result);
        $parent   = $result[0];
        $children = $parent->getChildren();

        self::assertInstanceOf(ECommerceCategory::class, $children[0]);
        self::assertSame($parent, $children[0]->getParent());
        self::assertEquals(' books', strstr($children[0]->getName(), ' books'));
        self::assertInstanceOf(ECommerceCategory::class, $children[1]);
        self::assertSame($parent, $children[1]->getParent());
        self::assertEquals(' books', strstr($children[1]->getName(), ' books'));
    }

    public function testLazyLoadsOneToManyAssociation(): void
    {
        $this->createFixture();
        $metadata                                         = $this->_em->getClassMetadata(ECommerceCategory::class);
        $metadata->associationMappings['children']->fetch = ClassMetadata::FETCH_LAZY;

        $query    = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
        $result   = $query->getResult();
        $parent   = $result[0];
        $children = $parent->getChildren();

        self::assertInstanceOf(ECommerceCategory::class, $children[0]);
        self::assertSame($parent, $children[0]->getParent());
        self::assertEquals(' books', strstr($children[0]->getName(), ' books'));
        self::assertInstanceOf(ECommerceCategory::class, $children[1]);
        self::assertSame($parent, $children[1]->getParent());
        self::assertEquals(' books', strstr($children[1]->getName(), ' books'));
    }

    private function createFixture(): void
    {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
        $this->_em->persist($this->parent);

        $this->_em->flush();
        $this->_em->clear();
    }

    public function assertForeignKeyIs($value, ECommerceCategory $child): void
    {
        $foreignKey = $this->_em->getConnection()->executeQuery('SELECT parent_id FROM ecommerce_categories WHERE id=?', [$child->getId()])->fetchOne();
        self::assertEquals($value, $foreignKey);
    }
}