1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\tests\unit\type; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\type\Type; |
14
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class TypeTest extends \PHPUnit\Framework\TestCase |
20
|
|
|
{ |
21
|
|
|
protected $sid1 = 101; |
22
|
|
|
protected $sid2 = 102; |
23
|
|
|
|
24
|
|
|
protected $sop1 = 'server1'; |
25
|
|
|
protected $sop2 = 'server2'; |
26
|
|
|
|
27
|
|
|
protected $did1 = 201; |
28
|
|
|
protected $did2 = 202; |
29
|
|
|
|
30
|
|
|
protected $dop1 = 'domain1'; |
31
|
|
|
protected $dop2 = 'domain2'; |
32
|
|
|
|
33
|
|
|
private Type $nonenone; |
34
|
|
|
|
35
|
|
|
private Type $server11; |
36
|
|
|
private Type $server12; |
37
|
|
|
private Type $server_1; |
38
|
|
|
private Type $server1_; |
39
|
|
|
private Type $serverN1; |
40
|
|
|
private Type $server1N; |
41
|
|
|
private Type $serverN_; |
42
|
|
|
private Type $server_N; |
43
|
|
|
private Type $server22; |
44
|
|
|
private Type $server21; |
45
|
|
|
private Type $server_2; |
46
|
|
|
private Type $server2_; |
47
|
|
|
private Type $serverN2; |
48
|
|
|
private Type $server2N; |
49
|
|
|
|
50
|
|
|
private Type $domain11; |
51
|
|
|
private Type $domain_1; |
52
|
|
|
private Type $domain1_; |
53
|
|
|
private Type $domainN1; |
54
|
|
|
private Type $domain1N; |
55
|
|
|
private Type $domain22; |
56
|
|
|
private Type $domain_2; |
57
|
|
|
private Type $domain2_; |
58
|
|
|
|
59
|
|
|
protected function setUp(): void |
60
|
|
|
{ |
61
|
|
|
$this->nonenone = new Type(Type::NONE, Type::NONE); |
62
|
|
|
|
63
|
|
|
$this->server11 = new Type($this->sid1, $this->sop1); |
64
|
|
|
$this->server12 = new Type($this->sid1, $this->sop2); |
65
|
|
|
$this->server_1 = new Type(Type::ANY, $this->sop1); |
66
|
|
|
$this->server1_ = new Type($this->sid1, Type::ANY); |
67
|
|
|
$this->serverN1 = new Type(Type::NONE, $this->sop1); |
68
|
|
|
$this->server1N = new Type($this->sid1, Type::NONE); |
69
|
|
|
$this->serverN_ = new Type(Type::NONE, Type::ANY); |
70
|
|
|
$this->server_N = new Type(Type::ANY, Type::NONE); |
71
|
|
|
$this->server22 = new Type($this->sid2, $this->sop2); |
72
|
|
|
$this->server21 = new Type($this->sid2, $this->sop1); |
73
|
|
|
$this->server_2 = new Type(Type::ANY, $this->sop2); |
74
|
|
|
$this->server2_ = new Type($this->sid2, Type::ANY); |
75
|
|
|
$this->serverN2 = new Type(Type::NONE, $this->sop2); |
76
|
|
|
$this->server2N = new Type($this->sid2, Type::NONE); |
77
|
|
|
|
78
|
|
|
$this->domain11 = new Type($this->did1, $this->dop1); |
79
|
|
|
$this->domain_1 = new Type(Type::ANY, $this->dop1); |
80
|
|
|
$this->domain1_ = new Type($this->did1, Type::ANY); |
81
|
|
|
$this->domainN1 = new Type(Type::NONE, $this->dop1); |
82
|
|
|
$this->domain1N = new Type($this->did1, Type::NONE); |
83
|
|
|
$this->domain22 = new Type($this->did2, $this->dop2); |
84
|
|
|
$this->domain_2 = new Type(Type::ANY, $this->dop2); |
85
|
|
|
$this->domain2_ = new Type($this->did2, Type::ANY); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function copy(TypeInterface $type): TypeInterface |
89
|
|
|
{ |
90
|
|
|
return new Type($type->getId(), $type->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetUniqueId(): void |
94
|
|
|
{ |
95
|
|
|
$this->assertSame($this->sop1, $this->server11->getUniqueId()); |
96
|
|
|
$this->assertSame($this->sop1, $this->server_1->getUniqueId()); |
97
|
|
|
$this->assertSame($this->sid1, $this->server1_->getUniqueId()); |
98
|
|
|
$this->assertSame($this->sop2, $this->server22->getUniqueId()); |
99
|
|
|
$this->assertSame($this->sop2, $this->server_2->getUniqueId()); |
100
|
|
|
$this->assertSame($this->sid2, $this->server2_->getUniqueId()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testEquals(): void |
104
|
|
|
{ |
105
|
|
|
$all = [$this->server11, $this->server_1, $this->server1_]; |
106
|
|
|
$copies = []; |
107
|
|
|
foreach ($all as $k => $v) { |
108
|
|
|
$copies[$k] = $this->copy($v); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($all as $k => $v) { |
112
|
|
|
foreach ($copies as $j => $w) { |
113
|
|
|
$this->checkEquals($j === $k, $v, $w); |
114
|
|
|
$this->checkEquals($j === $k, $w, $v); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function checkEquals(bool $expect, $lhs, $rhs): void |
120
|
|
|
{ |
121
|
|
|
$check = $lhs->equals($rhs); |
122
|
|
|
/*if ($check !== $expect) { |
123
|
|
|
var_dump('no equality', $expect, $lhs, $rhs); |
124
|
|
|
}*/ |
125
|
|
|
$this->assertSame($expect, $check); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testMatches(): void |
129
|
|
|
{ |
130
|
|
|
$this->checkMatches([$this->server11, $this->server_1]); |
131
|
|
|
$this->checkMatches([$this->server_1, $this->serverN1], false); |
132
|
|
|
$this->checkMatches([$this->server11, $this->server1_, $this->server12, $this->server1N]); |
133
|
|
|
|
134
|
|
|
$this->checkDoesntMatch([ |
135
|
|
|
$this->server1N, $this->server2N, $this->serverN_, |
136
|
|
|
$this->serverN1, $this->serverN2, $this->server_N, |
137
|
|
|
]); |
138
|
|
|
|
139
|
|
|
$this->checkDoesntMatch([ |
140
|
|
|
$this->server11, $this->server_2, $this->serverN1, |
141
|
|
|
$this->domain11, $this->domain_2, $this->domainN1, |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$this->checkDoesntMatch([ |
145
|
|
|
$this->server_1, $this->server1_, $this->server_2, $this->server2_, |
146
|
|
|
$this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_, |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function checkDoesntMatch(array $types): void |
151
|
|
|
{ |
152
|
|
|
foreach ($types as $k => $v) { |
153
|
|
|
foreach ($types as $j => $w) { |
154
|
|
|
if ($k !== $j) { |
155
|
|
|
$this->checkSingleMatch(false, $v, $w); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function checkMatches(array $types, bool $self = true): void |
162
|
|
|
{ |
163
|
|
|
$all = $types; |
164
|
|
|
if ($self) { |
165
|
|
|
foreach ($types as $type) { |
166
|
|
|
$all[] = $this->copy($type); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
foreach ($all as $k => $v) { |
170
|
|
|
foreach ($all as $j => $w) { |
171
|
|
|
if ($self || $k !== $j) { |
172
|
|
|
$this->checkSingleMatch(true, $v, $w); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function checkSingleMatch(bool $expect, $lhs, $rhs): void |
179
|
|
|
{ |
180
|
|
|
$check = $lhs->matches($rhs); |
181
|
|
|
/*if ($check !== $expect) { |
182
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
183
|
|
|
}*/ |
184
|
|
|
$this->assertSame($expect, $check); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testGetId(): void |
188
|
|
|
{ |
189
|
|
|
$type = new Type(1, 'monthly'); |
190
|
|
|
$this->assertEquals(1, $type->getId()); |
191
|
|
|
|
192
|
|
|
$type = new Type('customId', 'discount'); |
193
|
|
|
$this->assertEquals('customId', $type->getId()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetName(): void |
197
|
|
|
{ |
198
|
|
|
$type = new Type(1, 'monthly'); |
199
|
|
|
$this->assertEquals('monthly', $type->getName()); |
200
|
|
|
|
201
|
|
|
$type = new Type(2); |
202
|
|
|
$this->assertNull($type->getName()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testHasName(): void |
206
|
|
|
{ |
207
|
|
|
$type = new Type(1, 'monthly'); |
208
|
|
|
$this->assertTrue($type->hasName()); |
209
|
|
|
|
210
|
|
|
$type = new Type(2, ''); |
211
|
|
|
$this->assertFalse($type->hasName()); |
212
|
|
|
|
213
|
|
|
$type = new Type(3, TypeInterface::ANY); |
214
|
|
|
$this->assertFalse($type->hasName()); |
215
|
|
|
|
216
|
|
|
$type = new Type(4, TypeInterface::NONE); |
217
|
|
|
$this->assertFalse($type->hasName()); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testJsonSerialize(): void |
221
|
|
|
{ |
222
|
|
|
$type = new Type(1, 'monthly'); |
223
|
|
|
$expectedJson = [ |
224
|
|
|
'id' => 1, |
225
|
|
|
'name' => 'monthly', |
226
|
|
|
]; |
227
|
|
|
$this->assertEquals($expectedJson, $type->jsonSerialize()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testIsDefined(): void |
231
|
|
|
{ |
232
|
|
|
$type = new Type(1, 'monthly'); |
233
|
|
|
$this->assertTrue($type->isDefined()); |
234
|
|
|
|
235
|
|
|
$type = new Type(null, null); |
236
|
|
|
$this->assertFalse($type->isDefined()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testTypeWithAnyIdHasAnyId(): void |
240
|
|
|
{ |
241
|
|
|
$type = Type::anyId('monthly,leasing'); |
242
|
|
|
|
243
|
|
|
$this->assertSame(TypeInterface::ANY, $type->getId()); |
244
|
|
|
$this->assertSame('monthly,leasing', $type->getName()); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|