Passed
Push — master ( 5731b9...6612be )
by Jared
02:33
created

JCRequestTest::testHead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: jaredchu
6
 * Date: 31/05/2017
7
 * Time: 10:14
8
 */
9
10
use JC\JCRequest;
11
12
class JCRequestTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    private $baseUrl = 'https://httpbin.org';
15
    private $params;
16
    private $headers;
17
18
    public function setUp()
19
    {
20
        $this->params = [
21
            'b' => 2,
22
            'c' => '3'
23
        ];
24
        $this->headers = [
25
            'User-Agent' => 'Jared Chu',
26
            'Accept' => 'application/json'
27
        ];
28
    }
29
30 View Code Duplication
    public function testGet()
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...
31
    {
32
        $url = $this->baseUrl . '/get?a=1';
33
34
        $response = JCRequest::get($url, $this->params, $this->headers);
35
        $this->assertEquals(200, $response->status());
36
37
        $responseData = json_decode($response->body());
38
        $this->assertEquals('https://httpbin.org/get?a=1&b=2&c=3', $responseData->url);
39
        $this->assertEquals(1, $responseData->args->a);
40
        $this->assertEquals(2, $responseData->args->b);
41
        $this->assertEquals(3, $responseData->args->c);
42
43
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
44
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
45
    }
46
47 View Code Duplication
    public function testPost()
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...
48
    {
49
        $url = $this->baseUrl . '/post?a=1';
50
51
        $response = JCRequest::post($url, $this->params, $this->headers);
52
        $this->assertEquals(200, $response->status());
53
54
        $responseData = json_decode($response->body());
55
        $this->assertEquals('https://httpbin.org/post?a=1', $responseData->url);
56
        $this->assertEquals(1, $responseData->args->a);
57
        $this->assertEquals(2, $responseData->form->b);
58
        $this->assertEquals(3, $responseData->form->c);
59
60
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
61
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
62
    }
63
64 View Code Duplication
    public function testPut()
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...
65
    {
66
        $url = $this->baseUrl . '/put?a=1';
67
68
        $response = JCRequest::put($url, $this->params, $this->headers);
69
        $this->assertEquals(200, $response->status());
70
71
        $responseData = json_decode($response->body());
72
        $this->assertEquals('https://httpbin.org/put?a=1', $responseData->url);
73
        $this->assertEquals(1, $responseData->args->a);
74
        $this->assertEquals(2, $responseData->form->b);
75
        $this->assertEquals(3, $responseData->form->c);
76
77
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
78
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
79
    }
80
81 View Code Duplication
    public function testPatch()
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...
82
    {
83
        $url = $this->baseUrl . '/patch?a=1';
84
85
        $response = JCRequest::patch($url, $this->params, $this->headers);
86
        $this->assertEquals(200, $response->status());
87
88
        $responseData = json_decode($response->body());
89
        $this->assertEquals('https://httpbin.org/patch?a=1', $responseData->url);
90
        $this->assertEquals(1, $responseData->args->a);
91
        $this->assertEquals(2, $responseData->form->b);
92
        $this->assertEquals(3, $responseData->form->c);
93
94
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
95
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
96
    }
97
98 View Code Duplication
    public function testDelete()
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...
99
    {
100
        $url = $this->baseUrl . '/delete?a=1';
101
102
        $response = JCRequest::delete($url, $this->params, $this->headers);
103
        $this->assertEquals(200, $response->status());
104
105
        $responseData = json_decode($response->body());
106
        $this->assertEquals('https://httpbin.org/delete?a=1', $responseData->url);
107
        $this->assertEquals(1, $responseData->args->a);
108
        $this->assertEquals(2, $responseData->form->b);
109
        $this->assertEquals(3, $responseData->form->c);
110
111
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
112
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
113
    }
114
115
    public function testHead()
116
    {
117
        $url = $this->baseUrl . '/headers';
118
119
        $response = JCRequest::head($url, $this->params, $this->headers);
120
        $this->assertEquals(200, $response->status());
121
        $this->assertEmpty($response->body());
122
123
        $responseHeaders = $response->headers();
124
        $this->assertEquals('keep-alive', $responseHeaders['Connection'][0]);
125
        $this->assertEquals('application/json', $responseHeaders['Content-Type'][0]);
126
        $this->assertEquals('Flask', $responseHeaders['X-Powered-By'][0]);
127
        $this->assertEquals(144, $responseHeaders['Content-Length'][0]);
128
    }
129
}