1
|
|
|
<?php |
2
|
|
|
namespace VatNumberCheck\Test\TestCase\Controller; |
3
|
|
|
use Cake\TestSuite\IntegrationTestCase; |
4
|
|
|
use VatNumberCheck\Utility\Model\VatNumberCheck; |
5
|
|
|
/** |
6
|
|
|
* VatNumberChecksController Test Case. |
7
|
|
|
* |
8
|
|
|
* @property \VatNumberCheck\Controller\VatNumberChecksController $VatNumberChecks |
9
|
|
|
*/ |
10
|
|
|
class VatNumberChecksControllerTest extends IntegrationTestCase |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Fixtures |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $fixtures = []; |
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
|
|
|
// Post request, correct vat |
26
|
|
|
$VatNumberChecks = $this->_getMock(); |
27
|
|
|
$data = ['vatNumber' => 'NL820345672B01']; |
28
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
|
|
|
|
29
|
|
|
$expected = array_merge($data, ['status' => 'ok']); |
30
|
|
|
// Test response body |
31
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
32
|
|
|
$actual = $VatNumberChecks->response->statusCode(); |
33
|
|
|
$expected = 200; |
34
|
|
|
// Test response code |
35
|
|
|
$this->assertSame($expected, $actual); |
36
|
|
|
// Get request |
37
|
|
|
$VatNumberChecks = $this->_getMock(); |
|
|
|
|
38
|
|
|
$data = ['vatNumber' => '']; |
39
|
|
|
$actual = $this->testAction($url, ['return' => 'contents']); |
40
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
41
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
42
|
|
|
// Post request, incorrect vat |
43
|
|
|
$VatNumberChecks = $this->_getMock(); |
44
|
|
|
$data = ['vatNumber' => 'NL820345672B02']; |
45
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
46
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
47
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
48
|
|
|
// Post request, correct vat, timeout |
49
|
|
|
$VatNumberChecks = $this->generate('VatNumberCheck.VatNumberChecks', [ |
|
|
|
|
50
|
|
|
'models' => [ |
51
|
|
|
'VatNumberCheck.VatNumberCheck' => ['check'] |
52
|
|
|
] |
53
|
|
|
]); |
54
|
|
|
$VatNumberChecks->VatNumberCheck->setDataSource('vatNumberCheckWebservice'); |
55
|
|
|
$VatNumberChecks->VatNumberCheck->expects($this->any()) |
56
|
|
|
->method('check')->will($this->throwException(new Exception())); |
|
|
|
|
57
|
|
|
$data = ['vatNumber' => 'NL820345672B01']; |
58
|
|
|
$actual = $this->testAction($url, ['return' => 'contents', 'data' => $data, 'method' => 'post']); |
59
|
|
|
$expected = array_merge($data, ['status' => 'failure']); |
60
|
|
|
// Test response body |
61
|
|
|
$this->assertSame($expected, json_decode($actual, true)); |
62
|
|
|
$actual = $VatNumberChecks->response->statusCode(); |
63
|
|
|
$expected = 503; |
64
|
|
|
// Test response code |
65
|
|
|
$this->assertSame($expected, $actual); |
66
|
|
|
} |
67
|
|
|
/** |
68
|
|
|
* Gets a mocked controller instance. |
69
|
|
|
* |
70
|
|
|
* @return VatNumberChecksController |
71
|
|
|
*/ |
72
|
|
|
protected function _getMock() { |
73
|
|
|
$VatNumberChecks = $this->generate('VatNumberCheck.VatNumberChecks'); |
74
|
|
|
$VatNumberChecks->VatNumberCheck = ClassRegistry::init('VatNumberCheck.VatNumberCheck', true); |
|
|
|
|
75
|
|
|
$VatNumberChecks->VatNumberCheck->setDataSource('vatNumberCheckWebservice'); |
76
|
|
|
return $VatNumberChecks; |
77
|
|
|
} |
78
|
|
|
} |