1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ibonly\NaijaEmoji\Test; |
4
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Ibonly\NaijaEmoji\Emoji; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
use Ibonly\NaijaEmoji\AuthController; |
10
|
|
|
use GuzzleHttp\Exception\ClientException; |
11
|
|
|
|
12
|
|
|
class RoutesTest extends PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
protected $url; |
15
|
|
|
protected $token; |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
public function __construct () |
19
|
|
|
{ |
20
|
|
|
$this->emoji = new Emoji(); |
|
|
|
|
21
|
|
|
$auth = new AuthController(); |
22
|
|
|
|
23
|
|
|
$this->token = $auth->getTestToken(); |
24
|
|
|
$this->url = $auth->getAuthUrl(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setUp () |
28
|
|
|
{ |
29
|
|
|
$this->client = new Client(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getTestId () |
33
|
|
|
{ |
34
|
|
|
return $this->emoji->where(['name' => 'TestName'])->getData('id'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* testInvalidEndpoint |
39
|
|
|
*/ |
40
|
|
|
public function testInvalidEndpoint() |
41
|
|
|
{ |
42
|
|
|
$this->setExpectedException("GuzzleHttp\Exception\ClientException"); |
43
|
|
|
|
44
|
|
|
$request = $this->client->request('GET', $this->url.'/emogis'); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test if the ouput of getAll is an object |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function testGetAllEmoji () |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$request = $this->client->request('GET', $this->url.'/emojis'); |
53
|
|
|
|
54
|
|
|
$this->assertInternalType("object", $request->getBody()); |
55
|
|
|
$this->assertEquals(200, $request->getStatusCode()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test get a single emoji endpoint |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testGetSingleEmoji () |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$request = $this->client->request('GET', $this->url.'/emojis/3'); |
64
|
|
|
|
65
|
|
|
$this->assertInternalType("object", $request->getBody()); |
66
|
|
|
$this->assertEquals(200, $request->getStatusCode()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Test Post endpoint |
71
|
|
|
*/ |
72
|
|
|
public function testPOSTEmoji() |
73
|
|
|
{ |
74
|
|
|
$data = array( |
75
|
|
|
'name' => 'TestEmojiName', |
76
|
|
|
'char' => '🎃', |
77
|
|
|
'keywords' => "apple, friut, mac", |
78
|
|
|
'category' => 'fruit' |
79
|
|
|
); |
80
|
|
|
$request = $this->client->request('POST', $this->url.'/emojis',[ 'headers' => ['Authorization'=> $this->token],'form_params' => $data ]); |
81
|
|
|
|
82
|
|
|
$this->assertInternalType('object' , $request); |
83
|
|
|
$this->assertEquals('200', $request->getStatusCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test if Authorization Header is set |
88
|
|
|
*/ |
89
|
|
|
public function testPostIfAuthorizationNotSet () |
90
|
|
|
{ |
91
|
|
|
$data = array( |
92
|
|
|
'name' => 'TestEmojiName', |
93
|
|
|
'char' => '🎃', |
94
|
|
|
'keywords' => "apple, friut, mac", |
95
|
|
|
'category' => 'fruit' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->setExpectedException("GuzzleHttp\Exception\ClientException"); |
99
|
|
|
|
100
|
|
|
$request = $this->client->request('POST', $this->url.'/emojis', ['form_params' => $data]); |
|
|
|
|
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test PUT/PATCH emoji |
106
|
|
|
*/ |
107
|
|
|
public function testPutPatchEmoji () |
108
|
|
|
{ |
109
|
|
|
$data = array( |
110
|
|
|
'name' => 'TestName' |
111
|
|
|
); |
112
|
|
|
$request = $this->client->request('PUT', $this->url.'/emojis/'.$this->getTestId(),[ 'headers' => ['Authorization'=> $this->token],'form_params' => $data ]); |
113
|
|
|
|
114
|
|
|
$this->assertInternalType('object' , $request); |
115
|
|
|
$this->assertEquals('200', $request->getStatusCode()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Test DELETE an emoji |
120
|
|
|
*/ |
121
|
|
|
public function testDeleteEmoji() |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
$this->assertInternalType('integer', $this->getTestId()); |
125
|
|
|
// $data = array( |
|
|
|
|
126
|
|
|
// 'id' => '1' |
|
|
|
|
127
|
|
|
// ); |
128
|
|
|
// $this->request = $this->client->get('/emojis'); |
|
|
|
|
129
|
|
|
// $request = $this->client->delete('/emojis/2', $this->request->getUrl(), json_encode($data)); |
|
|
|
|
130
|
|
|
// $response = $request->send(); |
|
|
|
|
131
|
|
|
// $this->assertEquals(406, $response->getStatusCode()); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: