Completed
Push — master ( fd1dbe...d544ae )
by Andrii
03:02
created

TargetTest::checkMatches()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 9
nc 5
nop 2
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->target_ = new Target(Target::ANY,    Target::ANY);
29
        $this->target1 = new Target($this->id1,     Target::ANY);
30
        $this->target2 = new Target($this->id2,     Target::ANY);
31
        $this->server_ = new Target(Target::ANY,    'server');
32
        $this->server1 = new Target($this->id1,     'server');
33
        $this->server2 = new Target($this->id2,     'server');
34
        $this->serverN = new Target(Target::NONE,   'server');
35
        $this->servers = new TargetCollection([$this->server1, $this->server2]);
36
        $this->domain_ = new Target(Target::ANY,    'domain');
37
        $this->domain1 = new Target($this->id1,     'domain');
38
        $this->domain2 = new Target($this->id2,     'domain');
39
        $this->domainN = new Target(Target::NONE,   'domain');
40
        $this->domains = new TargetCollection([$this->domain1, $this->domain2]);
41
    }
42
43
    protected function tearDown()
44
    {
45
    }
46
47
    public function testGetUniqueId()
48
    {
49
        $this->assertSame(':',          $this->target_->getUniqueId());
50
        $this->assertSame(':1',         $this->target1->getUniqueId());
51
        $this->assertSame(':2',         $this->target2->getUniqueId());
52
        $this->assertSame('server:',    $this->server_->getUniqueId());
53
        $this->assertSame('server:1',   $this->server1->getUniqueId());
54
        $this->assertSame('server:2',   $this->server2->getUniqueId());
55
    }
56
57
    public function testEquals()
58
    {
59
        $all = [
60
            $this->target_, $this->target1, $this->target2,
61
            $this->server_, $this->server1, $this->server2,
62
            $this->domain_, $this->domain1, $this->domain2,
63
        ];
64
        $copies = [];
65
        foreach ($all as $k => $v) {
66
            $copies[$k] = $this->copy($v);
67
        }
68
69 View Code Duplication
        foreach ($all as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            foreach ($copies as $j => $w) {
71
                $this->assertSame($j === $k, $v->equals($w));
72
                $this->assertSame($j === $k, $w->equals($v));
73
            }
74
        }
75
    }
76
77
    protected function copy(TargetInterface $target)
78
    {
79
        if ($target instanceof TargetCollection) {
80
            return new TargetCollection($target->getTargets());
81
        } else {
82
            return new Target($target->getId(), $target->getType());
83
        }
84
    }
85
86
    public function testMatches()
87
    {
88
        $this->checkMatches([
89
            $this->target_, $this->server_, $this->serverN,
90
        ], false);
91
92
        $this->checkMatches([
93
            $this->target_, $this->server_, $this->server1, $this->servers,
94
        ]);
95
        $this->checkMatches([
96
            $this->target_, $this->server_, $this->server2, $this->servers,
97
        ]);
98
99
        $this->checkDoesntMatch([
100
            $this->server1, $this->server2, $this->serverN, $this->domain_,
101
        ]);
102
        $this->checkDoesntMatch([
103
            $this->domain1, $this->domain2, $this->domainN, $this->servers,
104
        ]);
105
106
        $this->checkDoesntMatch([
107
            $this->server_, $this->domains, $this->domainN, $this->domainN,
108
        ]);
109
    }
110
111 View Code Duplication
    protected function checkMatches(array $targets, $self = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        $all = $targets;
114
        if ($self) {
115
            foreach ($targets as $target) {
116
                $all[] = $this->copy($target);
117
            }
118
        }
119
        foreach ($all as $k => $v) {
120
            foreach ($all as $j => $w) {
121
                if ($self || $k !== $j) {
122
                    $this->checkSingleMatch(true, $v, $w);
123
                }
124
            }
125
        }
126
    }
127
128 View Code Duplication
    protected function checkDoesntMatch(array $targets)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        foreach ($targets as $k => $v) {
131
            foreach ($targets as $j => $w) {
132
                if ($k !== $j) {
133
                    $this->checkSingleMatch(false, $v, $w);
134
                }
135
            }
136
        }
137
    }
138
139 View Code Duplication
    protected function checkSingleMatch(bool $expect, $lhs, $rhs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $check = $lhs->matches($rhs);
142
        if ($check !== $expect) {
143
            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...
144
        }
145
        $this->assertSame($expect, $check);
146
    }
147
}
148