3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite\test
;
18 use hoplite\http
as http
;
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';
24 class TestRestAdapter
extends http\RestAdapter
26 protected function _GetRestAction()
28 return new TestRestAction($this->controller());
31 public function action()
37 class RestAdapterTest
extends \PHPUnit_Framework_TestCase
39 public function setUp()
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();
48 public function RestExpectSingleTrue($true_var)
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);
55 $this->assertFalse($this->fixture
->action()->$var);
58 public function testFetchGet()
60 $this->request
->http_method
= 'GET';
61 $this->request
->data
['action'] = 'fetch';
62 $this->controller
->InvokeAction($this->fixture
);
63 $this->RestExpectSingleTrue('did_get');
66 public function testFetchPost()
68 $this->request
->http_method
= 'POST';
69 $this->request
->data
['action'] = 'fetch';
70 $this->controller
->InvokeAction($this->fixture
);
71 $this->RestExpectSingleTrue('did_get');
74 public function testFetchInvalid()
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
);
83 public function testUpdate()
85 $this->request
->http_method
= 'POST';
86 $this->request
->data
['action'] = 'update';
87 $this->controller
->InvokeAction($this->fixture
);
88 $this->RestExpectSingleTrue('did_post');
91 public function testUpdateInvalid()
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
);
100 public function testDelete()
102 $this->request
->http_method
= 'POST';
103 $this->request
->data
['action'] = 'delete';
104 $this->controller
->InvokeAction($this->fixture
);
105 $this->RestExpectSingleTrue('did_delete');
108 public function testDeleteInvalid()
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
);
117 public function testPut()
119 $this->request
->http_method
= 'POST';
120 $this->request
->data
['action'] = 'insert';
121 $this->controller
->InvokeAction($this->fixture
);
122 $this->RestExpectSingleTrue('did_put');
125 public function testInsertInvalid()
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
);
134 public function testInvalid()
137 $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals));
139 $this->fixture
= new TestRestAdapter($mock);
141 $mock->expects($this->once())
144 $this->request
->http_method
= 'HEAD';
145 $this->controller
->InvokeAction($this->fixture
);
146 $this->RestExpectSingleTrue('___none___');
148 $this->assertEquals(http\ResponseCode
::NOT_FOUND
, $this->response
->response_code
);