|
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\target; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\billing\target\Target; |
|
14
|
|
|
use hiqdev\php\billing\target\TargetCollection; |
|
15
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class TargetTest extends \PHPUnit\Framework\TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
protected $id1 = 1; |
|
23
|
|
|
|
|
24
|
|
|
protected $id2 = 2; |
|
25
|
|
|
|
|
26
|
|
|
protected $idA = 8; |
|
27
|
|
|
|
|
28
|
|
|
protected $idB = 9; |
|
29
|
|
|
|
|
30
|
|
|
private \hiqdev\php\billing\target\AbstractTarget $target_; |
|
31
|
|
|
|
|
32
|
|
|
private Target $target1; |
|
33
|
|
|
|
|
34
|
|
|
private Target $target2; |
|
35
|
|
|
|
|
36
|
|
|
private Target $targetA; |
|
37
|
|
|
|
|
38
|
|
|
private Target $targetB; |
|
39
|
|
|
|
|
40
|
|
|
private TargetCollection $targets; |
|
41
|
|
|
|
|
42
|
|
|
private Target $server_; |
|
43
|
|
|
|
|
44
|
|
|
private Target $server1; |
|
45
|
|
|
|
|
46
|
|
|
private Target $server2; |
|
47
|
|
|
|
|
48
|
|
|
private Target $serverA; |
|
49
|
|
|
|
|
50
|
|
|
private Target $serverB; |
|
51
|
|
|
|
|
52
|
|
|
private Target $serverN; |
|
53
|
|
|
|
|
54
|
|
|
private TargetCollection $servers; |
|
55
|
|
|
|
|
56
|
|
|
private Target $domain_; |
|
57
|
|
|
|
|
58
|
|
|
private Target $domain1; |
|
59
|
|
|
|
|
60
|
|
|
private Target $domain2; |
|
61
|
|
|
|
|
62
|
|
|
private Target $domainN; |
|
63
|
|
|
|
|
64
|
|
|
private TargetCollection $domains; |
|
65
|
|
|
|
|
66
|
|
|
protected function setUp(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->target_ = Target::any(); |
|
69
|
|
|
$this->target1 = new Target($this->id1, Target::ANY); |
|
70
|
|
|
$this->target2 = new Target($this->id2, Target::ANY); |
|
71
|
|
|
$this->targetA = new Target($this->idA, Target::ANY, 'A'); |
|
72
|
|
|
$this->targetB = new Target($this->idB, Target::ANY, 'B'); |
|
73
|
|
|
$this->targets = new TargetCollection([$this->target1, $this->target2]); |
|
74
|
|
|
$this->server_ = new Target(Target::ANY, 'server'); |
|
75
|
|
|
$this->server1 = new Target($this->id1, 'server'); |
|
76
|
|
|
$this->server2 = new Target($this->id2, 'server'); |
|
77
|
|
|
$this->serverA = new Target($this->idA, 'server', 'A'); |
|
78
|
|
|
$this->serverB = new Target($this->idB, 'server', 'B'); |
|
79
|
|
|
$this->serverN = new Target(Target::NONE, 'server'); |
|
80
|
|
|
$this->servers = new TargetCollection([$this->server1, $this->server2]); |
|
81
|
|
|
$this->domain_ = new Target(Target::ANY, 'domain'); |
|
82
|
|
|
$this->domain1 = new Target($this->id1, 'domain'); |
|
83
|
|
|
$this->domain2 = new Target($this->id2, 'domain'); |
|
84
|
|
|
$this->domainN = new Target(Target::NONE, 'domain'); |
|
85
|
|
|
$this->domains = new TargetCollection([$this->domain1, $this->domain2]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function tearDown(): void |
|
89
|
|
|
{ |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testGetFullName() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->assertSame('', $this->target_->getFullName()); |
|
95
|
|
|
$this->assertSame('', $this->target1->getFullName()); |
|
96
|
|
|
$this->assertSame('', $this->target2->getFullName()); |
|
97
|
|
|
$this->assertSame(':A', $this->targetA->getFullName()); |
|
98
|
|
|
$this->assertSame(':B', $this->targetB->getFullName()); |
|
99
|
|
|
$this->assertSame('', $this->targets->getFullName()); |
|
100
|
|
|
$this->assertSame('server:', $this->server_->getFullName()); |
|
101
|
|
|
$this->assertSame('server:', $this->server1->getFullName()); |
|
102
|
|
|
$this->assertSame('server:', $this->server2->getFullName()); |
|
103
|
|
|
$this->assertSame('server:A', $this->serverA->getFullName()); |
|
104
|
|
|
$this->assertSame('server:B', $this->serverB->getFullName()); |
|
105
|
|
|
$this->assertSame('server:', $this->servers->getFullName()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testGetUniqueId() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->assertSame(':', $this->target_->getUniqueId()); |
|
111
|
|
|
$this->assertSame(':1', $this->target1->getUniqueId()); |
|
112
|
|
|
$this->assertSame(':2', $this->target2->getUniqueId()); |
|
113
|
|
|
$this->assertSame(':8', $this->targetA->getUniqueId()); |
|
114
|
|
|
$this->assertSame(':9', $this->targetB->getUniqueId()); |
|
115
|
|
|
$this->assertSame(':1', $this->targets->getUniqueId()); |
|
116
|
|
|
$this->assertSame('server:', $this->server_->getUniqueId()); |
|
117
|
|
|
$this->assertSame('server:1', $this->server1->getUniqueId()); |
|
118
|
|
|
$this->assertSame('server:2', $this->server2->getUniqueId()); |
|
119
|
|
|
$this->assertSame('server:8', $this->serverA->getUniqueId()); |
|
120
|
|
|
$this->assertSame('server:9', $this->serverB->getUniqueId()); |
|
121
|
|
|
$this->assertSame('server:1', $this->servers->getUniqueId()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testEquals() |
|
125
|
|
|
{ |
|
126
|
|
|
$all = [ |
|
127
|
|
|
$this->target_, $this->target1, $this->target2, |
|
128
|
|
|
$this->server_, $this->server1, $this->server2, |
|
129
|
|
|
$this->domain_, $this->domain1, $this->domain2, |
|
130
|
|
|
]; |
|
131
|
|
|
$copies = []; |
|
132
|
|
|
foreach ($all as $k => $v) { |
|
133
|
|
|
$copies[$k] = $this->copy($v); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
foreach ($all as $k => $v) { |
|
137
|
|
|
foreach ($copies as $j => $w) { |
|
138
|
|
|
$this->assertSame($j === $k, $v->equals($w)); |
|
139
|
|
|
$this->assertSame($j === $k, $w->equals($v)); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function copy(TargetInterface $target) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($target instanceof TargetCollection) { |
|
147
|
|
|
return new TargetCollection($target->getTargets()); |
|
148
|
|
|
} else { |
|
149
|
|
|
return new Target($target->getId(), $target->getType()); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testMatches() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->checkMatches([ |
|
156
|
|
|
$this->target_, $this->server_, $this->serverN, |
|
157
|
|
|
], false); |
|
158
|
|
|
|
|
159
|
|
|
$this->checkMatches([ |
|
160
|
|
|
$this->target_, $this->server_, $this->server1, $this->servers, |
|
161
|
|
|
]); |
|
162
|
|
|
$this->checkMatches([ |
|
163
|
|
|
$this->target_, $this->server_, $this->server2, $this->servers, |
|
164
|
|
|
]); |
|
165
|
|
|
|
|
166
|
|
|
$this->checkDoesntMatch([ |
|
167
|
|
|
$this->server1, $this->server2, $this->serverN, $this->domain_, |
|
168
|
|
|
]); |
|
169
|
|
|
$this->checkDoesntMatch([ |
|
170
|
|
|
$this->domain1, $this->domain2, $this->domainN, $this->servers, |
|
171
|
|
|
]); |
|
172
|
|
|
|
|
173
|
|
|
$this->checkDoesntMatch([ |
|
174
|
|
|
$this->server_, $this->domains, $this->domainN, $this->domainN, |
|
175
|
|
|
]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function checkMatches(array $targets, $self = true) |
|
179
|
|
|
{ |
|
180
|
|
|
$all = $targets; |
|
181
|
|
|
if ($self) { |
|
182
|
|
|
foreach ($targets as $target) { |
|
183
|
|
|
$all[] = $this->copy($target); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
foreach ($all as $k => $v) { |
|
187
|
|
|
foreach ($all as $j => $w) { |
|
188
|
|
|
if ($self || $k !== $j) { |
|
189
|
|
|
$this->checkSingleMatch(true, $v, $w); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
protected function checkDoesntMatch(array $targets) |
|
196
|
|
|
{ |
|
197
|
|
|
foreach ($targets as $k => $v) { |
|
198
|
|
|
foreach ($targets as $j => $w) { |
|
199
|
|
|
if ($k !== $j) { |
|
200
|
|
|
$this->checkSingleMatch(false, $v, $w); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
protected function checkSingleMatch(bool $expect, $lhs, $rhs) |
|
207
|
|
|
{ |
|
208
|
|
|
$check = $lhs->matches($rhs); |
|
209
|
|
|
if ($check !== $expect) { |
|
210
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
|
211
|
|
|
} |
|
212
|
|
|
$this->assertSame($expect, $check); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|