|
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, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\tests\unit\target; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\billing\target\TargetCollection; |
|
14
|
|
|
use hiqdev\php\billing\target\Target; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class TargetTest extends \PHPUnit\Framework\TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
protected $id1 = 1; |
|
22
|
|
|
|
|
23
|
|
|
protected $id2 = 2; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->atarget = new Target(null, null); |
|
28
|
|
|
$this->target1 = new Target($this->id1, null); |
|
29
|
|
|
$this->target2 = new Target($this->id2, null); |
|
30
|
|
|
$this->aserver = new Target(null, 'server'); |
|
31
|
|
|
$this->server1 = new Target($this->id1, 'server'); |
|
32
|
|
|
$this->server2 = new Target($this->id2, 'server'); |
|
33
|
|
|
$this->servers = new TargetCollection([$this->server1, $this->server2]); |
|
34
|
|
|
$this->adomain = new Target(null, 'domain'); |
|
35
|
|
|
$this->domain1 = new Target($this->id1, 'domain'); |
|
36
|
|
|
$this->domain2 = new Target($this->id2, 'domain'); |
|
37
|
|
|
$this->domains = new TargetCollection([$this->domain1, $this->domain2]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function tearDown() |
|
41
|
|
|
{ |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testGetUniqueId() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertSame(':', $this->atarget->getUniqueId()); |
|
47
|
|
|
$this->assertSame(':1', $this->target1->getUniqueId()); |
|
48
|
|
|
$this->assertSame(':2', $this->target2->getUniqueId()); |
|
49
|
|
|
$this->assertSame('server:', $this->aserver->getUniqueId()); |
|
50
|
|
|
$this->assertSame('server:1', $this->server1->getUniqueId()); |
|
51
|
|
|
$this->assertSame('server:2', $this->server2->getUniqueId()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testMatches() |
|
55
|
|
|
{ |
|
56
|
|
|
$atarget = new Target(null, null); |
|
57
|
|
|
$aserver = new Target(null, 'server'); |
|
58
|
|
|
$server1 = new Target($this->id1, 'server'); |
|
59
|
|
|
$server2 = new Target($this->id2, 'server'); |
|
60
|
|
|
$servers = new TargetCollection([$this->server1, $this->server2]); |
|
61
|
|
|
$adomain = new Target(null, 'domain'); |
|
|
|
|
|
|
62
|
|
|
$domain1 = new Target($this->id1, 'domain'); |
|
|
|
|
|
|
63
|
|
|
$domain2 = new Target($this->id2, 'domain'); |
|
|
|
|
|
|
64
|
|
|
$domains = new TargetCollection([$this->domain1, $this->domain2]); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->checkMatches([ |
|
67
|
|
|
$this->atarget, $atarget, |
|
68
|
|
|
$this->aserver, $aserver, |
|
69
|
|
|
$this->server1, $server1, |
|
70
|
|
|
$this->servers, $servers, |
|
71
|
|
|
]); |
|
72
|
|
|
$this->checkMatches([ |
|
73
|
|
|
$this->atarget, $atarget, |
|
74
|
|
|
$this->aserver, $aserver, |
|
75
|
|
|
$this->server2, $server2, |
|
76
|
|
|
$this->servers, $servers, |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$this->checkDoesntMatch([ |
|
80
|
|
|
$this->server1, $this->server2, $this->adomain, |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
$this->checkDoesntMatch([ |
|
84
|
|
|
$this->domain1, $this->domain2, $this->servers, |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function checkDoesntMatch(array $targets) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->checkMatches($targets, false); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function checkMatches(array $targets, bool $expect = true) |
|
94
|
|
|
{ |
|
95
|
|
|
foreach ($targets as $k => $v) { |
|
96
|
|
|
foreach ($targets as $j => $w) { |
|
97
|
|
|
if ($k === $j) { |
|
98
|
|
|
$this->assertTrue($v->matches($w)); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->checkSingleMatch($expect, $v, $w); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function checkSingleMatch(bool $expect, $lhs, $rhs) |
|
107
|
|
|
{ |
|
108
|
|
|
$check = $lhs->matches($rhs); |
|
109
|
|
|
if ($check !== $expect) { |
|
110
|
|
|
var_dump('no match', $expect, $lhs, $rhs); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
$this->assertSame($expect, $check); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.