AaaaTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 8.82 %

Importance

Changes 0
Metric Value
wmc 6
dl 9
loc 102
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 4 1
A testToText() 0 8 1
A testSetAddress() 0 12 1
A testFromText() 9 9 1
A testToWire() 0 11 1
A testToWireThrowsExceptionIfAddressIsMalformed() 0 13 1
A testGetType() 0 5 1
A testGetTypeCode() 0 5 1
A testFromWire() 0 15 1
A testFactory() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Tests\Rdata;
15
16
use Badcow\DNS\Rdata\AAAA;
17
use Badcow\DNS\Rdata\DecodeException;
18
use Badcow\DNS\Rdata\Factory;
19
use PHPUnit\Framework\TestCase;
20
21
class AaaaTest extends TestCase
22
{
23
    public function testToText(): void
24
    {
25
        $address = '2003:dead:beef:4dad:23:46:bb:101';
26
        $aaaa = new AAAA();
27
        $aaaa->setAddress($address);
28
29
        $this->assertEquals($address, $aaaa->toText());
30
    }
31
32
    public function testSetAddress(): void
33
    {
34
        $address = 'fe80::1';
35
        $aaaa = new AAAA();
36
        $aaaa->setAddress($address);
37
38
        $this->assertEquals($address, $aaaa->getAddress());
39
40
        $this->expectException(\InvalidArgumentException::class);
41
        $this->expectExceptionMessage('The address "abc" is not a valid IPv6 address.');
42
        $aaaa->setAddress('abc');
43
    }
44
45 View Code Duplication
    public function testFromText(): void
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...
46
    {
47
        $text = '2003:dead:beef:4dad:23:46:bb:101';
48
        /** @var AAAA $aaaa */
49
        $aaaa = new AAAA();
50
        $aaaa->fromText($text);
51
52
        $this->assertEquals($text, $aaaa->getAddress());
53
    }
54
55
    /**
56
     * @throws DecodeException
57
     */
58
    public function testToWire(): void
59
    {
60
        $address = '2003:dead:beef:4dad:23:46:bb:101';
61
        $expectation = inet_pton($address);
62
        /** @var AAAA $aaaa */
63
        $aaaa = new AAAA();
64
        $aaaa->fromWire($expectation);
65
66
        $this->assertEquals($expectation, $aaaa->toWire());
67
        $this->assertEquals($address, $aaaa->getAddress());
68
    }
69
70
    public function testToWireThrowsExceptionIfAddressIsMalformed(): void
71
    {
72
        $aaaa_prime = new class() extends AAAA {
73
            public function __construct()
74
            {
75
                $this->address = 'abc';
76
            }
77
        };
78
79
        $this->expectException(\InvalidArgumentException::class);
80
        $this->expectExceptionMessage('The IP address "abc" cannot be encoded. Check that it is a valid IP address.');
81
        $aaaa_prime->toWire();
82
    }
83
84
    public function testGetType(): void
85
    {
86
        $aaaa = new AAAA();
87
        $this->assertEquals('AAAA', $aaaa->getType());
88
    }
89
90
    public function testGetTypeCode(): void
91
    {
92
        $aaaa = new AAAA();
93
        $this->assertEquals(28, $aaaa->getTypeCode());
94
    }
95
96
    /**
97
     * @throws DecodeException
98
     */
99
    public function testFromWire(): void
100
    {
101
        $wire = chr(0x07).inet_pton('beef::1').chr(0x07);
102
        $offset = 1;
103
        $aaaa = new AAAA();
104
        $aaaa->fromWire($wire, $offset);
105
106
        $this->assertEquals('beef::1', $aaaa->getAddress());
107
        $this->assertEquals(17, $offset);
108
109
        $wire = pack('C3', 0x61, 0x62, 0x63);
110
        $aaaa = new AAAA();
111
        $this->expectException(DecodeException::class);
112
        $aaaa->fromWire($wire);
113
    }
114
115
    public function testFactory(): void
116
    {
117
        $aaaa = new AAAA();
118
        $aaaa->setAddress('2001:acad:1::');
119
120
        $this->assertEquals(Factory::AAAA('2001:acad:1::'), $aaaa);
121
    }
122
}
123