All tests pass again
[hoplite.git] / testing / tests / http / rest_action_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/root_controller.php';
22
23 class TestRestAction extends http\RestAction
24 {
25 public $did_get = FALSE;
26 public $did_post = FALSE;
27 public $did_delete = FALSE;
28 public $did_put = FALSE;
29
30 public function DoGet(http\Request $request, http\Response $response)
31 {
32 parent::DoGet($request, $response);
33 $this->did_get = TRUE;
34 }
35 public function DoPost(http\Request $request, http\Response $response)
36 {
37 parent::DoPost($request, $response);
38 $this->did_post = TRUE;
39 }
40 public function DoDelete(http\Request $request, http\Response $response)
41 {
42 parent::DoDelete($request, $response);
43 $this->did_delete = TRUE;
44 }
45 public function DoPut(http\Request $request, http\Response $response)
46 {
47 parent::DoPut($request, $response);
48 $this->did_put = TRUE;
49 }
50 }
51
52 class RestActionTest extends \PHPUnit_Framework_TestCase
53 {
54 public function setUp()
55 {
56 $globals = array();
57 $this->fixture = new TestRestAction(new http\RootController($globals));
58 $this->request = new http\Request();
59 $this->response = new http\Response();
60 }
61
62 public function RestExpectSingleTrue($true_var)
63 {
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);
68 else
69 $this->assertFalse($this->fixture->$var);
70 }
71
72 public function testGet()
73 {
74 $this->request->http_method = 'GET';
75 $this->fixture->Invoke($this->request, $this->response);
76 $this->RestExpectSingleTrue('did_get');
77 }
78
79 public function testPost()
80 {
81 $this->request->http_method = 'POST';
82 $this->fixture->Invoke($this->request, $this->response);
83 $this->RestExpectSingleTrue('did_post');
84 }
85
86 public function testDelete()
87 {
88 $this->request->http_method = 'DELETE';
89 $this->fixture->Invoke($this->request, $this->response);
90 $this->RestExpectSingleTrue('did_delete');
91 }
92
93 public function testPut()
94 {
95 $this->request->http_method = 'PUT';
96 $this->fixture->Invoke($this->request, $this->response);
97 $this->RestExpectSingleTrue('did_put');
98 }
99
100 public function testInvalid()
101 {
102 $globals = array();
103 $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals));
104
105 $this->fixture = new TestRestAction($mock);
106
107 $mock->expects($this->once())
108 ->method('Stop');
109
110 $this->request->http_method = 'HEAD';
111 $this->fixture->Invoke($this->request, $this->response);
112 $this->RestExpectSingleTrue('___none___');
113
114 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
115 }
116 }