Completed
Push — master ( 77c848...791681 )
by Andrii
04:18
created

TargetTest::copyTarget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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->atarget = new Target(null,       null);
29
        $this->target1 = new Target($this->id1, null);
30
        $this->target2 = new Target($this->id2, null);
31
        $this->aserver = new Target(null,       'server');
32
        $this->server1 = new Target($this->id1, 'server');
33
        $this->server2 = new Target($this->id2, 'server');
34
        $this->servers = new TargetCollection([$this->server1, $this->server2]);
35
        $this->adomain = new Target(null,       'domain');
36
        $this->domain1 = new Target($this->id1, 'domain');
37
        $this->domain2 = new Target($this->id2, 'domain');
38
        $this->domains = new TargetCollection([$this->domain1, $this->domain2]);
39
    }
40
41
    protected function tearDown()
42
    {
43
    }
44
45
    public function testGetUniqueId()
46
    {
47
        $this->assertSame(':',          $this->atarget->getUniqueId());
48
        $this->assertSame(':1',         $this->target1->getUniqueId());
49
        $this->assertSame(':2',         $this->target2->getUniqueId());
50
        $this->assertSame('server:',    $this->aserver->getUniqueId());
51
        $this->assertSame('server:1',   $this->server1->getUniqueId());
52
        $this->assertSame('server:2',   $this->server2->getUniqueId());
53
    }
54
55
    public function testEquals()
56
    {
57
        $all = [
58
            $this->atarget, $this->target1, $this->target2,
59
            $this->aserver, $this->server1, $this->server2,
60
            $this->adomain, $this->domain1, $this->domain2,
61
        ];
62
        $copies = [];
63
        foreach ($all as $k => $v) {
64
            $copies[$k] = $this->copyTarget($v);
65
        }
66
67
        foreach ($all as $k => $v) {
68
            foreach ($copies as $j => $w) {
69
                $this->assertSame($j === $k, $v->equals($w));
70
                $this->assertSame($j === $k, $w->equals($v));
71
            }
72
        }
73
    }
74
75
    protected function copyTarget(TargetInterface $target)
76
    {
77
        if ($target instanceof TargetCollection) {
78
            return new TargetCollection($target->getTargets());
79
        } else {
80
            return new Target($target->getId(), $target->getType());
81
        }
82
    }
83
84
    public function testMatches()
85
    {
86
        $atarget = new Target(null,       null);
87
        $aserver = new Target(null,       'server');
88
        $server1 = new Target($this->id1, 'server');
89
        $server2 = new Target($this->id2, 'server');
90
        $servers = new TargetCollection([$this->server1, $this->server2]);
91
        $adomain = new Target(null,       'domain');
0 ignored issues
show
Unused Code introduced by
$adomain is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        $domain1 = new Target($this->id1, 'domain');
0 ignored issues
show
Unused Code introduced by
$domain1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        $domain2 = new Target($this->id2, 'domain');
0 ignored issues
show
Unused Code introduced by
$domain2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
        $domains = new TargetCollection([$this->domain1, $this->domain2]);
0 ignored issues
show
Unused Code introduced by
$domains is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
96
        $this->checkMatches([
97
            $this->atarget, $atarget,
98
            $this->aserver, $aserver,
99
            $this->server1, $server1,
100
            $this->servers, $servers,
101
        ]);
102
        $this->checkMatches([
103
            $this->atarget, $atarget,
104
            $this->aserver, $aserver,
105
            $this->server2, $server2,
106
            $this->servers, $servers,
107
        ]);
108
109
        $this->checkDoesntMatch([
110
            $this->server1, $this->server2, $this->adomain,
111
        ]);
112
113
        $this->checkDoesntMatch([
114
            $this->domain1, $this->domain2, $this->servers,
115
        ]);
116
    }
117
118
    protected function checkDoesntMatch(array $targets)
119
    {
120
        $this->checkMatches($targets, false);
121
    }
122
123
    protected function checkMatches(array $targets, bool $expect = true)
124
    {
125
        foreach ($targets as $k => $v) {
126
            foreach ($targets as $j => $w) {
127
                $this->checkSingleMatch($k === $j || $expect, $v, $w);
128
            }
129
        }
130
    }
131
132
    protected function checkSingleMatch(bool $expect, $lhs, $rhs)
133
    {
134
        $check = $lhs->matches($rhs);
135
        if ($check !== $expect) {
136
            var_dump('no match', $expect, $lhs, $rhs);
0 ignored issues
show
Security Debugging Code introduced by
var_dump('no match', $expect, $lhs, $rhs); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
137
        }
138
        $this->assertSame($expect, $check);
139
    }
140
}
141