TypeTest   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 5
Metric Value
eloc 75
c 12
b 0
f 5
dl 0
loc 144
rs 10
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 3 1
A testEquals() 0 12 4
B checkMatches() 0 12 7
A setUp() 0 27 1
A testGetUniqueId() 0 8 1
A checkSingleMatch() 0 7 1
A testMatches() 0 19 1
A tearDown() 0 2 1
A checkEquals() 0 7 1
A checkDoesntMatch() 0 6 4
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\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(): void
34
    {
35
        $this->nonenone = new Type(Type::NONE,   Type::NONE);
0 ignored issues
show
Bug Best Practice introduced by
The property nonenone does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
37
        $this->server11 = new Type($this->sid1,  $this->sop1);
0 ignored issues
show
Bug Best Practice introduced by
The property server11 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        $this->server12 = new Type($this->sid1,  $this->sop2);
0 ignored issues
show
Bug Best Practice introduced by
The property server12 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
        $this->server_1 = new Type(Type::ANY,    $this->sop1);
0 ignored issues
show
Bug Best Practice introduced by
The property server_1 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->server1_ = new Type($this->sid1,  Type::ANY);
0 ignored issues
show
Bug Best Practice introduced by
The property server1_ does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
        $this->serverN1 = new Type(Type::NONE,   $this->sop1);
0 ignored issues
show
Bug Best Practice introduced by
The property serverN1 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        $this->server1N = new Type($this->sid1,  Type::NONE);
0 ignored issues
show
Bug Best Practice introduced by
The property server1N does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->serverN_ = new Type(Type::NONE,   Type::ANY);
0 ignored issues
show
Bug Best Practice introduced by
The property serverN_ does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        $this->server_N = new Type(Type::ANY,    Type::NONE);
0 ignored issues
show
Bug Best Practice introduced by
The property server_N does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
        $this->server22 = new Type($this->sid2,  $this->sop2);
0 ignored issues
show
Bug Best Practice introduced by
The property server22 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->server21 = new Type($this->sid2,  $this->sop1);
0 ignored issues
show
Bug Best Practice introduced by
The property server21 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        $this->server_2 = new Type(Type::ANY,    $this->sop2);
0 ignored issues
show
Bug Best Practice introduced by
The property server_2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
        $this->server2_ = new Type($this->sid2,  Type::ANY);
0 ignored issues
show
Bug Best Practice introduced by
The property server2_ does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
        $this->serverN2 = new Type(Type::NONE,   $this->sop2);
0 ignored issues
show
Bug Best Practice introduced by
The property serverN2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        $this->server2N = new Type($this->sid2,  Type::NONE);
0 ignored issues
show
Bug Best Practice introduced by
The property server2N does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
52
        $this->domain11 = new Type($this->did1,  $this->dop1);
0 ignored issues
show
Bug Best Practice introduced by
The property domain11 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        $this->domain_1 = new Type(Type::ANY,    $this->dop1);
0 ignored issues
show
Bug Best Practice introduced by
The property domain_1 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
        $this->domain1_ = new Type($this->did1,  Type::ANY);
0 ignored issues
show
Bug Best Practice introduced by
The property domain1_ does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
        $this->domainN1 = new Type(Type::NONE,   $this->dop1);
0 ignored issues
show
Bug Best Practice introduced by
The property domainN1 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        $this->domain1N = new Type($this->did1,  Type::NONE);
0 ignored issues
show
Bug Best Practice introduced by
The property domain1N does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
        $this->domain22 = new Type($this->did2,  $this->dop2);
0 ignored issues
show
Bug Best Practice introduced by
The property domain22 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
        $this->domain_2 = new Type(Type::ANY,    $this->dop2);
0 ignored issues
show
Bug Best Practice introduced by
The property domain_2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        $this->domain2_ = new Type($this->did2,  Type::ANY);
0 ignored issues
show
Bug Best Practice introduced by
The property domain2_ does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
    }
61
62
    protected function tearDown(): void
63
    {
64
    }
65
66
    protected function copy(TypeInterface $type)
67
    {
68
        return new Type($type->getId(), $type->getName());
69
    }
70
71
    public function testGetUniqueId()
72
    {
73
        $this->assertSame($this->sop1,  $this->server11->getUniqueId());
74
        $this->assertSame($this->sop1,  $this->server_1->getUniqueId());
75
        $this->assertSame($this->sid1,  $this->server1_->getUniqueId());
76
        $this->assertSame($this->sop2,  $this->server22->getUniqueId());
77
        $this->assertSame($this->sop2,  $this->server_2->getUniqueId());
78
        $this->assertSame($this->sid2,  $this->server2_->getUniqueId());
79
    }
80
81
    public function testEquals()
82
    {
83
        $all = [$this->server11, $this->server_1, $this->server1_];
84
        $copies = [];
85
        foreach ($all as $k => $v) {
86
            $copies[$k] = $this->copy($v);
87
        }
88
89
        foreach ($all as $k => $v) {
90
            foreach ($copies as $j => $w) {
91
                $this->checkEquals($j === $k, $v, $w);
92
                $this->checkEquals($j === $k, $w, $v);
93
            }
94
        }
95
    }
96
97
    protected function checkEquals(bool $expect, $lhs, $rhs)
98
    {
99
        $check = $lhs->equals($rhs);
100
        /*if ($check !== $expect) {
101
            var_dump('no equality', $expect, $lhs, $rhs);
102
        }*/
103
        $this->assertSame($expect, $check);
104
    }
105
106
    public function testMatches()
107
    {
108
        $this->checkMatches([$this->server11, $this->server_1]);
109
        $this->checkMatches([$this->server_1, $this->serverN1], false);
110
        $this->checkMatches([$this->server11, $this->server1_, $this->server12, $this->server1N]);
111
112
        $this->checkDoesntMatch([
113
            $this->server1N, $this->server2N, $this->serverN_,
114
            $this->serverN1, $this->serverN2, $this->server_N,
115
        ]);
116
117
        $this->checkDoesntMatch([
118
            $this->server11, $this->server_2, $this->serverN1,
119
            $this->domain11, $this->domain_2, $this->domainN1,
120
        ]);
121
122
        $this->checkDoesntMatch([
123
            $this->server_1, $this->server1_, $this->server_2, $this->server2_,
124
            $this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_,
125
        ]);
126
    }
127
128
    protected function checkDoesntMatch(array $types)
129
    {
130
        foreach ($types as $k => $v) {
131
            foreach ($types as $j => $w) {
132
                if ($k !== $j) {
133
                    $this->checkSingleMatch(false, $v, $w);
134
                }
135
            }
136
        }
137
    }
138
139
    protected function checkMatches(array $types, bool $self = true)
140
    {
141
        $all = $types;
142
        if ($self) {
143
            foreach ($types as $type) {
144
                $all[] = $this->copy($type);
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 checkSingleMatch(bool $expect, $lhs, $rhs)
157
    {
158
        $check = $lhs->matches($rhs);
159
        /*if ($check !== $expect) {
160
            var_dump('no match', $expect, $lhs, $rhs);
161
        }*/
162
        $this->assertSame($expect, $check);
163
    }
164
}
165