Passed
Push — master ( 4bb1d0...373e60 )
by Dmitry
11:42
created

TypeTest::testJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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;
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 copy(TypeInterface $type): TypeInterface
63
    {
64
        return new Type($type->getId(), $type->getName());
65
    }
66
67
    public function testGetUniqueId(): void
68
    {
69
        $this->assertSame($this->sop1,  $this->server11->getUniqueId());
70
        $this->assertSame($this->sop1,  $this->server_1->getUniqueId());
71
        $this->assertSame($this->sid1,  $this->server1_->getUniqueId());
72
        $this->assertSame($this->sop2,  $this->server22->getUniqueId());
73
        $this->assertSame($this->sop2,  $this->server_2->getUniqueId());
74
        $this->assertSame($this->sid2,  $this->server2_->getUniqueId());
75
    }
76
77
    public function testEquals(): void
78
    {
79
        $all = [$this->server11, $this->server_1, $this->server1_];
80
        $copies = [];
81
        foreach ($all as $k => $v) {
82
            $copies[$k] = $this->copy($v);
83
        }
84
85
        foreach ($all as $k => $v) {
86
            foreach ($copies as $j => $w) {
87
                $this->checkEquals($j === $k, $v, $w);
88
                $this->checkEquals($j === $k, $w, $v);
89
            }
90
        }
91
    }
92
93
    protected function checkEquals(bool $expect, $lhs, $rhs): void
94
    {
95
        $check = $lhs->equals($rhs);
96
        /*if ($check !== $expect) {
97
            var_dump('no equality', $expect, $lhs, $rhs);
98
        }*/
99
        $this->assertSame($expect, $check);
100
    }
101
102
    public function testMatches(): void
103
    {
104
        $this->checkMatches([$this->server11, $this->server_1]);
105
        $this->checkMatches([$this->server_1, $this->serverN1], false);
106
        $this->checkMatches([$this->server11, $this->server1_, $this->server12, $this->server1N]);
107
108
        $this->checkDoesntMatch([
109
            $this->server1N, $this->server2N, $this->serverN_,
110
            $this->serverN1, $this->serverN2, $this->server_N,
111
        ]);
112
113
        $this->checkDoesntMatch([
114
            $this->server11, $this->server_2, $this->serverN1,
115
            $this->domain11, $this->domain_2, $this->domainN1,
116
        ]);
117
118
        $this->checkDoesntMatch([
119
            $this->server_1, $this->server1_, $this->server_2, $this->server2_,
120
            $this->domain_1, $this->domain1_, $this->domain_2, $this->domain2_,
121
        ]);
122
    }
123
124
    protected function checkDoesntMatch(array $types): void
125
    {
126
        foreach ($types as $k => $v) {
127
            foreach ($types as $j => $w) {
128
                if ($k !== $j) {
129
                    $this->checkSingleMatch(false, $v, $w);
130
                }
131
            }
132
        }
133
    }
134
135
    protected function checkMatches(array $types, bool $self = true): void
136
    {
137
        $all = $types;
138
        if ($self) {
139
            foreach ($types as $type) {
140
                $all[] = $this->copy($type);
141
            }
142
        }
143
        foreach ($all as $k => $v) {
144
            foreach ($all as $j => $w) {
145
                if ($self || $k !== $j) {
146
                    $this->checkSingleMatch(true, $v, $w);
147
                }
148
            }
149
        }
150
    }
151
152
    protected function checkSingleMatch(bool $expect, $lhs, $rhs): void
153
    {
154
        $check = $lhs->matches($rhs);
155
        /*if ($check !== $expect) {
156
            var_dump('no match', $expect, $lhs, $rhs);
157
        }*/
158
        $this->assertSame($expect, $check);
159
    }
160
161
    public function testGetId(): void
162
    {
163
        $type = new Type(1, 'monthly');
164
        $this->assertEquals(1, $type->getId());
165
166
        $type = new Type('customId', 'discount');
167
        $this->assertEquals('customId', $type->getId());
168
    }
169
170
    public function testGetName(): void
171
    {
172
        $type = new Type(1, 'monthly');
173
        $this->assertEquals('monthly', $type->getName());
174
175
        $type = new Type(2);
176
        $this->assertNull($type->getName());
177
    }
178
179
    public function testHasName(): void
180
    {
181
        $type = new Type(1, 'monthly');
182
        $this->assertTrue($type->hasName());
183
184
        $type = new Type(2, '');
185
        $this->assertFalse($type->hasName());
186
187
        $type = new Type(3, TypeInterface::ANY);
188
        $this->assertFalse($type->hasName());
189
190
        $type = new Type(4, TypeInterface::NONE);
191
        $this->assertFalse($type->hasName());
192
    }
193
194
    public function testJsonSerialize(): void
195
    {
196
        $type = new Type(1, 'monthly');
197
        $expectedJson = [
198
            'id' => 1,
199
            'name' => 'monthly',
200
        ];
201
        $this->assertEquals($expectedJson, $type->jsonSerialize());
202
    }
203
204
    public function testIsDefined(): void
205
    {
206
        $type = new Type(1, 'monthly');
207
        $this->assertTrue($type->isDefined());
208
209
        $type = new Type(null, null);
210
        $this->assertFalse($type->isDefined());
211
    }
212
213
    public function testTypeWithAnyIdHasAnyId(): void
214
    {
215
        $type = Type::anyId('monthly,leasing');
216
217
        $this->assertSame(TypeInterface::ANY, $type->getId());
218
        $this->assertSame('monthly,leasing', $type->getName());
219
    }
220
}
221