|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ibonly\NaijaEmoji\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Http\Client; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
|
8
|
|
|
|
|
9
|
|
|
class RoutesTest extends PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function setUp() |
|
12
|
|
|
{ |
|
13
|
|
|
$this->client = new Client('https://emojinaija.herokuapp.com'); |
|
|
|
|
|
|
14
|
|
|
$this->request = $this->client->get('/emojis'); |
|
|
|
|
|
|
15
|
|
|
$this->response = $this->request->send(); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Test the URL |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testURL() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->assertEquals("https://emojinaija.herokuapp.com/emojis", $this->request->getUrl()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Test if the ouput of getAll is an object |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testGetAllEmoji() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->assertInternalType("object", $this->response->getBody()); |
|
32
|
|
|
$this->assertEquals(200, $this->response->getStatusCode()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Test get a single emoji endpoint |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testGetSingleEmoji() |
|
39
|
|
|
{ |
|
40
|
|
|
$endpoint = $this->client->get('emogis/5'); |
|
|
|
|
|
|
41
|
|
|
$this->assertEquals(200, $this->response->getStatusCode()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test Post endpoint |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testPOSTEmoji() |
|
48
|
|
|
{ |
|
49
|
|
|
// create our http client (Guzzle) |
|
50
|
|
|
$data = array( |
|
51
|
|
|
'name' => 'Run', |
|
52
|
|
|
'char' => '🏃', |
|
53
|
|
|
'keywords' => "Run, Ere", |
|
54
|
|
|
'category' => 'force' |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$this->setExpectedException("Guzzle\Http\Exception\ClientErrorResponseException"); |
|
58
|
|
|
$request = $this->client->post('/emojis', null, json_encode($data)); |
|
59
|
|
|
$response = $request->send(); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test if Authorization Header is set |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testHeaderAuthorizationNotSet() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->assertFalse(in_array('Host', array_keys($this->response->getHeaders()->toArray()) |
|
68
|
|
|
)); |
|
69
|
|
|
$this->assertFalse($this->response->hasHeader("Authorization")); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Test PUT/PATCH emoji |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testPutPatchEmoji() |
|
76
|
|
|
{ |
|
77
|
|
|
$data = array( |
|
78
|
|
|
'name' => 'Run', |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$this->setExpectedException("Guzzle\Http\Exception\ClientErrorResponseException"); |
|
82
|
|
|
$request = $this->client->put('/emojis/2', null, json_encode($data)); |
|
|
|
|
|
|
83
|
|
|
$request = $this->client->patch('/emojis/2', null, json_encode($data)); |
|
84
|
|
|
$response = $request->send(); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
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: