Passed
Pull Request — master (#57)
by
unknown
08:52
created

RequestTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 113
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testResponseInstance() 0 6 1
A testBadAuthentication() 0 9 1
A testSuccessfulGetResponse() 0 6 1
A testFailedGetResponse() 0 6 1
A testSuccesfulPostResponse() 0 6 1
A testFailedPostResponse() 0 6 1
A testBasket() 0 30 1
A testStandardHeaders() 0 5 1
A testAdditionalHeadersOnInit() 0 8 1
A testAdditionalHeadersAfterInit() 0 8 1
1
<?php
2
3
namespace QuickPay\Tests;
4
5
use QuickPay\API\Client;
6
use QuickPay\API\Request;
7
use QuickPay\API\Response;
8
9
class RequestTest extends \PHPUnit_Framework_TestCase
10
{
11
    protected $request;
12
13
    public function setUp()
14
    {
15
        $this->client = new Client();
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        $this->request = new Request($this->client);
17
    }
18
19
    public function testResponseInstance()
20
    {
21
        $pingResponse = $this->request->get('/ping');
22
23
        $this->assertTrue(($pingResponse instanceof Response));
24
    }
25
26
    public function testBadAuthentication()
27
    {
28
        $client = new Client(':foo');
29
        $request = new Request($client);
30
31
        $response = $request->get('/ping');
32
33
        $this->assertEquals(401, $response->httpStatus());
34
    }
35
36
    public function testSuccessfulGetResponse()
37
    {
38
        $pingResponse = $this->request->get('/ping');
39
40
        $this->assertTrue($pingResponse->isSuccess());
41
    }
42
43
    public function testFailedGetResponse()
44
    {
45
        $pingResponse = $this->request->get('/foobar');
46
47
        $this->assertFalse($pingResponse->isSuccess());
48
    }
49
50
    public function testSuccesfulPostResponse()
51
    {
52
        $pingResponse = $this->request->post('/ping');
53
54
        $this->assertTrue($pingResponse->isSuccess());
55
    }
56
57
    public function testFailedPostResponse()
58
    {
59
        $pingResponse = $this->request->post('/foobar');
60
61
        $this->assertFalse($pingResponse->isSuccess());
62
    }
63
64
    /**
65
     * Test function added to make sure that issue gh-54 is fixed.
66
     */
67
    public function testBasket()
68
    {
69
        $basket = [];
70
        $basket[0] = [
71
            'qty' => 1,
72
            'item_no' => 2,
73
            'item_name' => 'Test 1',
74
            'item_price' => 100,
75
            'vat_rate' => 0.25,
76
        ];
77
        $basket[1] = [
78
            'qty' => 1,
79
            'item_no' => 2,
80
            'item_name' => 'Test 2',
81
            'item_price' => 100,
82
            'vat_rate' => 0.25,
83
        ];
84
85
        $form = [
86
            'currency' => 'DKK',
87
            'order_id' => 1,
88
            'basket' => $basket,
89
        ];
90
91
        $query = $this->request->httpBuildQuery($form);
92
93
        $expected = 'currency=DKK&order_id=1&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 1&basket[][item_price]=100&basket[][vat_rate]=0.25&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 2&basket[][item_price]=100&basket[][vat_rate]=0.25';
94
95
        $this->assertEquals(urldecode($query), $expected);
96
    }
97
98
    public function testStandardHeaders()
99
    {
100
        $pingResponse = $this->request->get('/ping');
0 ignored issues
show
Unused Code introduced by
$pingResponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
        $this->assertContains('Accept: application/json', curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT));
102
    }
103
104
    public function testAdditionalHeadersOnInit()
105
    {
106
        $extra_headers = array('SpecialVar: foo');
107
        $client = new Client(null, $extra_headers);
108
        $request = new Request($client);
109
        $pingResponse = $request->get('/ping');
0 ignored issues
show
Unused Code introduced by
$pingResponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
        $this->assertContains($extra_headers[0], curl_getinfo($client->ch, CURLINFO_HEADER_OUT));
111
    }
112
113
    public function testAdditionalHeadersAfterInit()
114
    {
115
        $extra_headers = array('NewVar: foo');
116
        $this->client->setHeaders($extra_headers);
117
        $request = new Request($this->client);
118
        $pingResponse = $request->get('/ping');
0 ignored issues
show
Unused Code introduced by
$pingResponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
        $this->assertContains($extra_headers[0], curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT));
120
    }
121
}
122