Introduce FrontController as the replacement for RootController.
[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/front_controller.php';
21 require_once HOPLITE_ROOT . '/http/rest_action.php';
22 require_once TEST_ROOT . '/tests/http/fixtures.php';
23
24 class RestActionTest extends \PHPUnit_Framework_TestCase
25 {
26 public function setUp()
27 {
28 $globals = array();
29 $this->fixture = new TestRestAction(new http\FrontController($globals));
30 $this->request = new http\Request();
31 $this->response = new http\Response();
32 }
33
34 public function RestExpectSingleTrue($true_var)
35 {
36 $vars = array('did_get', 'did_post', 'did_delete', 'did_put');
37 foreach ($vars as $var)
38 if ($var == $true_var)
39 $this->assertTrue($this->fixture->$var);
40 else
41 $this->assertFalse($this->fixture->$var);
42 }
43
44 public function testGet()
45 {
46 $this->request->http_method = 'GET';
47 $this->fixture->Invoke($this->request, $this->response);
48 $this->RestExpectSingleTrue('did_get');
49 }
50
51 public function testPost()
52 {
53 $this->request->http_method = 'POST';
54 $this->fixture->Invoke($this->request, $this->response);
55 $this->RestExpectSingleTrue('did_post');
56 }
57
58 public function testDelete()
59 {
60 $this->request->http_method = 'DELETE';
61 $this->fixture->Invoke($this->request, $this->response);
62 $this->RestExpectSingleTrue('did_delete');
63 }
64
65 public function testPut()
66 {
67 $this->request->http_method = 'PUT';
68 $this->fixture->Invoke($this->request, $this->response);
69 $this->RestExpectSingleTrue('did_put');
70 }
71
72 public function testInvalid()
73 {
74 $globals = array();
75 $mock = $this->getMock('hoplite\http\FrontController', array('Stop'), array($globals));
76
77 $this->fixture = new TestRestAction($mock);
78
79 $mock->expects($this->once())
80 ->method('Stop');
81
82 $this->request->http_method = 'HEAD';
83 $this->fixture->Invoke($this->request, $this->response);
84 $this->RestExpectSingleTrue('___none___');
85
86 $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code);
87 }
88 }