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-2018, 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
|
|
|
protected $id2 = 2; |
24
|
|
|
protected $idA = 8; |
25
|
|
|
protected $idB = 9; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->target_ = new Target(Target::ANY, Target::ANY); |
|
|
|
|
30
|
|
|
$this->target1 = new Target($this->id1, Target::ANY); |
|
|
|
|
31
|
|
|
$this->target2 = new Target($this->id2, Target::ANY); |
|
|
|
|
32
|
|
|
$this->targetA = new Target($this->idA, Target::ANY, 'A'); |
|
|
|
|
33
|
|
|
$this->targetB = new Target($this->idB, Target::ANY, 'B'); |
|
|
|
|
34
|
|
|
$this->server_ = new Target(Target::ANY, 'server'); |
|
|
|
|
35
|
|
|
$this->server1 = new Target($this->id1, 'server'); |
|
|
|
|
36
|
|
|
$this->server2 = new Target($this->id2, 'server'); |
|
|
|
|
37
|
|
|
$this->serverA = new Target($this->idA, 'server', 'A'); |
|
|
|
|
38
|
|
|
$this->serverB = new Target($this->idB, 'server', 'B'); |
|
|
|
|
39
|
|
|
$this->serverN = new Target(Target::NONE, 'server'); |
|
|
|
|
40
|
|
|
$this->servers = new TargetCollection([$this->server1, $this->server2]); |
|
|
|
|
41
|
|
|
$this->domain_ = new Target(Target::ANY, 'domain'); |
|
|
|
|
42
|
|
|
$this->domain1 = new Target($this->id1, 'domain'); |
|
|
|
|
43
|
|
|
$this->domain2 = new Target($this->id2, 'domain'); |
|
|
|
|
44
|
|
|
$this->domainN = new Target(Target::NONE, 'domain'); |
|
|
|
|
45
|
|
|
$this->domains = new TargetCollection([$this->domain1, $this->domain2]); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function tearDown() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetFullName() |
53
|
|
|
{ |
54
|
|
|
$this->assertSame('', $this->target_->getFullName()); |
55
|
|
|
$this->assertSame('', $this->target1->getFullName()); |
56
|
|
|
$this->assertSame('', $this->target2->getFullName()); |
57
|
|
|
$this->assertSame(':A', $this->targetA->getFullName()); |
58
|
|
|
$this->assertSame(':B', $this->targetB->getFullName()); |
59
|
|
|
$this->assertSame('server:', $this->server_->getFullName()); |
60
|
|
|
$this->assertSame('server:', $this->server1->getFullName()); |
61
|
|
|
$this->assertSame('server:', $this->server2->getFullName()); |
62
|
|
|
$this->assertSame('server:A', $this->serverA->getFullName()); |
63
|
|
|
$this->assertSame('server:B', $this->serverB->getFullName()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetUniqueId() |
67
|
|
|
{ |
68
|
|
|
$this->assertSame(':', $this->target_->getUniqueId()); |
69
|
|
|
$this->assertSame(':1', $this->target1->getUniqueId()); |
70
|
|
|
$this->assertSame(':2', $this->target2->getUniqueId()); |
71
|
|
|
$this->assertSame(':8', $this->targetA->getUniqueId()); |
72
|
|
|
$this->assertSame(':9', $this->targetB->getUniqueId()); |
73
|
|
|
$this->assertSame('server:', $this->server_->getUniqueId()); |
74
|
|
|
$this->assertSame('server:1', $this->server1->getUniqueId()); |
75
|
|
|
$this->assertSame('server:2', $this->server2->getUniqueId()); |
76
|
|
|
$this->assertSame('server:8', $this->serverA->getUniqueId()); |
77
|
|
|
$this->assertSame('server:9', $this->serverB->getUniqueId()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testEquals() |
81
|
|
|
{ |
82
|
|
|
$all = [ |
83
|
|
|
$this->target_, $this->target1, $this->target2, |
84
|
|
|
$this->server_, $this->server1, $this->server2, |
85
|
|
|
$this->domain_, $this->domain1, $this->domain2, |
86
|
|
|
]; |
87
|
|
|
$copies = []; |
88
|
|
|
foreach ($all as $k => $v) { |
89
|
|
|
$copies[$k] = $this->copy($v); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($all as $k => $v) { |
93
|
|
|
foreach ($copies as $j => $w) { |
94
|
|
|
$this->assertSame($j === $k, $v->equals($w)); |
95
|
|
|
$this->assertSame($j === $k, $w->equals($v)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function copy(TargetInterface $target) |
101
|
|
|
{ |
102
|
|
|
if ($target instanceof TargetCollection) { |
103
|
|
|
return new TargetCollection($target->getTargets()); |
104
|
|
|
} else { |
105
|
|
|
return new Target($target->getId(), $target->getType()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testMatches() |
110
|
|
|
{ |
111
|
|
|
$this->checkMatches([ |
112
|
|
|
$this->target_, $this->server_, $this->serverN, |
113
|
|
|
], false); |
114
|
|
|
|
115
|
|
|
$this->checkMatches([ |
116
|
|
|
$this->target_, $this->server_, $this->server1, $this->servers, |
117
|
|
|
]); |
118
|
|
|
$this->checkMatches([ |
119
|
|
|
$this->target_, $this->server_, $this->server2, $this->servers, |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->checkDoesntMatch([ |
123
|
|
|
$this->server1, $this->server2, $this->serverN, $this->domain_, |
124
|
|
|
]); |
125
|
|
|
$this->checkDoesntMatch([ |
126
|
|
|
$this->domain1, $this->domain2, $this->domainN, $this->servers, |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$this->checkDoesntMatch([ |
130
|
|
|
$this->server_, $this->domains, $this->domainN, $this->domainN, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function checkMatches(array $targets, $self = true) |
135
|
|
|
{ |
136
|
|
|
$all = $targets; |
137
|
|
|
if ($self) { |
138
|
|
|
foreach ($targets as $target) { |
139
|
|
|
$all[] = $this->copy($target); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
foreach ($all as $k => $v) { |
143
|
|
|
foreach ($all as $j => $w) { |
144
|
|
|
if ($self || $k !== $j) { |
145
|
|
|
$this->checkSingleMatch(true, $v, $w); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function checkDoesntMatch(array $targets) |
152
|
|
|
{ |
153
|
|
|
foreach ($targets as $k => $v) { |
154
|
|
|
foreach ($targets as $j => $w) { |
155
|
|
|
if ($k !== $j) { |
156
|
|
|
$this->checkSingleMatch(false, $v, $w); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function checkSingleMatch(bool $expect, $lhs, $rhs) |
163
|
|
|
{ |
164
|
|
|
$check = $lhs->matches($rhs); |
165
|
|
|
if ($check !== $expect) { |
166
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
$this->assertSame($expect, $check); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|