1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\RelationshipHelper; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @small |
14
|
|
|
* @testdox EdmondsCommerce\DoctrineStaticMeta\RelationshipHelper |
15
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\RelationshipHelper |
16
|
|
|
*/ |
17
|
|
|
class RelationshipHelperTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $mapping |
22
|
|
|
* @param bool $expectedResult |
23
|
|
|
* @param bool $expectException |
24
|
|
|
* |
25
|
|
|
* @test |
26
|
|
|
* @dataProvider isPluralData |
27
|
|
|
*/ |
28
|
|
|
public function itCanDetermineIfPlural(array $mapping, bool $expectedResult, bool $expectException): void |
29
|
|
|
{ |
30
|
|
|
$class = $this->getTestClass(); |
31
|
|
|
if ($expectException === true) { |
32
|
|
|
$this->expectException(InvalidArgumentException::class); |
33
|
|
|
} |
34
|
|
|
$actualResult = $class->isPlural($mapping); |
35
|
|
|
self::assertSame($expectedResult, $actualResult); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $mapping |
40
|
|
|
* @param string $expectedMethod |
41
|
|
|
* @param bool $expectException |
42
|
|
|
* |
43
|
|
|
* @test |
44
|
|
|
* @dataProvider adderData |
45
|
|
|
*/ |
46
|
|
|
public function itCanGetTheAdders(array $mapping, string $expectedMethod, bool $expectException): void |
47
|
|
|
{ |
48
|
|
|
$class = $this->getTestClass(); |
49
|
|
|
if ($expectException === true) { |
50
|
|
|
$this->expectException(InvalidArgumentException::class); |
51
|
|
|
} |
52
|
|
|
$actualMethod = $class->getAdderFromDoctrineMapping($mapping); |
53
|
|
|
self::assertSame($expectedMethod, $actualMethod); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $mapping |
58
|
|
|
* @param string $expectedMethod |
59
|
|
|
* |
60
|
|
|
* @test |
61
|
|
|
* @dataProvider getterData |
62
|
|
|
*/ |
63
|
|
|
public function itCanGetTheGetters(array $mapping, string $expectedMethod): void |
64
|
|
|
{ |
65
|
|
|
$class = $this->getTestClass(); |
66
|
|
|
$actualMethod = $class->getGetterFromDoctrineMapping($mapping); |
67
|
|
|
self::assertSame($expectedMethod, $actualMethod); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $mapping |
72
|
|
|
* @param string $expectedMethod |
73
|
|
|
* @param bool $expectException |
74
|
|
|
* |
75
|
|
|
* @test |
76
|
|
|
* @dataProvider removerData |
77
|
|
|
*/ |
78
|
|
|
public function itCanGetTheRemovers(array $mapping, string $expectedMethod, bool $expectException): void |
79
|
|
|
{ |
80
|
|
|
$class = $this->getTestClass(); |
81
|
|
|
if ($expectException === true) { |
82
|
|
|
$this->expectException(InvalidArgumentException::class); |
83
|
|
|
} |
84
|
|
|
$actualMethod = $class->getRemoverFromDoctrineMapping($mapping); |
85
|
|
|
self::assertSame($expectedMethod, $actualMethod); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $mapping |
90
|
|
|
* @param string $expectedMethod |
91
|
|
|
* |
92
|
|
|
* @test |
93
|
|
|
* @dataProvider setterData |
94
|
|
|
*/ |
95
|
|
|
public function itCanGetTheSetters(array $mapping, string $expectedMethod): void |
96
|
|
|
{ |
97
|
|
|
$class = $this->getTestClass(); |
98
|
|
|
$actualMethod = $class->getSetterFromDoctrineMapping($mapping); |
99
|
|
|
self::assertSame($expectedMethod, $actualMethod); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getterData(): array |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::ONE_TO_ONE), 'getUser'], |
106
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::MANY_TO_ONE), 'getUser'], |
107
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::ONE_TO_MANY), 'getUsers'], |
108
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::MANY_TO_MANY), 'getUsers'], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function setterData(): array |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::ONE_TO_ONE), 'setUser'], |
116
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::MANY_TO_ONE), 'setUser'], |
117
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::ONE_TO_MANY), 'setUsers'], |
118
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::MANY_TO_MANY), 'setUsers'], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function adderData(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::MANY_TO_ONE), 'setUser', true], |
126
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::ONE_TO_MANY), 'addUser', false], |
127
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::MANY_TO_MANY), 'addUser', false], |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function removerData(): array |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::MANY_TO_ONE), 'setUser', true], |
135
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::ONE_TO_MANY), 'removeUser', false], |
136
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::MANY_TO_MANY), 'removeUser', false], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function isPluralData(): array |
141
|
|
|
{ |
142
|
|
|
return [ |
143
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::ONE_TO_ONE), false, false], |
144
|
|
|
[$this->getMappingArray('user', ClassMetadataInfo::MANY_TO_ONE), false, false], |
145
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::ONE_TO_MANY), true, false], |
146
|
|
|
[$this->getMappingArray('users', ClassMetadataInfo::MANY_TO_MANY), true, false], |
147
|
|
|
[$this->getMappingArray('users', 100), true, true], |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function getMappingArray(string $property, int $type): array |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
'fieldName' => $property, |
155
|
|
|
'type' => $type, |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function getTestClass(): RelationshipHelper |
160
|
|
|
{ |
161
|
|
|
return new RelationshipHelper(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|