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

TypeTest::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\type;
12
13
use hiqdev\php\billing\type\Type;
14
use hiqdev\php\billing\type\TypeInterface;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class TypeTest extends \PHPUnit\Framework\TestCase
20
{
21
    protected $sid1 = 101;
22
    protected $sid2 = 102;
23
24
    protected $sop1 = 'server1';
25
    protected $sop2 = 'server2';
26
27
    protected $did1 = 201;
28
    protected $did2 = 202;
29
30
    protected $dop1 = 'domain1';
31
    protected $dop2 = 'domain2';
32
33
    protected function setUp()
34
    {
35
        $this->nonenone = new Type(Type::NONE,   Type::NONE);
36
37
        $this->server11 = new Type($this->sid1,  $this->sop1);
38
        $this->server12 = new Type($this->sid1,  $this->sop2);
39
        $this->server_1 = new Type(Type::ANY,    $this->sop1);
40
        $this->server1_ = new Type($this->sid1,  Type::ANY);
41
        $this->serverN1 = new Type(Type::NONE,   $this->sop1);
42
        $this->server1N = new Type($this->sid1,  Type::NONE);
43
        $this->serverN_ = new Type(Type::NONE,   Type::ANY);
44
        $this->server_N = new Type(Type::ANY,    Type::NONE);
45
        $this->server22 = new Type($this->sid2,  $this->sop2);
46
        $this->server21 = new Type($this->sid2,  $this->sop1);
47
        $this->server_2 = new Type(Type::ANY,    $this->sop2);
48
        $this->server2_ = new Type($this->sid2,  Type::ANY);
49
        $this->serverN2 = new Type(Type::NONE,   $this->sop2);
50
        $this->server2N = new Type($this->sid2,  Type::NONE);
51
52
        $this->domain11 = new Type($this->did1,  $this->dop1);
53
        $this->domain_1 = new Type(Type::ANY,    $this->dop1);
54
        $this->domain1_ = new Type($this->did1,  Type::ANY);
55
        $this->domain22 = new Type($this->did2,  $this->dop2);
56
        $this->domain_2 = new Type(Type::ANY,    $this->dop2);
57
        $this->domain2_ = new Type($this->did2,  Type::ANY);
58
    }
59
60
    protected function tearDown()
61
    {
62
    }
63
64
    protected function copy(TypeInterface $type)
65
    {
66
        return new Type($type->getId(), $type->getName());
0 ignored issues
show
Bug introduced by
Consider using $type->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
67
    }
68
69
    public function testGetUniqueId()
70
    {
71
        $this->assertSame($this->sid1,  $this->server11->getUniqueId());
72
        $this->assertSame($this->sop1,  $this->server_1->getUniqueId());
73
        $this->assertSame($this->sid1,  $this->server1_->getUniqueId());
74
        $this->assertSame($this->sid2,  $this->server22->getUniqueId());
75
        $this->assertSame($this->sop2,  $this->server_2->getUniqueId());
76
        $this->assertSame($this->sid2,  $this->server2_->getUniqueId());
77
    }
78
79
    public function testEquals()
80
    {
81
        $all = [$this->server11, $this->server_1, $this->server1_];
82
        $copies = [];
83
        foreach ($all as $k => $v) {
84
            $copies[$k] = $this->copy($v);
85
        }
86
87 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...
88
            foreach ($copies as $j => $w) {
89
                $this->checkEquals($j === $k, $v, $w);
90
                $this->checkEquals($j === $k, $w, $v);
91
            }
92
        }
93
    }
94
95 View Code Duplication
    protected function checkEquals(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...
96
    {
97
        $check = $lhs->equals($rhs);
98
        if ($check !== $expect) {
99
            var_dump('no equality', $expect, $lhs, $rhs);
0 ignored issues
show
Security Debugging Code introduced by
var_dump('no equality', $expect, $lhs, $rhs); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
100
        }
101
        $this->assertSame($expect, $check);
102
    }
103
104
    public function testMatches()
105
    {
106
        $this->checkMatches([$this->server11, $this->server_1]);
107
        $this->checkMatches([$this->server_1, $this->serverN1], false);
108
        $this->checkMatches([$this->server11, $this->server1_, $this->server12, $this->server1N]);
109
110
        $this->checkDoesntMatch([
111
            $this->server1N, $this->server2N, $this->serverN_,
112
            $this->serverN1, $this->serverN2, $this->server_N,
113
        ]);
114
115
        $this->checkDoesntMatch([$this->server11, $this->server_2, $this->serverN1]);
116
117
        $this->checkDoesntMatch([
118
            $this->server_1, $this->server1_, $this->server_2, $this->server2_,
119
            $this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_,
120
        ]);
121
    }
122
123 View Code Duplication
    protected function checkDoesntMatch(array $types)
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...
124
    {
125
        foreach ($types as $k => $v) {
126
            foreach ($types as $j => $w) {
127
                if ($k !== $j) {
128
                    $this->checkSingleMatch(false, $v, $w);
129
                }
130
            }
131
        }
132
    }
133
134 View Code Duplication
    protected function checkMatches(array $types, bool $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...
135
    {
136
        $all = $types;
137
        if ($self) {
138
            foreach ($types as $type) {
139
                $all[] = $this->copy($type);
140
            }
141
        }
142
        foreach ($all as $k => $v) {
143
            foreach ($all as $j => $w) {
144
                if ($self || $k !== $j) {
145
                    $this->checkSingleMatch(true, $v, $w);
146
                }
147
            }
148
        }
149
    }
150
151 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...
152
    {
153
        $check = $lhs->matches($rhs);
154
        if ($check !== $expect) {
155
            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...
156
        }
157
        $this->assertSame($expect, $check);
158
    }
159
}
160