Completed
Push — master ( d7714e...4700f7 )
by Nil
06:00
created

testDeleteActionWhenEmployeeDoesNotExistReturns404()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
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)
0 ignored issues
show
Coding Style introduced by
serverEnvironment uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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