Passed
Pull Request — master (#78)
by
unknown
14:55
created

TargetTest::checkMatches()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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