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/root_controller.php';
23 class TestRestAction
extends http\RestAction
25 public $did_get = FALSE
;
26 public $did_post = FALSE
;
27 public $did_delete = FALSE
;
28 public $did_put = FALSE
;
30 public function DoGet(http\Request
$request, http\Response
$response)
32 parent
::_DoGet($request, $response);
33 $this->did_get
= TRUE
;
35 public function DoPost(http\Request
$request, http\Response
$response)
37 parent
::_DoPost($request, $response);
38 $this->did_post
= TRUE
;
40 public function DoDelete(http\Request
$request, http\Response
$response)
42 parent
::_DoDelete($request, $response);
43 $this->did_delete
= TRUE
;
45 public function DoPut(http\Request
$request, http\Response
$response)
47 parent
::_DoPut($request, $response);
48 $this->did_put
= TRUE
;
52 class RestActionTest
extends \PHPUnit_Framework_TestCase
54 public function setUp()
57 $this->fixture
= new TestRestAction(new http\
RootController($globals));
58 $this->request
= new http\
Request();
59 $this->response
= new http\
Response();
62 public function RestExpectSingleTrue($true_var)
64 $vars = array('did_get', 'did_post', 'did_delete', 'did_put');
65 foreach ($vars as $var)
66 if ($var == $true_var)
67 $this->assertTrue($this->fixture
->$var);
69 $this->assertFalse($this->fixture
->$var);
72 public function testGet()
74 $this->request
->http_method
= 'GET';
75 $this->fixture
->Invoke($this->request
, $this->response
);
76 $this->RestExpectSingleTrue('did_get');
79 public function testPost()
81 $this->request
->http_method
= 'POST';
82 $this->fixture
->Invoke($this->request
, $this->response
);
83 $this->RestExpectSingleTrue('did_post');
86 public function testDelete()
88 $this->request
->http_method
= 'DELETE';
89 $this->fixture
->Invoke($this->request
, $this->response
);
90 $this->RestExpectSingleTrue('did_delete');
93 public function testPut()
95 $this->request
->http_method
= 'PUT';
96 $this->fixture
->Invoke($this->request
, $this->response
);
97 $this->RestExpectSingleTrue('did_put');
100 public function testInvalid()
103 $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals));
105 $this->fixture
= new TestRestAction($mock);
107 $mock->expects($this->once())
110 $this->request
->http_method
= 'HEAD';
111 $this->fixture
->Invoke($this->request
, $this->response
);
112 $this->RestExpectSingleTrue('___none___');
114 $this->assertEquals(http\ResponseCode
::METHOD_NOT_ALLOWED
, $this->response
->response_code
);