Completed
Push — master ( 791681...fd1dbe )
by Andrii
02:06
created

TargetTest::copy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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->copy($v);
65
        }
66
67 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...
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 copy(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
        $this->checkMatches([
87
            $this->atarget, $this->copy($this->atarget),
88
            $this->aserver, $this->copy($this->aserver),
89
            $this->server1, $this->copy($this->server1),
90
            $this->servers, $this->copy($this->servers),
91
        ]);
92
        $this->checkMatches([
93
            $this->atarget, $this->copy($this->atarget),
94
            $this->aserver, $this->copy($this->aserver),
95
            $this->server2, $this->copy($this->server2),
96
            $this->servers, $this->copy($this->servers),
97
        ]);
98
99
        $this->checkDoesntMatch([
100
            $this->server1, $this->server2, $this->adomain,
101
        ]);
102
103
        $this->checkDoesntMatch([
104
            $this->domain1, $this->domain2, $this->servers,
105
        ]);
106
    }
107
108
    protected function checkDoesntMatch(array $targets)
109
    {
110
        $this->checkMatches($targets, false);
111
    }
112
113 View Code Duplication
    protected function checkMatches(array $targets, bool $expect = 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...
114
    {
115
        foreach ($targets as $k => $v) {
116
            foreach ($targets as $j => $w) {
117
                $this->checkSingleMatch($k === $j || $expect, $v, $w);
118
            }
119
        }
120
    }
121
122 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...
123
    {
124
        $check = $lhs->matches($rhs);
125
        if ($check !== $expect) {
126
            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...
127
        }
128
        $this->assertSame($expect, $check);
129
    }
130
}
131