-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostcodeTest.php
More file actions
206 lines (183 loc) · 4.64 KB
/
PostcodeTest.php
File metadata and controls
206 lines (183 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* PostcodeTest.php
*
* @author: Dean Haines
* @copyright: Dean Haines, 2018, UK
* @license: MIT See LICENSE.md
*/
namespace Test\vbpupil\Postcode;
use Exception;
use PHPUnit\Framework\TestCase;
use vbpupil\Exceptions\InvalidPostcodeException;
use vbpupil\Postcode\Postcode;
/**
* Class PostcodeTest
* @package Test\vbpupil
*/
class PostcodeTest extends TestCase
{
/**
* @var
*/
public $sut;
/**
* Checking that what we have been given is NOT a string
*/
public function testPassedValueIsString()
{
try {
$this->sut = new Postcode(2);
} catch (\Exception $e) {
$this->assertTrue($e instanceof InvalidPostcodeException);
$this->assertEquals('Non string value passed.', $e->getMessage());
}
}
/**
* Checking that what we have been given IS empty
*/
public function testEmptyPostcode()
{
try {
$this->sut = new Postcode('');
} catch (\Exception $e) {
$this->assertTrue($e instanceof InvalidPostcodeException);
$this->assertEquals('Empty Postcode.', $e->getMessage());
}
}
/**
* Checking that the postcode we have been give is NOT valid
*/
public function testPostcodeIsNotValid()
{
try {
$this->sut = new Postcode('NN00 1234');
} catch (\Exception $e) {
$this->assertTrue($e instanceof InvalidPostcodeException);
$this->assertEquals('Invalid Postcode.', $e->getMessage());
}
}
/**
* Checking that the postcode has NOT been able to be identified
*/
public function testPostcodeIsNotRecognised()
{
try {
$this->sut = new Postcode('NB1 7JY');
} catch (\Exception $e) {
$this->assertTrue($e instanceof InvalidPostcodeException);
$this->assertEquals('Non recognised postcode.', $e->getMessage());
}
}
/**
* @throws Exception
*/
public function testIsHighlandPostcodes()
{
$arr = [
'postcode' => [
'HS29NP'
],
'head' => [
'HS2'
],
'tail' => [
'9NP'
],
'expected_type' => 'SCOTTISH_HIGHLAND'
];
$this->loopPcodes($arr);
}
/**
* @throws Exception
*/
public function testukMainlandPostcodes()
{
$arr = [
'postcode' => [
'MK10 1SJ', 'NN 14 P W', 'SW1A1AA'
],
'head' => [
'MK10', 'NN1','SW1A'
],
'tail' => [
'1SJ', '4PW','1AA'
],
'expected_type' => 'UK_MAINLAND'
];
$this->loopPcodes($arr);
}
/**
* @throws Exception
*/
public function testNorthernIrelandPostcodes()
{
$arr = [
'postcode' => [
'BT11LA', 'BT1 1DA', 'BT1 1ND', 'BT1 1FP', 'BT11EB', 'BT1 1QB', 'BT1 1NE'
],
'head' => [
'BT1', 'BT1', 'BT1', 'BT1', 'BT1', 'BT1', 'BT1'
],
'tail' => [
'1LA', '1DA', '1ND', '1FP', '1EB', '1QB', '1NE'
],
'expected_type' => 'NORTHERN_IRE'
];
$this->loopPcodes($arr);
}
/**
* @throws Exception
*/
public function testIsleOfWhightPostcodes()
{
$arr = [
'postcode' => [
'PO301NR'
],
'head' => [
'PO30'
],
'tail' => [
'1NR'
],
'expected_type' => 'ISLE_OF_WIGHT'
];
$this->loopPcodes($arr);
}
/**
* @throws Exception
*/
public function testIsleOfManPostcodes()
{
$arr = [
'postcode' => [
'IM13LD','IM1 1BE','IM1 1LB','IM1 5EB'
],
'head' => [
'IM1','IM1','IM1','IM1'
],
'tail' => [
'3LD','1BE','1LB','5EB'
],
'expected_type' => 'ISLE_OF_MAN'
];
$this->loopPcodes($arr);
}
/**
* Used to aid postcode lookup
*
* @param $arr
*/
public function loopPcodes($arr)
{
$cnt = 0;
foreach ($arr['postcode'] as $p) {
$this->sut = new Postcode($p);
$this->assertEquals($arr['expected_type'], $this->sut->getType());
$this->assertEquals($arr['head'][$cnt], $this->sut->getHead());
$this->assertEquals($arr['tail'][$cnt], $this->sut->getTail());
$cnt++;
}
}
}