WRestresponseTest.php
2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
class WRestResponseTest extends CTestCase
{
/**
* @var JsonResponse
*/
static $response = null;
public function setUp()
{
static::$response = new JsonResponse();
}
public function testRestPesonseObj()
{
$response = WRestResponse::factory('json');
$this->assertTrue($response instanceof WRestResponse);
$this->assertTrue($response instanceof JsonResponse);
$this->assertTrue(static::$response instanceof WRestResponse);
//test default param for new obj
$this->assertEmpty(static::$response->getBody());
$this->assertEquals(static::$response->getStatus(), 200);
}
public function testStatuses()
{
$this->assertTrue(static::$response->setStatus(200) instanceof WRestResponse);
static::$response->setStatus(200);
$this->assertEquals(static::$response->getStatus(), 200);
$this->assertEquals(static::$response->getStatusCodeMessage(200), "OK");
static::$response->setStatus(400);
$this->assertEquals(static::$response->getStatus(), 400);
$this->assertEquals(static::$response->getStatusCodeMessage(400), "Bad Request");
$this->assertEquals(static::$response->getStatusCodeMessage(401), "Unauthorized");
$this->assertEquals(static::$response->getStatusCodeMessage(401, false), "You must be authorized to view this page.");
$this->assertEquals(static::$response->getStatusCodeMessage(402), "Payment Required");
$this->assertEquals(static::$response->getStatusCodeMessage(403), "Forbidden");
$this->assertEquals(static::$response->getStatusCodeMessage(404), "Not Found");
$this->assertEquals(static::$response->getStatusCodeMessage(500), "Internal Server Error");
$this->assertEquals(static::$response->getStatusCodeMessage(501), "Not Implemented");
$this->assertEquals(static::$response->getStatusCodeMessage(9999), ""); //test unexisting code
}
public function testGetErrorMessage(){
$error = static::$response->getErrorMessage(500);
$expectedValue = array(
'code' => 500,
'title' => 'Internal Server Error',
'message' => 'The server encountered an error processing your request.',
);
$this->assertEquals($error, $expectedValue);
}
public function testGetHeaders(){
$expectedValue = array(
'HTTP/1.1 200 OK',
'Content-type: '.static::$response->getContentType(),
);
static::$response->setStatus(200);
$headers = static::$response->getHeaders();
$this->assertEquals($headers, $expectedValue);
}
public function testGetContentType(){
$expectedValue = "application/json";
$this->assertEquals(static::$response->getContentType(), $expectedValue);
}
public function testGetBody(){
$data = array(
'data' => 'someData',
);
$expectedValue = json_encode($data);
static::$response->setParams($data);
$this->assertEquals($expectedValue, static::$response->getBody());
}
}