* Add RestAdapaterTest
[hoplite.git] / testing / tests / http / rest_adapter_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\http as http;
19
20 require_once HOPLITE_ROOT . '/http/rest_action.php';
21 require_once HOPLITE_ROOT . '/http/rest_adapter.php';
22 require_once TEST_ROOT . '/tests/http/fixtures.php';
23
24 class TestRestAdapter extends http\RestAdapter
25 {
26 protected function _GetRestAction()
27 {
28 return new TestRestAction($this->controller());
29 }
30
31 public function action()
32 {
33 return $this->action;
34 }
35 }
36
37 class RestAdapterTest extends \PHPUnit_Framework_TestCase
38 {
39 public function setUp()
40 {
41 $globals = array();
42 $this->controller = new http\RootController($globals);
43 $this->fixture = new TestRestAdapter($this->controller);
44 $this->request = $this->controller->request();
45 $this->response = $this->controller->response();
46 }
47
48 public function RestExpectSingleTrue($true_var)
49 {
50 $vars = array('did_get', 'did_post', 'did_delete', 'did_put');
51 foreach ($vars as $var)
52 if ($var == $true_var)
53 $this->assertTrue($this->fixture->action()->$var);
54 else
55 $this->assertFalse($this->fixture->action()->$var);
56 }
57
58 public function testFetchGet()
59 {
60 $this->request->http_method = 'GET';
61 $this->request->data['action'] = 'fetch';
62 $this->controller->InvokeAction($this->fixture);
63 $this->RestExpectSingleTrue('did_get');
64 }
65
66 public function testFetchPost()
67 {
68 $this->request->http_method = 'POST';
69 $this->request->data['action'] = 'fetch';
70 $this->controller->InvokeAction($this->fixture);
71 $this->RestExpectSingleTrue('did_get');
72 }
73
74 public function testFetchInvalid()
75 {
76 $this->request->http_method = 'PUT';
77 $this->request->data['action'] = 'fetch';
78 $this->controller->InvokeAction($this->fixture);
79 $this->RestExpectSingleTrue(NULL);
80 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
81 }
82
83 public function testUpdate()
84 {
85 $this->request->http_method = 'POST';
86 $this->request->data['action'] = 'update';
87 $this->controller->InvokeAction($this->fixture);
88 $this->RestExpectSingleTrue('did_post');
89 }
90
91 public function testUpdateInvalid()
92 {
93 $this->request->http_method = 'GET';
94 $this->request->data['action'] = 'update';
95 $this->controller->InvokeAction($this->fixture);
96 $this->RestExpectSingleTrue(NULL);
97 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
98 }
99
100 public function testDelete()
101 {
102 $this->request->http_method = 'POST';
103 $this->request->data['action'] = 'delete';
104 $this->controller->InvokeAction($this->fixture);
105 $this->RestExpectSingleTrue('did_delete');
106 }
107
108 public function testDeleteInvalid()
109 {
110 $this->request->http_method = 'GET';
111 $this->request->data['action'] = 'delete';
112 $this->controller->InvokeAction($this->fixture);
113 $this->RestExpectSingleTrue(NULL);
114 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
115 }
116
117 public function testPut()
118 {
119 $this->request->http_method = 'POST';
120 $this->request->data['action'] = 'insert';
121 $this->controller->InvokeAction($this->fixture);
122 $this->RestExpectSingleTrue('did_put');
123 }
124
125 public function testInsertInvalid()
126 {
127 $this->request->http_method = 'GET';
128 $this->request->data['action'] = 'insert';
129 $this->controller->InvokeAction($this->fixture);
130 $this->RestExpectSingleTrue(NULL);
131 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
132 }
133
134 public function testInvalid()
135 {
136 $globals = array();
137 $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals));
138
139 $this->fixture = new TestRestAdapter($mock);
140
141 $mock->expects($this->once())
142 ->method('Stop');
143
144 $this->request->http_method = 'HEAD';
145 $this->controller->InvokeAction($this->fixture);
146 $this->RestExpectSingleTrue('___none___');
147
148 $this->assertEquals(http\ResponseCode::NOT_FOUND, $this->response->response_code);
149 }
150 }