-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathAssociationMappingTest.php
129 lines (105 loc) · 4.31 KB
/
AssociationMappingTest.php
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
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ManyToManyInverseSideMapping;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Mapping\OneToManyAssociationMapping;
use OutOfRangeException;
use PHPUnit\Framework\TestCase;
use function assert;
use function serialize;
use function unserialize;
final class AssociationMappingTest extends TestCase
{
public function testItSurvivesSerialization(): void
{
$mapping = new MyAssociationMapping(
fieldName: 'foo',
sourceEntity: self::class,
targetEntity: self::class,
);
$mapping->cascade = ['persist'];
$mapping->fetch = ClassMetadata::FETCH_EAGER;
$mapping->inherited = self::class;
$mapping->declared = self::class;
$mapping->cache = ['usage' => ClassMetadata::CACHE_USAGE_READ_ONLY];
$mapping->id = true;
$mapping->isOnDeleteCascade = true;
$mapping->originalClass = self::class;
$mapping->originalField = 'foo';
$mapping->orphanRemoval = true;
$mapping->unique = true;
$resurrectedMapping = unserialize(serialize($mapping));
assert($resurrectedMapping instanceof AssociationMapping);
self::assertSame(['persist'], $resurrectedMapping->cascade);
self::assertSame(ClassMetadata::FETCH_EAGER, $resurrectedMapping->fetch);
self::assertSame(self::class, $resurrectedMapping->inherited);
self::assertSame(self::class, $resurrectedMapping->declared);
self::assertSame(['usage' => ClassMetadata::CACHE_USAGE_READ_ONLY], $resurrectedMapping->cache);
self::assertTrue($resurrectedMapping->id);
self::assertTrue($resurrectedMapping->isOnDeleteCascade);
self::assertSame(self::class, $resurrectedMapping->originalClass);
self::assertSame('foo', $resurrectedMapping->originalField);
self::assertTrue($resurrectedMapping->orphanRemoval);
self::assertTrue($resurrectedMapping->unique);
}
public function testItThrowsWhenAccessingUnknownProperty(): void
{
$mapping = new MyAssociationMapping(
fieldName: 'foo',
sourceEntity: self::class,
targetEntity: self::class,
);
$this->expectException(OutOfRangeException::class);
$mapping['foo'];
}
public function testItThrowsWhenSettingUnknownProperty(): void
{
$mapping = new MyAssociationMapping(
fieldName: 'foo',
sourceEntity: self::class,
targetEntity: self::class,
);
$this->expectException(OutOfRangeException::class);
$mapping['foo'] = 'bar';
}
public function testItThrowsWhenUnsettingUnknownProperty(): void
{
$mapping = new MyAssociationMapping(
fieldName: 'foo',
sourceEntity: self::class,
targetEntity: self::class,
);
$this->expectException(OutOfRangeException::class);
unset($mapping['foo']);
}
public function testItThrowsWhenJoinTableIsDefinedOnNonManyToManyProperty(): void
{
$mapping = [
'fieldName' => 'foo',
'sourceEntity' => self::class,
'targetEntity' => self::class,
'joinTable' => 'bar',
];
$this->expectException(MappingException::class);
$this->expectExceptionMessage("Mapping error on field 'foo' in " . self::class . " : relation is not many-to-many, but 'joinTable' is set.");
OneToManyAssociationMapping::fromMappingArray($mapping);
}
public function testItThrowsWhenJoinTableIsDefinedOnManyToManyInverseSideProperty(): void
{
$mapping = [
'fieldName' => 'foo',
'sourceEntity' => self::class,
'targetEntity' => self::class,
'joinTable' => 'bar',
];
$this->expectException(MappingException::class);
$this->expectExceptionMessage("Mapping error on field 'foo' in " . self::class . " : 'joinTable' can only be set on many-to-many owning side.");
ManyToManyInverseSideMapping::fromMappingArray($mapping);
}
}
class MyAssociationMapping extends AssociationMapping
{
}