Completed
Pull Request — master (#97)
by Sam
04:21
created

CsyncTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 13.27 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 13
loc 98
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetType() 0 5 1
A testGetTypeCode() 0 5 1
A testToText() 0 11 1
A testToWire() 0 15 1
A testFromText() 13 13 1
A testFromWire() 0 18 1
A testFactory() 0 9 1
A testClearTypes() 0 8 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\A;
17
use Badcow\DNS\Rdata\AAAA;
18
use Badcow\DNS\Rdata\CSYNC;
19
use Badcow\DNS\Rdata\Factory;
20
use Badcow\DNS\Rdata\NS;
21
use PHPUnit\Framework\TestCase;
22
23
class CsyncTest extends TestCase
24
{
25
    public function testGetType(): void
26
    {
27
        $csync = new CSYNC();
28
        $this->assertEquals('CSYNC', $csync->getType());
29
    }
30
31
    public function testGetTypeCode(): void
32
    {
33
        $csync = new CSYNC();
34
        $this->assertEquals(62, $csync->getTypeCode());
35
    }
36
37
    public function testToText(): void
38
    {
39
        $csync = new CSYNC();
40
        $csync->setFlags(3);
41
        $csync->setSoaSerial(66);
42
        $csync->addType(A::TYPE);
43
        $csync->addType(NS::TYPE);
44
        $csync->addType(AAAA::TYPE);
45
46
        $this->assertEquals('66 3 A NS AAAA', $csync->toText());
47
    }
48
49
    public function testToWire(): void
50
    {
51
        $csync = new CSYNC();
52
        $csync->setFlags(3);
53
        $csync->setSoaSerial(66);
54
        $csync->addType(A::TYPE);
55
        $csync->addType(NS::TYPE);
56
        $csync->addType(AAAA::TYPE);
57
58
        $expectation = chr(0x00).chr(0x00).chr(0x00).chr(0x42).
59
            chr(0x00).chr(0x03).
60
            chr(0x00).chr(0x04).chr(0x60).chr(0x00).chr(0x00).chr(0x08);
61
62
        $this->assertEquals($expectation, $csync->toWire());
63
    }
64
65 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...
66
    {
67
        $csync = new CSYNC();
68
        $csync->setFlags(3);
69
        $csync->setSoaSerial(66);
70
        $csync->addType(A::TYPE);
71
        $csync->addType(NS::TYPE);
72
        $csync->addType(AAAA::TYPE);
73
74
        $fromText = new CSYNC();
75
        $fromText->fromText('66 3 A NS AAAA');
76
        $this->assertEquals($csync, $fromText);
77
    }
78
79
    /**
80
     * @throws \Badcow\DNS\Rdata\DecodeException
81
     * @throws \Badcow\DNS\Rdata\UnsupportedTypeException
82
     */
83
    public function testFromWire(): void
84
    {
85
        $wireFormat = chr(0x00).chr(0x00).chr(0x00).chr(0x42).
86
            chr(0x00).chr(0x03).
87
            chr(0x00).chr(0x04).chr(0x60).chr(0x00).chr(0x00).chr(0x08);
88
89
        $expectation = new CSYNC();
90
        $expectation->setFlags(3);
91
        $expectation->setSoaSerial(66);
92
        $expectation->addType(A::TYPE);
93
        $expectation->addType(NS::TYPE);
94
        $expectation->addType(AAAA::TYPE);
95
96
        $fromWire = new CSYNC();
97
        $fromWire->fromWire($wireFormat);
98
99
        $this->assertEquals($expectation, $fromWire);
100
    }
101
102
    public function testFactory(): void
103
    {
104
        $types = [A::TYPE, NS::TYPE, AAAA::TYPE];
105
        $csync = Factory::CSYNC(66, 3, $types);
106
107
        $this->assertEquals(66, $csync->getSoaSerial());
108
        $this->assertEquals(3, $csync->getFlags());
109
        $this->assertEquals($types, $csync->getTypes());
110
    }
111
112
    public function testClearTypes(): void
113
    {
114
        $csync = new CSYNC();
115
        $csync->addType('A');
116
        $this->assertCount(1, $csync->getTypes());
0 ignored issues
show
Documentation introduced by
$csync->getTypes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
        $csync->clearTypes();
118
        $this->assertCount(0, $csync->getTypes());
0 ignored issues
show
Documentation introduced by
$csync->getTypes() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
    }
120
}
121