Repository navigation
Expand file tree
/
Copy pathRestTest.php
More file actions
186 lines (165 loc) · 7.8 KB
/
Copy pathRestTest.php
File metadata and controls
186 lines (165 loc) · 7.8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
use Codeception\Util\Stub as Stub;
class RestTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Codeception\Module\REST
*/
protected $module;
public function setUp() {
$this->module = new \Codeception\Module\REST();
$connector = new \Codeception\Util\Connector\Universal();
$connector->setIndex(\Codeception\Configuration::dataDir().'/rest/index.php');
$this->module->client = $connector;
$this->module->_before(Stub::makeEmpty('\Codeception\TestCase\Cest'));
$this->module->client->setServerParameters(array(
'SCRIPT_FILENAME' => 'index.php',
'SCRIPT_NAME' => 'index',
'SERVER_NAME' => 'localhost',
'SERVER_PROTOCOL' => 'http'
));
}
public function testGet() {
$this->module->sendGET('/rest/user/');
$this->module->seeResponseIsJson();
$this->module->seeResponseContains('davert');
$this->module->seeResponseContainsJson(array('name' => 'davert'));
$this->module->seeResponseCodeIs(200);
$this->module->dontSeeResponseCodeIs(404);
}
public function testPost() {
$this->module->sendPOST('/rest/user/', array('name' => 'john'));
$this->module->seeResponseContains('john');
$this->module->seeResponseContainsJson(array('name' => 'john'));
}
public function testPut() {
$this->module->sendPUT('/rest/user/', array('name' => 'laura'));
$this->module->seeResponseContains('davert@mail.ua');
$this->module->seeResponseContainsJson(array('name' => 'laura'));
$this->module->dontSeeResponseContainsJson(array('name' => 'john'));
}
public function testGrabDataFromJsonResponse() {
$this->module->sendGET('/rest/user/');
// simple assoc array
$this->assertEquals('davert@mail.ua', $this->module->grabDataFromJsonResponse('email'));
// nested assoc array
$this->assertEquals('Kyiv', $this->module->grabDataFromJsonResponse('address.city'));
// nested index array
$this->assertEquals('DavertMik', $this->module->grabDataFromJsonResponse('aliases.0'));
// fail if data not found
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError', 'Response does not have required data');
$this->module->grabDataFromJsonResponse('address.street');
}
public function testValidJson()
{
$this->module->response = '{"xxx": "yyy"}';
$this->module->seeResponseIsJson();
$this->module->response = '{"xxx": "yyy", "zzz": ["a","b"]}';
$this->module->seeResponseIsJson();
$this->module->seeResponseEquals($this->module->response);
}
public function testInvalidJson()
{
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->module->response = '{xxx = yyy}';
$this->module->seeResponseIsJson();
}
public function testValidXml()
{
$this->module->response = '<xml></xml>';
$this->module->seeResponseIsXml();
$this->module->response = '<xml><name>John</name></xml>';
$this->module->seeResponseIsXml();
$this->module->seeResponseEquals($this->module->response);
}
public function testInvalidXml()
{
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->module->response = '<xml><name>John</surname></xml>';
$this->module->seeResponseIsXml();
}
public function testSeeInJson()
{
$this->module->response = '{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}, "labels": null}}';
$this->module->seeResponseIsJson();
$this->module->seeResponseContainsJson(array('name' => 'Davert'));
$this->module->seeResponseContainsJson(array('user' => array('name' => 'Davert')));
$this->module->seeResponseContainsJson(array('ticket' => array('title' => 'Bug should be fixed')));
$this->module->seeResponseContainsJson(array('ticket' => array('user' => array('name' => 'Davert'))));
$this->module->seeResponseContainsJson(array('ticket' => array('labels' => null)));
}
public function testArrayJson()
{
$this->module->response = '[{"id":1,"title": "Bug should be fixed"},{"title": "Feature should be implemented","id":2}]';
$this->module->seeResponseContainsJson(array('id' => 1));
}
public function testDontSeeInJson()
{
$this->module->response = '{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}}}';
$this->module->seeResponseIsJson();
$this->module->dontSeeResponseContainsJson(array('name' => 'Davet'));
$this->module->dontSeeResponseContainsJson(array('user' => array('name' => 'Davet')));
$this->module->dontSeeResponseContainsJson(array('user' => array('title' => 'Bug should be fixed')));
}
public function testApplicationJsonIncludesJsonAsContent()
{
$this->module->haveHttpHeader('Content-Type', 'application/json');
$this->module->sendPOST('/', array('name' => 'john'));
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertContains('application/json', $request->getServer());
$server = $request->getServer();
$this->assertEquals('application/json', $server['HTTP_Content-Type']);
$this->assertJson($request->getContent());
$this->assertEmpty($request->getParameters());
}
public function testGetApplicationJsonNotIncludesJsonAsContent()
{
$this->module->haveHttpHeader('Content-Type', 'application/json');
$this->module->sendGET('/', array('name' => 'john'));
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertNull($request->getContent());
$this->assertContains('john', $request->getParameters());
}
public function testUrlIsFull()
{
$this->module->sendGET('/api/v1/users');
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertEquals('http://localhost/api/v1/users',$request->getUri());
}
public function testSeeHeaders()
{
$response = new \Symfony\Component\BrowserKit\Response("", 200, array(
'Cache-Control' => array('no-cache', 'no-store'),
'Content_Language' => 'en-US'
));
$this->module->client->mockResponse($response);
$this->module->sendGET('/');
$this->module->seeHttpHeader('Cache-Control');
$this->module->seeHttpHeader('content_language','en-US');
$this->module->seeHttpHeader('Content-Language','en-US');
$this->module->dontSeeHttpHeader('Content-Language','en-RU');
$this->module->dontSeeHttpHeader('Content-Language1');
$this->module->seeHttpHeaderOnce('Content-Language');
\Codeception\Util\Debug::debug($this->module->grabHttpHeader('Cache-Control', false));
$this->assertEquals('en-US', $this->module->grabHttpHeader('Content-Language'));
$this->assertEquals('no-cache', $this->module->grabHttpHeader('Cache-Control'));
$this->assertEquals(array('no-cache', 'no-store'), $this->module->grabHttpHeader('Cache-Control', false));
}
public function testSeeHeadersOnce()
{
$this->shouldFail();
$response = new \Symfony\Component\BrowserKit\Response("", 200, array(
'Cache-Control' => array('no-cache', 'no-store'),
));
$this->module->client->mockResponse($response);
$this->module->sendGET('/');
$this->module->seeHttpHeaderOnce('Cache-Control');
}
protected function shouldFail()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
}
}