Test Failed
Pull Request — master (#54)
by Lars
02:18
created

RequestTest::testSuccesfulPostResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
        $client = new Client();
16
        $this->request = new Request($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
    public function testBasket()
65
    {
66
        $basket = [];
67
        $basket[0] = [
68
            'qty' => 1,
69
            'item_no' => 2,
70
            'item_name' => 'Test 1',
71
            'item_price' => 100,
72
            'vat_rate' => 0.25,
73
        ];
74
        $basket[1] = [
75
            'qty' => 1,
76
            'item_no' => 2,
77
            'item_name' => 'Test 2',
78
            'item_price' => 100,
79
            'vat_rate' => 0.25,
80
        ];
81
82
        $form = [
83
            'currency' => 'DKK',
84
            'order_id' => 1,
85
            'basket' => $basket,
86
        ];
87
88
        $query = $this->crpost($form);
0 ignored issues
show
Bug introduced by
The method crpost() does not seem to exist on object<QuickPay\Tests\RequestTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        print_r(urldecode($query));
90
91
        $payment = $this->request->post('/payments', $form);
0 ignored issues
show
Unused Code introduced by
$payment 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...
92
93
    }
94
95
    /**
96
     * Remove keys of multi array to post
97
     *
98
     * @param array $a
99
     * @param string $b
100
     * @param int $c
101
     * @return bool|string
102
     */
103
    public function handleArrayData($a, $b = '', $c=0)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$c" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$c"; expected 1 but found 0
Loading history...
104
    {
105
        if (!is_array($a)) {
106
            return false;
107
        }
108
        foreach ((array)$a as $k => $v) {
109
            if ($c) {
110
                if(is_numeric($k)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
111
                    $k = $b."[]";
112
                } else {
113
                    $k = $b."[$k]";
114
                }
115
            } else {
116
                if (is_int($k)) {
117
                    $k = $b.$k;
118
                }
119
            }
120
121
            if (is_array($v)||is_object($v)) {
122
                $r[] = $this->crpost($v,$k,1);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$r was never initialized. Although not strictly required by PHP, it is generally a good practice to add $r = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The method crpost() does not seem to exist on object<QuickPay\Tests\RequestTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
                continue;
124
            }
125
            $r[] = urlencode($k)."=".urlencode($v);
0 ignored issues
show
Bug introduced by
The variable $r does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
126
        }
127
        return implode("&", $r);
128
    }
129
}
130