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
|
|
|
protected function setUp(): void |
34
|
|
|
{ |
35
|
|
|
$this->nonenone = new Type(Type::NONE, Type::NONE); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->server11 = new Type($this->sid1, $this->sop1); |
|
|
|
|
38
|
|
|
$this->server12 = new Type($this->sid1, $this->sop2); |
|
|
|
|
39
|
|
|
$this->server_1 = new Type(Type::ANY, $this->sop1); |
|
|
|
|
40
|
|
|
$this->server1_ = new Type($this->sid1, Type::ANY); |
|
|
|
|
41
|
|
|
$this->serverN1 = new Type(Type::NONE, $this->sop1); |
|
|
|
|
42
|
|
|
$this->server1N = new Type($this->sid1, Type::NONE); |
|
|
|
|
43
|
|
|
$this->serverN_ = new Type(Type::NONE, Type::ANY); |
|
|
|
|
44
|
|
|
$this->server_N = new Type(Type::ANY, Type::NONE); |
|
|
|
|
45
|
|
|
$this->server22 = new Type($this->sid2, $this->sop2); |
|
|
|
|
46
|
|
|
$this->server21 = new Type($this->sid2, $this->sop1); |
|
|
|
|
47
|
|
|
$this->server_2 = new Type(Type::ANY, $this->sop2); |
|
|
|
|
48
|
|
|
$this->server2_ = new Type($this->sid2, Type::ANY); |
|
|
|
|
49
|
|
|
$this->serverN2 = new Type(Type::NONE, $this->sop2); |
|
|
|
|
50
|
|
|
$this->server2N = new Type($this->sid2, Type::NONE); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->domain11 = new Type($this->did1, $this->dop1); |
|
|
|
|
53
|
|
|
$this->domain_1 = new Type(Type::ANY, $this->dop1); |
|
|
|
|
54
|
|
|
$this->domain1_ = new Type($this->did1, Type::ANY); |
|
|
|
|
55
|
|
|
$this->domainN1 = new Type(Type::NONE, $this->dop1); |
|
|
|
|
56
|
|
|
$this->domain1N = new Type($this->did1, Type::NONE); |
|
|
|
|
57
|
|
|
$this->domain22 = new Type($this->did2, $this->dop2); |
|
|
|
|
58
|
|
|
$this->domain_2 = new Type(Type::ANY, $this->dop2); |
|
|
|
|
59
|
|
|
$this->domain2_ = new Type($this->did2, Type::ANY); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function tearDown(): void |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function copy(TypeInterface $type) |
67
|
|
|
{ |
68
|
|
|
return new Type($type->getId(), $type->getName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetUniqueId() |
72
|
|
|
{ |
73
|
|
|
$this->assertSame($this->sop1, $this->server11->getUniqueId()); |
74
|
|
|
$this->assertSame($this->sop1, $this->server_1->getUniqueId()); |
75
|
|
|
$this->assertSame($this->sid1, $this->server1_->getUniqueId()); |
76
|
|
|
$this->assertSame($this->sop2, $this->server22->getUniqueId()); |
77
|
|
|
$this->assertSame($this->sop2, $this->server_2->getUniqueId()); |
78
|
|
|
$this->assertSame($this->sid2, $this->server2_->getUniqueId()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testEquals() |
82
|
|
|
{ |
83
|
|
|
$all = [$this->server11, $this->server_1, $this->server1_]; |
84
|
|
|
$copies = []; |
85
|
|
|
foreach ($all as $k => $v) { |
86
|
|
|
$copies[$k] = $this->copy($v); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($all as $k => $v) { |
90
|
|
|
foreach ($copies as $j => $w) { |
91
|
|
|
$this->checkEquals($j === $k, $v, $w); |
92
|
|
|
$this->checkEquals($j === $k, $w, $v); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function checkEquals(bool $expect, $lhs, $rhs) |
98
|
|
|
{ |
99
|
|
|
$check = $lhs->equals($rhs); |
100
|
|
|
/*if ($check !== $expect) { |
101
|
|
|
var_dump('no equality', $expect, $lhs, $rhs); |
102
|
|
|
}*/ |
103
|
|
|
$this->assertSame($expect, $check); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testMatches() |
107
|
|
|
{ |
108
|
|
|
$this->checkMatches([$this->server11, $this->server_1]); |
109
|
|
|
$this->checkMatches([$this->server_1, $this->serverN1], false); |
110
|
|
|
$this->checkMatches([$this->server11, $this->server1_, $this->server12, $this->server1N]); |
111
|
|
|
|
112
|
|
|
$this->checkDoesntMatch([ |
113
|
|
|
$this->server1N, $this->server2N, $this->serverN_, |
114
|
|
|
$this->serverN1, $this->serverN2, $this->server_N, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$this->checkDoesntMatch([ |
118
|
|
|
$this->server11, $this->server_2, $this->serverN1, |
119
|
|
|
$this->domain11, $this->domain_2, $this->domainN1, |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->checkDoesntMatch([ |
123
|
|
|
$this->server_1, $this->server1_, $this->server_2, $this->server2_, |
124
|
|
|
$this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function checkDoesntMatch(array $types) |
129
|
|
|
{ |
130
|
|
|
foreach ($types as $k => $v) { |
131
|
|
|
foreach ($types as $j => $w) { |
132
|
|
|
if ($k !== $j) { |
133
|
|
|
$this->checkSingleMatch(false, $v, $w); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function checkMatches(array $types, bool $self = true) |
140
|
|
|
{ |
141
|
|
|
$all = $types; |
142
|
|
|
if ($self) { |
143
|
|
|
foreach ($types as $type) { |
144
|
|
|
$all[] = $this->copy($type); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
foreach ($all as $k => $v) { |
148
|
|
|
foreach ($all as $j => $w) { |
149
|
|
|
if ($self || $k !== $j) { |
150
|
|
|
$this->checkSingleMatch(true, $v, $w); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function checkSingleMatch(bool $expect, $lhs, $rhs) |
157
|
|
|
{ |
158
|
|
|
$check = $lhs->matches($rhs); |
159
|
|
|
/*if ($check !== $expect) { |
160
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
161
|
|
|
}*/ |
162
|
|
|
$this->assertSame($expect, $check); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|