File: OptionallyUnqualifiedNamedObjectSetTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (190 lines) | stat: -rw-r--r-- 6,048 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Schema\Collections;

use Doctrine\DBAL\Schema\Collections\Exception\ObjectAlreadyExists;
use Doctrine\DBAL\Schema\Collections\Exception\ObjectDoesNotExist;
use Doctrine\DBAL\Schema\Collections\OptionallyUnqualifiedNamedObjectSet;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\OptionallyNamedObject;
use PHPUnit\Framework\TestCase;

class OptionallyUnqualifiedNamedObjectSetTest extends TestCase
{
    public function testInstantiationWithoutArguments(): void
    {
        $set = new OptionallyUnqualifiedNamedObjectSet();

        self::assertTrue($set->isEmpty());
        self::assertEmpty($set->toList());
    }

    public function testInstantiationWithArguments(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2);

        self::assertSame([$object1, $object2], $set->toList());
    }

    public function testAdd(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1);

        $set->add($object2);
        self::assertSame([$object1, $object2], $set->toList());
    }

    public function testAddExistingObject(): void
    {
        $object = $this->createObject('object', 1);

        $set = new OptionallyUnqualifiedNamedObjectSet($object);

        $this->expectException(ObjectAlreadyExists::class);

        $set->add($object);
    }

    public function testRemove(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2);

        $set->remove(UnqualifiedName::unquoted('object1'));
        self::assertSame([$object2], $set->toList());
    }

    public function testRemoveNonExistingObject(): void
    {
        $object1 = $this->createObject('object1', 1);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1);

        $this->expectException(ObjectDoesNotExist::class);

        $set->remove(UnqualifiedName::unquoted('object2'));
    }

    public function testGetExistingObject(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);
        $object3 = $this->createObject('object3', 3);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2, $object3);

        self::assertSame($object1, $set->get(UnqualifiedName::unquoted('object1')));
        self::assertSame($object3, $set->get(UnqualifiedName::unquoted('object3')));
    }

    public function testGetNonExistingObject(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2);

        self::assertNull($set->get(UnqualifiedName::unquoted('object3')));
    }

    public function testModifyObject(): void
    {
        $object11 = $this->createObject('object1', 11);
        $object12 = $this->createObject('object1', 12);
        $object2  = $this->createObject('object2', 2);

        $set = new OptionallyUnqualifiedNamedObjectSet($object11, $object2);

        $set->modify(
            UnqualifiedName::unquoted('object1'),
            static fn (OptionallyNamedObject $object): OptionallyNamedObject => $object12,
        );

        self::assertSame([$object12, $object2], $set->toList());
    }

    public function testModifyNonExistingObject(): void
    {
        $object1 = $this->createObject('object1', 1);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1);

        $this->expectException(ObjectDoesNotExist::class);

        $set->modify(
            UnqualifiedName::unquoted('object2'),
            static fn (OptionallyNamedObject $object): OptionallyNamedObject => $object,
        );
    }

    public function testRenameToExistingName(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);
        $object3 = $this->createObject('object3', 3);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2, $object3);

        $this->expectException(ObjectAlreadyExists::class);

        $set->modify(
            UnqualifiedName::unquoted('object1'),
            static fn (OptionallyNamedObject $object): OptionallyNamedObject => $object3,
        );
    }

    public function testRenameToNullName(): void
    {
        $object1 = $this->createObject('object1', 1);
        $object2 = $this->createObject(null, 2);
        $object3 = $this->createObject('object3', 3);

        $set = new OptionallyUnqualifiedNamedObjectSet($object1, $object2, $object3);

        $set->modify(
            UnqualifiedName::unquoted('object1'),
            static fn (OptionallyNamedObject $object): OptionallyNamedObject => $object2,
        );

        self::assertSame([$object2, $object2, $object3], $set->toList());
    }

    /**
     * @param ?non-empty-string $name
     *
     * @return OptionallyNamedObject<UnqualifiedName>
     */
    private function createObject(?string $name, int $value): OptionallyNamedObject
    {
        return new /** @template-implements OptionallyNamedObject<UnqualifiedName> */
        class ($name, $value) implements OptionallyNamedObject {
            private readonly ?UnqualifiedName $name;

            /** @param ?non-empty-string $name */
            public function __construct(?string $name, private readonly int $value)
            {
                $this->name = $name !== null ? UnqualifiedName::unquoted($name) : null;
            }

            public function getObjectName(): ?UnqualifiedName
            {
                return $this->name;
            }

            public function getValue(): int
            {
                return $this->value;
            }
        };
    }
}