AplTest::testFromText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\APL;
17
use Badcow\DNS\Rdata\Factory;
18
use PhpIP\IPBlock;
19
use PhpIP\IPv4Block;
20
use PhpIP\IPv6Block;
21
use PHPUnit\Framework\TestCase;
22
23
class AplTest extends TestCase
24
{
25 View Code Duplication
    public function testOutput(): 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...
26
    {
27
        $includedRanges = [
28
            IPBlock::create('192.168.0.0/23'),
29
            IPBlock::create('2001:acad:1::/112'),
30
        ];
31
32
        $excludedRanges = [
33
            IPBlock::create('192.168.1.64/28'),
34
            IPBlock::create('2001:acad:1::8/128'),
35
        ];
36
37
        $apl = Factory::APL($includedRanges, $excludedRanges);
38
39
        $expectation = '1:192.168.0.0/23 2:2001:acad:1::/112 !1:192.168.1.64/28 !2:2001:acad:1::8/128';
40
        $this->assertEquals($expectation, $apl->toText());
41
    }
42
43 View Code Duplication
    public function testGetters(): 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...
44
    {
45
        $includedRanges = [
46
            IPBlock::create('192.168.0.0/23'),
47
            IPBlock::create('2001:acad:1::/112'),
48
        ];
49
50
        $excludedRanges = [
51
            IPBlock::create('192.168.1.64/28'),
52
            IPBlock::create('2001:acad:1::8/128'),
53
        ];
54
55
        $apl = Factory::APL($includedRanges, $excludedRanges);
56
57
        $this->assertEquals($includedRanges, $apl->getIncludedAddressRanges());
58
        $this->assertEquals($excludedRanges, $apl->getExcludedAddressRanges());
59
    }
60
61
    /**
62
     * @throws \Exception
63
     */
64
    public function testFromText(): void
65
    {
66
        $text = '1:192.168.0.0/23 2:2001:acad:1::/112 !1:192.168.1.64/28 !2:2001:acad:1::8/128';
67
        $expectation_incl = [
68
            new IPv4Block('192.168.0.0', 23),
69
            new IPv6Block('2001:acad:1::', 112),
70
            ];
71
72
        $expectation_excl = [
73
            new IPv4Block('192.168.1.64', 28),
74
            new IPv6Block('2001:acad:1::8', 128),
75
        ];
76
77
        /** @var APL $apl */
78
        $apl = new APL();
79
        $apl->fromText($text);
80
81
        $this->assertCount(2, $apl->getIncludedAddressRanges());
0 ignored issues
show
Documentation introduced by
$apl->getIncludedAddressRanges() is of type array<integer,object<PhpIP\IPBlock>>, 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...
82
        $this->assertCount(2, $apl->getExcludedAddressRanges());
0 ignored issues
show
Documentation introduced by
$apl->getExcludedAddressRanges() is of type array<integer,object<PhpIP\IPBlock>>, 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...
83
84
        $this->assertEquals($expectation_incl, $apl->getIncludedAddressRanges());
85
        $this->assertEquals($expectation_excl, $apl->getExcludedAddressRanges());
86
    }
87
88
    public function testWire(): void
89
    {
90
        $expectation = pack('nCCC4nCC',
91
            1,                  //Address Family
92
            24,                 //Prefix
93
            0 +                 //N: "!" is present
94
            4,                  //AFD Length
95
            255, 255, 255, 255,    //AFDPart
96
97
            2,                  //Address Family
98
            64,                 //Prefix
99
            128 +               //N: "!" is present
100
            16                  //AFD Length
101
        ).inet_pton('2001:acad:dead:beef::'); //AFDPart
102
103
        $apl = new APL();
104
        $apl->addAddressRange(IPBlock::create('255.255.255.255/24'), true);
105
        $apl->addAddressRange(IPBlock::create('2001:acad:dead:beef::/64'), false);
106
107
        $this->assertEquals($expectation, $apl->toWire());
108
        $aplFromWire = new APL();
109
        $aplFromWire->fromWire($expectation);
110
111
        $this->assertCount(1, $apl->getIncludedAddressRanges());
0 ignored issues
show
Documentation introduced by
$apl->getIncludedAddressRanges() is of type array<integer,object<PhpIP\IPBlock>>, 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...
112
        $this->assertCount(1, $apl->getExcludedAddressRanges());
0 ignored issues
show
Documentation introduced by
$apl->getExcludedAddressRanges() is of type array<integer,object<PhpIP\IPBlock>>, 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...
113
        $this->assertEquals($apl, $aplFromWire);
114
    }
115
}
116