Completed
Push — master ( 72b670...3b819e )
by Sam
05:26 queued 03:37
created

CertTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 15.84 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 16
loc 101
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetType() 0 5 1
A testGetTypeCode() 0 5 1
A testToText() 0 12 1
A testWire() 0 18 1
A testFromText() 0 14 1
A testFactory() 0 10 1
A testGetKeyTypeMnemonic() 8 8 1
A testGetKeyTypeValue() 8 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\Algorithms;
17
use Badcow\DNS\Rdata\CERT;
18
use Badcow\DNS\Rdata\Factory;
19
use PHPUnit\Framework\TestCase;
20
21
class CertTest extends TestCase
22
{
23
    /**
24
     * @var string
25
     */
26
    private $certificate;
27
28
    public function setUp(): void
29
    {
30
        $certificate = file_get_contents(__DIR__.'/../Resources/google.com.cer');
31
        $this->certificate = base64_decode(str_replace(['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----', "\r", "\n"], '', $certificate));
32
    }
33
34
    public function testGetType(): void
35
    {
36
        $cert = new CERT();
37
        $this->assertEquals('CERT', $cert->getType());
38
    }
39
40
    public function testGetTypeCode(): void
41
    {
42
        $cert = new CERT();
43
        $this->assertEquals(37, $cert->getTypeCode());
44
    }
45
46
    public function testToText(): void
47
    {
48
        $cert = new CERT();
49
        $cert->setCertificateType('PGP');
50
        $cert->setKeyTag(65);
51
        $cert->setAlgorithm(Algorithms::ECC);
52
        $cert->setCertificate($this->certificate);
53
54
        $expectation = 'PGP 65 ECC '.base64_encode($this->certificate);
55
56
        $this->assertEquals($expectation, $cert->toText());
57
    }
58
59
    public function testWire(): void
60
    {
61
        $cert = new CERT();
62
        $cert->setCertificateType('PGP');
63
        $cert->setKeyTag(65);
64
        $cert->setAlgorithm(Algorithms::ECC);
65
        $cert->setCertificate($this->certificate);
66
67
        $wireFormatted = $cert->toWire();
68
69
        $fromWire = new CERT();
70
        $fromWire->fromWire($wireFormatted);
71
72
        $this->assertEquals(3, $fromWire->getCertificateType());
73
        $this->assertEquals(65, $fromWire->getKeyTag());
74
        $this->assertEquals(4, $fromWire->getAlgorithm());
75
        $this->assertEquals($this->certificate, $fromWire->getCertificate());
76
    }
77
78
    public function testFromText(): void
79
    {
80
        $cert = new CERT();
81
        $cert->setCertificateType('PGP');
82
        $cert->setKeyTag(65);
83
        $cert->setAlgorithm(Algorithms::ECC);
84
        $cert->setCertificate($this->certificate);
85
86
        $text = 'PGP 65 ECC '.base64_encode($this->certificate);
87
88
        $fromText = new CERT();
89
        $fromText->fromText($text);
90
        $this->assertEquals($cert, $fromText);
91
    }
92
93
    public function testFactory(): void
94
    {
95
        $cert = new CERT();
96
        $cert->setCertificateType('PGP');
97
        $cert->setKeyTag(65);
98
        $cert->setAlgorithm(Algorithms::ECC);
99
        $cert->setCertificate($this->certificate);
100
101
        $this->assertEquals($cert, Factory::CERT('PGP', 65, Algorithms::ECC, $this->certificate));
102
    }
103
104 View Code Duplication
    public function testGetKeyTypeMnemonic(): 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...
105
    {
106
        $this->assertEquals('IACPKIX', CERT::getKeyTypeMnemonic(8));
107
108
        $this->expectException(\InvalidArgumentException::class);
109
        $this->expectExceptionMessage('"256" is not a valid key type.');
110
        CERT::getKeyTypeMnemonic(256);
111
    }
112
113 View Code Duplication
    public function testGetKeyTypeValue(): 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...
114
    {
115
        $this->assertEquals(8, CERT::getKeyTypeValue('IACPKIX'));
116
117
        $this->expectException(\InvalidArgumentException::class);
118
        $this->expectExceptionMessage('"NOT_A_VALUE" is not a valid key type mnemonic.');
119
        CERT::getKeyTypeValue('NOT_A_VALUE');
120
    }
121
}
122