-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPostsResourceCest.php
More file actions
73 lines (65 loc) · 2.72 KB
/
Copy pathPostsResourceCest.php
File metadata and controls
73 lines (65 loc) · 2.72 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
<?php
class PostsResourceCest
{
protected $endpoint = '/api/posts';
// tests
public function getAllPosts(ApiTester $I)
{
$id = $I->haveRecord('posts', $this->getPostAttributes(['title' => 'Game of Thrones']));
$id2 = $I->haveRecord('posts', $this->getPostAttributes(['title' => 'Lord of the Rings']));
$I->sendGET($this->endpoint);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['id' => "$id", 'title' => 'Game of Thrones']);
$I->seeResponseContainsJson(['id' => "$id2", 'title' => 'Lord of the Rings']);
$I->seeResponseContainsJson([['id' => "$id"], ['id' => "$id2"]]);
}
public function getSinglePost(ApiTester $I)
{
$id = $I->haveRecord('posts', $this->getPostAttributes(['title' => 'Starwars']));
$I->sendGET($this->endpoint."/$id");
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['id' => "$id", 'title' => 'Starwars']);
$I->dontSeeResponseContainsJson([['id' => $id]]);
}
public function createPost(ApiTester $I)
{
$I->sendPOST($this->endpoint, ['title' => 'Game of Rings', 'body' => 'By George Tolkien']);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['title' => 'Game of Rings']);
$id = $I->grabDataFromJsonResponse('id');
$I->seeRecord('posts', ['id' => $id, 'title' => 'Game of Rings']);
$I->sendGET($this->endpoint."/$id");
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['title' => 'Game of Rings']);
}
public function updatePost(ApiTester $I)
{
$id = $I->haveRecord('posts', $this->getPostAttributes(['title' => 'Game of Thrones']));
$I->sendPUT($this->endpoint."/$id", ['title' => 'Lord of Thrones']);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['title' => 'Lord of Thrones']);
$I->seeRecord('posts', ['title' => 'Lord of Thrones']);
$I->dontSeeRecord('posts', ['title' => 'Game of Thrones']);
}
public function deletePost(ApiTester $I)
{
$id = $I->haveRecord('posts', $this->getPostAttributes(['title' => 'Game of Thrones']));
$I->sendDELETE($this->endpoint."/$id");
$I->seeResponseCodeIs(200);
$I->dontSeeRecord('posts', ['id' => $id]);
}
private function getPostAttributes($attributes = [])
{
return array_merge([
'title' => 'Hello Universe',
'body' => 'You are so awesome',
'created_at' => new DateTime(),
'updated_at' => new DateTime()
], $attributes);
}
}