1
|
|
|
<?php |
2
|
|
|
App::uses('VatNumberCheck', 'VatNumberCheck.Model'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* VatNumberCheck Test Case |
6
|
|
|
* |
7
|
|
|
* @property VatNumberCheck.VatNumberCheck $VatNumberCheck |
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
class VatNumberCheckTest extends CakeTestCase { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Fixtures |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $fixtures = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* setUp method |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function setUp() { |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
$this->VatNumberCheck = ClassRegistry::init('VatNumberCheck.VatNumberCheck'); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* tearDown method |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function tearDown() { |
35
|
|
|
unset($this->VatNumberCheck); |
36
|
|
|
|
37
|
|
|
parent::tearDown(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* testNormalize method |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function testNormalize() { |
46
|
|
|
// Correct |
47
|
|
|
|
48
|
|
|
$vatNumber = 'NL820345672B01'; |
49
|
|
|
$result = $this->VatNumberCheck->normalize($vatNumber); |
50
|
|
|
$expected = 'NL820345672B01'; |
51
|
|
|
|
52
|
|
|
$this->assertIdentical($expected, $result); |
53
|
|
|
|
54
|
|
|
// To upper case |
55
|
|
|
|
56
|
|
|
$vatNumber = 'NL820345672b01'; |
57
|
|
|
$result = $this->VatNumberCheck->normalize($vatNumber); |
58
|
|
|
$expected = 'NL820345672B01'; |
59
|
|
|
|
60
|
|
|
$this->assertIdentical($expected, $result); |
61
|
|
|
|
62
|
|
|
// Removal of non-alphanumeric |
63
|
|
|
|
64
|
|
|
$vatNumber = 'NL820345672 B01'; |
65
|
|
|
$result = $this->VatNumberCheck->normalize($vatNumber); |
66
|
|
|
$expected = 'NL820345672B01'; |
67
|
|
|
|
68
|
|
|
$this->assertIdentical($expected, $result); |
69
|
|
|
|
70
|
|
|
$vatNumber = 'NL820345672!B01'; |
71
|
|
|
$result = $this->VatNumberCheck->normalize($vatNumber); |
72
|
|
|
$expected = 'NL820345672B01'; |
73
|
|
|
|
74
|
|
|
$this->assertIdentical($expected, $result); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* testToQueryString method |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function testToQueryString() { |
83
|
|
|
// Correct |
84
|
|
|
|
85
|
|
|
$vatNumber = 'NL820345672B01'; |
86
|
|
|
$result = $this->VatNumberCheck->toQueryString($vatNumber); |
87
|
|
|
$expected = ['ms' => 'NL', 'iso' => 'NL', 'vat' => '820345672B01']; |
88
|
|
|
|
89
|
|
|
$this->assertIdentical($expected, $result); |
90
|
|
|
|
91
|
|
|
// Missing vat |
92
|
|
|
|
93
|
|
|
$vatNumber = 'NL'; |
94
|
|
|
$result = $this->VatNumberCheck->toQueryString($vatNumber); |
95
|
|
|
$expected = ['ms' => 'NL', 'iso' => 'NL', 'vat' => '']; |
96
|
|
|
|
97
|
|
|
$this->assertIdentical($expected, $result); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* testConstructUrl method |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function testConstructUrl() { |
106
|
|
|
// Correct |
107
|
|
|
|
108
|
|
|
$vatNumber = 'NL820345672B01'; |
109
|
|
|
$result = $this->VatNumberCheck->constructUrl($vatNumber); |
110
|
|
|
$expected = sprintf( |
111
|
|
|
'http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=%s&iso=%s&vat=%s', 'NL', 'NL', '820345672B01' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->assertIdentical($expected, $result); |
115
|
|
|
|
116
|
|
|
// Missing vat |
117
|
|
|
|
118
|
|
|
$vatNumber = 'NL'; |
119
|
|
|
$result = $this->VatNumberCheck->constructUrl($vatNumber); |
120
|
|
|
$expected = sprintf( |
121
|
|
|
'http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=%s&iso=%s&vat=%s', 'NL', 'NL', '' |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->assertIdentical($expected, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* testGetUrlContent method |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function testGetUrlContent() { |
133
|
|
|
// Correct |
134
|
|
|
|
135
|
|
|
$url = sprintf( |
136
|
|
|
'http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=%s&iso=%s&vat=%s', 'NL', 'NL', '820345672B01' |
137
|
|
|
); |
138
|
|
|
$result = $this->VatNumberCheck->getUrlContent($url); |
139
|
|
|
|
140
|
|
|
$this->assertTextContains('<body>', $result); |
141
|
|
|
|
142
|
|
|
// Missing url |
143
|
|
|
|
144
|
|
|
$url = ''; |
145
|
|
|
$result = $this->VatNumberCheck->getUrlContent($url); |
146
|
|
|
|
147
|
|
|
$this->assertFalse($result); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* testCheck method |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function testCheck() { |
156
|
|
|
// Correct |
157
|
|
|
|
158
|
|
|
$vatNumber = 'NL820345672B01'; |
159
|
|
|
$result = $this->VatNumberCheck->check($vatNumber); |
160
|
|
|
|
161
|
|
|
$this->assertTrue($result); |
162
|
|
|
|
163
|
|
|
// Incorrect vat |
164
|
|
|
|
165
|
|
|
$vatNumber = 'NL820345672B02'; |
166
|
|
|
$result = $this->VatNumberCheck->check($vatNumber); |
167
|
|
|
|
168
|
|
|
$this->assertFalse($result); |
169
|
|
|
|
170
|
|
|
// Empty vat |
171
|
|
|
|
172
|
|
|
$vatNumber = ''; |
173
|
|
|
$result = $this->VatNumberCheck->check($vatNumber); |
174
|
|
|
|
175
|
|
|
$this->assertFalse($result); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* testCheckException method |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
* @expectedException InternalErrorException |
183
|
|
|
*/ |
184
|
|
|
public function testCheckException() { |
185
|
|
|
// Simulate a timeout of `VatNumberCheck::getUrlContent` |
186
|
|
|
|
187
|
|
|
$VatNumberCheck = $this->getMockForModel('VatNumberCheck', ['getUrlContent']); |
188
|
|
|
$VatNumberCheck->expects($this->any())->method('getUrlContent')->will($this->returnValue(false)); |
189
|
|
|
|
190
|
|
|
$vatNumber = 'NL820345672B01'; |
191
|
|
|
$VatNumberCheck->check($vatNumber); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|