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