1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 12/9/15 |
5
|
|
|
* Time: 3:05 PM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Tests\Laravel5\JsonApi; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class JsonApiControllerTest. |
15
|
|
|
*/ |
16
|
|
|
class JsonApiControllerTest extends LaravelTestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Setup DB before each test. |
20
|
|
|
*/ |
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This is required for \Symfony\Bridge\PsrHttpMessage\Factory to work. |
28
|
|
|
* This comes as a trade-off of building the underlying package as framework-agnostic. |
29
|
|
|
* |
30
|
|
|
* @param string $method |
31
|
|
|
* @param string $server |
32
|
|
|
* @param string $uri |
33
|
|
|
*/ |
34
|
|
|
protected function serverEnvironment($method, $server, $uri) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$_SERVER['REQUEST_METHOD'] = strtoupper($method); |
37
|
|
|
$_SERVER['SERVER_NAME'] = str_replace(['http://', 'https://'], '', $server); |
38
|
|
|
$_SERVER['REQUEST_URI'] = $uri; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testListAction() |
42
|
|
|
{ |
43
|
|
|
$this->serverEnvironment('GET', 'example.com', '/api/v1/employees'); |
44
|
|
|
$response = $this->call('GET', 'http://example.com/api/v1/employees'); |
45
|
|
|
|
46
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function testGetActionWhenEmployeeDoesNotExist() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->serverEnvironment('GET', 'example.com', '/api/v1/employees/1000'); |
52
|
|
|
$response = $this->call('GET', 'http://example.com/api/v1/employees/1000'); |
53
|
|
|
|
54
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testPostAction() |
58
|
|
|
{ |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testPatchAction() |
62
|
|
|
{ |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testPutAction() |
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testDeleteActionWhenEmployeeDoesNotExistReturns404() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->serverEnvironment('DELETE', 'example.com', '/api/v1/employees/1000'); |
72
|
|
|
$response = $this->call('DELETE', 'http://example.com/api/v1/employees/1000'); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: