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

KxTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 53.95 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 3
dl 41
loc 76
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetters() 0 11 1
A testOutput() 0 9 1
A testOutputThrowsExceptionWhenMissingPreference() 9 9 1
A testOutputThrowsExceptionWhenMissingExchanger() 9 9 1
A testFactory() 0 6 1
A testFromText() 10 10 1
A testWire() 13 13 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\Factory;
17
use Badcow\DNS\Rdata\KX;
18
use PHPUnit\Framework\TestCase;
19
20
class KxTest extends TestCase
21
{
22
    public function testSetters(): void
23
    {
24
        $target = 'foo.example.com.';
25
        $preference = 10;
26
        $kx = new KX();
27
        $kx->setExchanger($target);
28
        $kx->setPreference($preference);
29
30
        $this->assertEquals($target, $kx->getExchanger());
31
        $this->assertEquals($preference, $kx->getPreference());
32
    }
33
34
    public function testOutput(): void
35
    {
36
        $target = 'foo.example.com.';
37
        $kx = new KX();
38
        $kx->SetExchanger($target);
39
        $kx->setPreference(42);
40
41
        $this->assertEquals('42 foo.example.com.', $kx->toText());
42
    }
43
44 View Code Duplication
    public function testOutputThrowsExceptionWhenMissingPreference(): 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...
45
    {
46
        $kx = new KX();
47
        $kx->setExchanger('mail.google.com.');
48
49
        $this->expectException(\InvalidArgumentException::class);
50
        $this->expectExceptionMessage('No preference has been set on KX object.');
51
        $kx->toText();
52
    }
53
54 View Code Duplication
    public function testOutputThrowsExceptionWhenMissingExchanger(): 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...
55
    {
56
        $kx = new KX();
57
        $kx->setPreference(15);
58
59
        $this->expectException(\InvalidArgumentException::class);
60
        $this->expectExceptionMessage('No exchanger has been set on KX object.');
61
        $kx->toText();
62
    }
63
64
    public function testFactory(): void
65
    {
66
        $kx = Factory::KX(15, 'mx.example.com.');
67
        $this->assertInstanceOf(KX::class, $kx);
68
        $this->assertEquals('15 mx.example.com.', $kx->toText());
69
    }
70
71 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...
72
    {
73
        $text = '10 mail.example.com.';
74
        /** @var KX $kx */
75
        $kx = new KX();
76
        $kx->fromText($text);
77
78
        $this->assertEquals(10, $kx->getPreference());
79
        $this->assertEquals('mail.example.com.', $kx->getExchanger());
80
    }
81
82 View Code Duplication
    public function testWire(): 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...
83
    {
84
        $kx = new KX();
85
        $kx->setExchanger('mail.example.com.');
86
        $kx->setPreference(10);
87
88
        $expectation = pack('n', 10).chr(4).'mail'.chr(7).'example'.chr(3).'com'.chr(0);
89
90
        $this->assertEquals($expectation, $kx->toWire());
91
        $fromWire = new KX();
92
        $fromWire->fromWire($expectation);
93
        $this->assertEquals($kx, $fromWire);
94
    }
95
}
96