Completed
Push — master ( 666d36...a3ce9d )
by Sam
02:53
created

ZoneTest::testFillOut()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 8.3272
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS\Tests;
13
14
use Badcow\DNS\Classes;
15
use Badcow\DNS\Zone;
16
use Badcow\DNS\ResourceRecord;
17
use Badcow\DNS\Rdata\Factory;
18
use Badcow\DNS\AlignedBuilder;
19
use Badcow\DNS\ZoneBuilder;
20
21
class ZoneTest extends TestCase
22
{
23
    /**
24
     * @expectedException \InvalidArgumentException
25
     * @expectedExceptionMessage Zone "example.com" is not a fully qualified domain name.
26
     */
27
    public function testSetName()
28
    {
29
        $zone = new Zone();
30
        $zone->setName('example.com.');
31
        $this->assertEquals('example.com.', $zone->getName());
32
33
        //Should throw exception
34
        $zone->setName('example.com');
35
    }
36
37
    public function testFillOut()
38
    {
39
        $zone = new Zone('example.com.');
40
        $zone->setDefaultTtl(3600);
41
42
        $soa = new ResourceRecord();
43
        $soa->setName('@');
44
        $soa->setRdata(Factory::Soa(
45
            '@',
46
            'post',
47
            '2014110501',
48
            3600,
49
            14400,
50
            604800,
51
            3600
52
        ));
53
54
        $soa->setClass(\Badcow\DNS\Classes::INTERNET);
55
56
        $ns1 = new ResourceRecord();
57
        $ns1->setName('@');
58
        $ns1->setRdata(Factory::Ns('ns1.nameserver.com.'));
59
60
        $ns2 = new ResourceRecord();
61
        $ns2->setName('@');
62
        $ns2->setRdata(Factory::Ns('ns2.nameserver.com.'));
63
64
        $a = new ResourceRecord();
65
        $a->setName('sub.domain');
66
        $a->setRdata(Factory::A('192.168.1.42'));
67
        $a->setComment('This is a local ip.');
68
69
        $a6 = new ResourceRecord();
70
        $a6->setName('ipv6.domain');
71
        $a6->setRdata(Factory::Aaaa('::1'));
72
        $a6->setComment('This is an IPv6 domain.');
73
74
        $mx1 = new ResourceRecord();
75
        $mx1->setName('@');
76
        $mx1->setRdata(Factory::Mx(10, 'mail-gw1.example.net.'));
77
78
        $mx2 = new ResourceRecord();
79
        $mx2->setName('@');
80
        $mx2->setRdata(Factory::Mx(20, 'mail-gw2.example.net.'));
81
82
        $mx3 = new ResourceRecord();
83
        $mx3->setName('@');
84
        $mx3->setRdata(Factory::Mx(30, 'mail-gw3.example.net.'));
85
86
        $loc = new ResourceRecord();
87
        $loc->setName('canberra');
88
        $loc->setRdata(Factory::Loc(
89
            -35.3075,   //Lat
90
            149.1244,   //Lon
91
            500,        //Alt
92
            20.12,      //Size
93
            200.3,      //HP
94
            300.1       //VP
95
        ));
96
        $loc->setComment('This is Canberra');
97
98
        $zone->fromList($loc, $mx2);
99
        $zone->addResourceRecord($soa);
100
        $zone->addResourceRecord($ns1);
101
        $zone->addResourceRecord($mx3);
102
        $zone->addResourceRecord($a);
103
        $zone->addResourceRecord($a6);
104
        $zone->addResourceRecord($ns2);
105
        $zone->addResourceRecord($mx1);
106
107
        $apl = new \Badcow\DNS\Rdata\APL();
108
        $apl->addAddressRange(\IPBlock::create('192.168.0.0/23'));
109
        $apl->addAddressRange(\IPBlock::create('192.168.1.64/28'), false);
110
        $apl->addAddressRange(\IPBlock::create('2001:acad:1::/112'), true);
111
        $apl->addAddressRange(\IPBlock::create('2001:acad:1::8/128'), false);
112
113
        $multicast = new ResourceRecord('multicast', $apl);
114
115
        $zone->addResourceRecord($multicast);
116
117
        ZoneBuilder::fillOutZone($zone);
118
        $expectation = file_get_contents(__DIR__.'/Resources/example.com_filled-out.txt');
119
120
        $this->assertEquals($expectation, AlignedBuilder::build($zone));
121
    }
122
123
    public function testOtherFunctions()
124
    {
125
        $zone = $this->buildTestZone();
126
        $this->assertCount(13, $zone);
127
        $this->assertFalse($zone->isEmpty());
128
129
        $rr = $zone->getResourceRecords()[0];
130
        $this->assertTrue($zone->contains($rr));
131
        $this->assertTrue($zone->remove($rr));
132
        $this->assertFalse($zone->remove($rr));
133
        $this->assertFalse($zone->contains($rr));
134
    }
135
136
    public function testGetClassReturnsDefaultClass()
137
    {
138
        $h1 = new ResourceRecord('host1');
139
        $h2 = new ResourceRecord('host2');
140
        $h3 = new ResourceRecord('host3');
141
        $zone = new Zone('example.com.');
142
        $zone->fromList($h1, $h2, $h3);
143
144
        $this->assertNull($h1->getClass());
145
        $this->assertNull($h2->getClass());
146
        $this->assertNull($h3->getClass());
147
148
        $this->assertEquals(Classes::INTERNET, $zone->getClass());
149
    }
150
}
151