1
|
|
|
<?php |
2
|
|
|
App::uses('VatNumberChecksController', 'VatNumberCheck.Controller'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* VatNumberChecksController Test Case |
6
|
|
|
* |
7
|
|
|
* @property VatNumberCheck.VatNumberChecksController $VatNumberChecks |
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
class VatNumberChecksControllerTest extends ControllerTestCase { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Fixtures |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $fixtures = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tests `/vat_number_check/vat_number_checks/check.json`. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function testCheck() { |
24
|
|
|
$url = '/vat_number_check/vat_number_checks/check.json'; |
25
|
|
|
|
26
|
|
|
// Post request, correct vat |
27
|
|
|
|
28
|
|
|
$VatNumberChecks = $this->_getMock(); |
29
|
|
|
|
30
|
|
|
$data = ['vatNumber' => 'NL820345672B01']; |
31
|
|
|
|
32
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
33
|
|
|
$expected = array_merge($data, ['status' => 'ok']); |
34
|
|
|
|
35
|
|
|
// Test response body |
36
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
37
|
|
|
|
38
|
|
|
$actual = $VatNumberChecks->response->statusCode(); |
39
|
|
|
$expected = 200; |
40
|
|
|
|
41
|
|
|
// Test response code |
42
|
|
|
$this->assertSame($expected, $actual); |
43
|
|
|
|
44
|
|
|
// Get request |
45
|
|
|
|
46
|
|
|
$VatNumberChecks = $this->_getMock(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$data = ['vatNumber' => '']; |
49
|
|
|
|
50
|
|
|
$actual = $this->testAction($url, ['return' => 'contents']); |
51
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
52
|
|
|
|
53
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
54
|
|
|
|
55
|
|
|
// Post request, incorrect vat |
56
|
|
|
|
57
|
|
|
$VatNumberChecks = $this->_getMock(); |
58
|
|
|
|
59
|
|
|
$data = ['vatNumber' => 'NL820345672B02']; |
60
|
|
|
|
61
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
62
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
63
|
|
|
|
64
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tests `/vat_number_check/vat_number_checks/check.json`. |
69
|
|
|
* |
70
|
|
|
* Post request, correct vat, timeout |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* @requires PHPUnit 5.7 |
74
|
|
|
* @todo This should ever happen, right? |
75
|
|
|
*/ |
76
|
|
|
public function testCheck503() { |
77
|
|
|
$url = '/vat_number_check/vat_number_checks/check.json'; |
78
|
|
|
|
79
|
|
|
$VatNumberChecks = $this->generate('VatNumberCheck.VatNumberChecks', [ |
80
|
|
|
'models' => [ |
81
|
|
|
'VatNumberCheck.VatNumberCheck' => ['check'] |
82
|
|
|
] |
83
|
|
|
]); |
84
|
|
|
$VatNumberChecks->VatNumberCheck->setDataSource('vatNumberCheckWebservice'); |
85
|
|
|
$VatNumberChecks->VatNumberCheck->expects($this->any()) |
86
|
|
|
->method('check')->will($this->throwException(new Exception())); |
87
|
|
|
|
88
|
|
|
$data = ['vatNumber' => 'NL820345672B01']; |
89
|
|
|
|
90
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
91
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
92
|
|
|
|
93
|
|
|
// Test response body |
94
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
95
|
|
|
|
96
|
|
|
$actual = $VatNumberChecks->response->statusCode(); |
97
|
|
|
$expected = 503; |
98
|
|
|
|
99
|
|
|
// Test response code |
100
|
|
|
$this->assertSame($expected, $actual); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets a mocked controller instance. |
105
|
|
|
* |
106
|
|
|
* @return VatNumberChecksController |
107
|
|
|
*/ |
108
|
|
|
protected function _getMock() { |
109
|
|
|
$VatNumberChecks = $this->generate('VatNumberCheck.VatNumberChecks'); |
110
|
|
|
$VatNumberChecks->VatNumberCheck = ClassRegistry::init('VatNumberCheck.VatNumberCheck', true); |
|
|
|
|
111
|
|
|
$VatNumberChecks->VatNumberCheck->setDataSource('vatNumberCheckWebservice'); |
112
|
|
|
|
113
|
|
|
return $VatNumberChecks; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|