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

TestCase::buildTestZone()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8
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\ResourceRecord;
15
use Badcow\DNS\Rdata\Factory;
16
use Badcow\DNS\Classes;
17
use Badcow\DNS\Zone;
18
19
abstract class TestCase extends \PHPUnit\Framework\TestCase
20
{
21
    const PHP_ENV_CHECKZONE_PATH = 'CHECKZONE_PATH';
22
    const PHP_ENV_PRINT_TEST_ZONE = 'PRINT_TEST_ZONE';
23
24
    /**
25
     * @var string
26
     */
27
    protected $expected = <<< 'DNS'
28
$ORIGIN example.com.
29
$TTL 3600
30
@ IN SOA example.com. postmaster.example.com. 2015050801 3600 14400 604800 3600
31
@ 14400 IN NS ns1.example.net.au.
32
@ 14400 IN NS ns2.example.net.au.
33
subdomain.au A 192.168.1.2; This is a local ip.
34
ipv6domain 3600 IN AAAA ::1; This is an IPv6 domain.
35
canberra IN LOC 35 18 27.000 S 149 7 27.840 E 500.00m 20.12m 200.30m 300.10m; This is Canberra
36
bar.example.com. IN DNAME foo.example.com.
37
@ MX 30 mail-gw3.example.net.
38
@ MX 10 mail-gw1.example.net.
39
@ MX 20 mail-gw2.example.net.
40
alias CNAME subdomain.au.example.com.
41
example.net. IN TXT "v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all"
42
@ IN HINFO "2.7GHz" "Ubuntu 12.04"
43
44
DNS;
45
46
    protected function setUp()
47
    {
48
        $this->normaliseLineFeeds($this->expected);
49
    }
50
51
    protected function normaliseLineFeeds(string &$string)
52
    {
53
        $string = str_replace("\r\n", "\n", $string);
54
        $string = str_replace("\n", PHP_EOL, $string);
55
    }
56
57
    /**
58
     * Get an environment variable.
59
     *
60
     * @param string $varname
61
     *
62
     * @return mixed
63
     */
64
    protected function getEnvVariable($varname)
65
    {
66
        if (false !== $var = getenv($varname)) {
67
            return $var;
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * Print out a block of text.
75
     *
76
     * @param string $text
77
     * @param string $title
78
     */
79
    protected function printBlock($text, $title = '')
80
    {
81
        $output =
82
            PHP_EOL.PHP_EOL.
83
            '====================================='.$title.'====================================='.
84
            PHP_EOL.
85
            $text.
86
            PHP_EOL.
87
            '====================================='.$title.'====================================='.
88
            PHP_EOL.PHP_EOL;
89
90
        echo $output;
91
    }
92
93
    /**
94
     * @return Zone
95
     */
96
    protected function buildTestZone()
97
    {
98
        $soa = new ResourceRecord();
99
        $soa->setClass('IN');
100
        $soa->setName('@');
101
        $soa->setRdata(Factory::Soa(
102
            'example.com.',
103
            'postmaster.example.com.',
104
            2015050801,
105
            3600,
106
            14400,
107
            604800,
108
            3600
109
        ));
110
111
        $ns1 = new ResourceRecord();
112
        $ns1->setClass('IN');
113
        $ns1->setName('@');
114
        $ns1->setTtl(14400);
115
        $ns1->setRdata(Factory::Ns('ns1.example.net.au.'));
116
117
        $ns2 = new ResourceRecord();
118
        $ns2->setClass('IN');
119
        $ns2->setName('@');
120
        $ns2->setTtl(14400);
121
        $ns2->setRdata(Factory::Ns('ns2.example.net.au.'));
122
123
        $a_record = new ResourceRecord();
124
        $a_record->setName('subdomain.au');
125
        $a_record->setRdata(Factory::A('192.168.1.2'));
126
        $a_record->setComment('This is a local ip.');
127
128
        $cname = new ResourceRecord();
129
        $cname->setName('alias');
130
        $cname->setRdata(Factory::Cname('subdomain.au.example.com.'));
131
132
        $aaaa = new ResourceRecord(
133
            'ipv6domain',
134
            Factory::Aaaa('::1'),
135
            3600,
136
            Classes::INTERNET,
137
            'This is an IPv6 domain.'
138
        );
139
140
        $mx1 = new ResourceRecord();
141
        $mx1->setName('@');
142
        $mx1->setRdata(Factory::Mx(10, 'mail-gw1.example.net.'));
143
144
        $mx2 = new ResourceRecord();
145
        $mx2->setName('@');
146
        $mx2->setRdata(Factory::Mx(20, 'mail-gw2.example.net.'));
147
148
        $mx3 = new ResourceRecord();
149
        $mx3->setName('@');
150
        $mx3->setRdata(Factory::Mx(30, 'mail-gw3.example.net.'));
151
152
        $txt = new ResourceRecord();
153
        $txt->setName('example.net.');
154
        $txt->setRdata(Factory::txt('v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all'));
155
        $txt->setClass(Classes::INTERNET);
156
157
        $loc = new ResourceRecord();
158
        $loc->setName('canberra');
159
        $loc->setRdata(Factory::Loc(
160
            -35.3075,   //Lat
161
            149.1244,   //Lon
162
            500,        //Alt
163
            20.12,      //Size
164
            200.3,      //HP
165
            300.1       //VP
166
        ));
167
        $loc->setComment('This is Canberra');
168
        $loc->setClass(Classes::INTERNET);
169
170
        $dname = new ResourceRecord();
171
        $dname->setName('bar.example.com.');
172
        $dname->setClass(Classes::INTERNET);
173
        $dname->setRdata(Factory::Dname('foo.example.com.'));
174
175
        $hinfo = new ResourceRecord();
176
        $hinfo->setName('@');
177
        $hinfo->setClass(Classes::INTERNET);
178
        $hinfo->setRdata(Factory::Hinfo('2.7GHz', 'Ubuntu 12.04'));
179
180
        return new Zone('example.com.', 3600, [
181
            $soa,
182
            $ns1,
183
            $ns2,
184
            $a_record,
185
            $aaaa,
186
            $loc,
187
            $dname,
188
            $mx3,
189
            $mx1,
190
            $mx2,
191
            $cname,
192
            $txt,
193
            $hinfo,
194
        ]);
195
    }
196
}
197