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\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() |
34
|
|
|
{ |
35
|
|
|
$this->server11 = new Type($this->sid1, $this->sop1); |
36
|
|
|
$this->server_1 = new Type(null, $this->sop1); |
37
|
|
|
$this->server1_ = new Type($this->sid1, null); |
38
|
|
|
$this->server22 = new Type($this->sid2, $this->sop2); |
39
|
|
|
$this->server_2 = new Type(null, $this->sop2); |
40
|
|
|
$this->server2_ = new Type($this->sid2, null); |
41
|
|
|
|
42
|
|
|
$this->domain11 = new Type($this->did1, $this->dop1); |
43
|
|
|
$this->domain_1 = new Type(null, $this->dop1); |
44
|
|
|
$this->domain1_ = new Type($this->did1, null); |
45
|
|
|
$this->domain22 = new Type($this->did2, $this->dop2); |
46
|
|
|
$this->domain_2 = new Type(null, $this->dop2); |
47
|
|
|
$this->domain2_ = new Type($this->did2, null); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function tearDown() |
51
|
|
|
{ |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function copy(TypeInterface $type) |
55
|
|
|
{ |
56
|
|
|
return new Type($type->getId(), $type->getName()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetUniqueId() |
60
|
|
|
{ |
61
|
|
|
$this->assertSame($this->sid1, $this->server11->getUniqueId()); |
62
|
|
|
$this->assertSame($this->sop1, $this->server_1->getUniqueId()); |
63
|
|
|
$this->assertSame($this->sid1, $this->server1_->getUniqueId()); |
64
|
|
|
$this->assertSame($this->sid2, $this->server22->getUniqueId()); |
65
|
|
|
$this->assertSame($this->sop2, $this->server_2->getUniqueId()); |
66
|
|
|
$this->assertSame($this->sid2, $this->server2_->getUniqueId()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testEquals() |
70
|
|
|
{ |
71
|
|
|
$all = [$this->server11, $this->server_1, $this->server1_]; |
72
|
|
|
$copies = []; |
73
|
|
|
foreach ($all as $k => $v) { |
74
|
|
|
$copies[$k] = $this->copy($v); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
foreach ($all as $k => $v) { |
|
|
|
|
78
|
|
|
foreach ($copies as $j => $w) { |
79
|
|
|
$this->checkEquals($j === $k, $v, $w); |
80
|
|
|
$this->checkEquals($j === $k, $w, $v); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
protected function checkEquals(bool $expect, $lhs, $rhs) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$check = $lhs->equals($rhs); |
88
|
|
|
if ($check !== $expect) { |
89
|
|
|
var_dump('no equality', $expect, $lhs, $rhs); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
$this->assertSame($expect, $check); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testMatches() |
95
|
|
|
{ |
96
|
|
|
$this->checkMatches([ |
97
|
|
|
$this->server11, $this->copy($this->server11), |
98
|
|
|
$this->server_1, $this->copy($this->server_1), |
99
|
|
|
]); |
100
|
|
|
$this->checkMatches([ |
101
|
|
|
$this->server11, $this->copy($this->server11), |
102
|
|
|
$this->server1_, $this->copy($this->server1_), |
103
|
|
|
]); |
104
|
|
|
$this->checkDoesntMatch([ |
105
|
|
|
$this->server_1, $this->server1_, $this->server_2, $this->server2_, |
106
|
|
|
$this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function checkDoesntMatch(array $types) |
111
|
|
|
{ |
112
|
|
|
$this->checkMatches($types, false); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
protected function checkMatches(array $types, bool $expect = true) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
foreach ($types as $k => $v) { |
118
|
|
|
foreach ($types as $j => $w) { |
119
|
|
|
$this->checkSingleMatch($k === $j || $expect, $v, $w); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
protected function checkSingleMatch(bool $expect, $lhs, $rhs) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$check = $lhs->matches($rhs); |
127
|
|
|
if ($check !== $expect) { |
128
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
$this->assertSame($expect, $check); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|