1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tests for the base rest controller. |
9
|
|
|
* |
10
|
|
|
* @todo: test different serializers, pagination and error handling |
11
|
|
|
* @author Christian Blank <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class BaseRestControllerTest extends RestTest { |
14
|
|
|
|
15
|
|
|
public function setUp() { |
16
|
|
|
parent::setUp(); |
17
|
|
|
Config::inst()->update('Director', 'rules', [ |
18
|
|
|
'v/1/RestTestRoute/$ID/$OtherID' => 'Ntb\RestAPI\TestController', |
19
|
|
|
]); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testControllerGET() { |
23
|
|
|
$result = $this->makeApiRequest('RestTestRoute'); |
24
|
|
|
|
25
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
26
|
|
|
$this->assertEquals('Test GET', $result['message']); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testControllerDELETE() { |
30
|
|
|
$result = $this->makeApiRequest('RestTestRoute', ['method' => 'DELETE']); |
31
|
|
|
|
32
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
33
|
|
|
$this->assertEquals('Test DELETE', $result['message']); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testControllerPOST() { |
37
|
|
|
$result = $this->makeApiRequest('RestTestRoute', ['method' => 'POST']); |
38
|
|
|
|
39
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
40
|
|
|
$this->assertEquals('Test POST', $result['message']); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testControllerPUT() { |
44
|
|
|
$result = $this->makeApiRequest('RestTestRoute', ['method' => 'PUT']); |
45
|
|
|
|
46
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
47
|
|
|
$this->assertEquals('Test PUT', $result['message']); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testControllerHEAD() { |
51
|
|
|
$result = $this->makeApiRequest('RestTestRoute', ['method' => 'HEAD']); |
52
|
|
|
$this->assertEquals(null, $result); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testControllerPATCH() { |
56
|
|
|
$result = $this->makeApiRequest('RestTestRoute', ['method' => 'PATCH']); |
57
|
|
|
|
58
|
|
|
$this->assertTrue(array_key_exists('message', $result)); |
|
|
|
|
59
|
|
|
$this->assertEquals('Test PATCH', $result['message']); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
class TestController extends BaseRestController implements \TestOnly { |
64
|
|
|
|
65
|
|
|
private static $allowed_actions = array ( |
|
|
|
|
66
|
|
|
'post' => true, |
67
|
|
|
'delete' => true, |
68
|
|
|
'get' => true, |
69
|
|
|
'put' => true, |
70
|
|
|
'patch' => true |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
public function get() { |
74
|
|
|
return ['message' => 'Test GET']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function post() { |
78
|
|
|
return ['message' => 'Test POST']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function put() { |
82
|
|
|
return ['message' => 'Test PUT']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function delete() { |
86
|
|
|
return ['message' => 'Test DELETE']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function patch() { |
90
|
|
|
return ['message' => 'Test PATCH']; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.