Completed
Push — master ( c55f61...fd7370 )
by Joachim
16:23
created

PaymentHandlerTest::testRefund2()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 32
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 32
loc 32
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Handler;
4
5
use Loevgaard\AltaPay\Client;
6
use Loevgaard\AltaPay\Payload\OrderLine;
7
use Loevgaard\AltaPay\Payload\RefundCapturedReservation as RefundCapturedReservationPayload;
8
use Loevgaard\AltaPay\Response\RefundCapturedReservation as RefundCapturedReservationResponse;
9
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
10
use Loevgaard\DandomainAltapayBundle\Entity\PaymentLine;
11
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler;
12
use Loevgaard\DandomainAltapayBundle\Manager\PaymentManager;
13
use PHPUnit\Framework\TestCase;
14
15
final class PaymentHandlerTest extends TestCase
16
{
17 View Code Duplication
    public function testRefund1()
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...
18
    {
19
        $amount = null;
20
        $orderLines = [];
21
22
        $altapayClient = $this->getAltapayClient();
23
        $altapayClient
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\AltaPay\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
24
            ->expects($this->any())
25
            ->method('refundCapturedReservation')
26
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
27
                /** @var RefundCapturedReservationPayload $val */
28
                $amount = $val->getAmount();
29
                $orderLines = $val->getOrderLines();
30
31
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
32
                    ->disableOriginalConstructor()
33
                    ->getMock()
34
                ;
35
36
                return $response;
37
            })
38
        ;
39
40
        $payment = $this->getPayment();
41
        $payment->setAltapayId('altapayid');
0 ignored issues
show
Bug introduced by
The method setAltapayId does only exist in Loevgaard\DandomainAltapayBundle\Entity\Payment, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
43
        $paymentHandler = $this->getPaymentHandler($altapayClient);
44
        $paymentHandler->refund($payment);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 40 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...aymentHandler::refund() does only seem to accept object<Loevgaard\Dandoma...yBundle\Entity\Payment>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
46
        $this->assertSame(null, $amount);
47
        $this->assertSame([], $orderLines);
48
    }
49
50 View Code Duplication
    public function testRefund2()
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...
51
    {
52
        $amount = null;
53
        $orderLines = [];
54
55
        $altapayClient = $this->getAltapayClient();
56
        $altapayClient
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\AltaPay\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
            ->expects($this->any())
58
            ->method('refundCapturedReservation')
59
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
60
                /** @var RefundCapturedReservationPayload $val */
61
                $amount = $val->getAmount();
62
                $orderLines = $val->getOrderLines();
63
64
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
65
                    ->disableOriginalConstructor()
66
                    ->getMock()
67
                ;
68
69
                return $response;
70
            })
71
        ;
72
73
        $payment = $this->getPayment();
74
        $payment->setAltapayId('altapayid');
0 ignored issues
show
Bug introduced by
The method setAltapayId does only exist in Loevgaard\DandomainAltapayBundle\Entity\Payment, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
76
        $paymentHandler = $this->getPaymentHandler($altapayClient);
77
        $paymentHandler->refund($payment, null, 100.55);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 73 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...aymentHandler::refund() does only seem to accept object<Loevgaard\Dandoma...yBundle\Entity\Payment>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
79
        $this->assertSame(100.55, $amount);
80
        $this->assertSame([], $orderLines);
81
    }
82
83 View Code Duplication
    public function testRefundAmountMatchesPaymentLineAmount()
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...
84
    {
85
        $amount = null;
86
        $orderLines = [];
87
88
        $altapayClient = $this->getAltapayClient();
89
        $altapayClient
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\AltaPay\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
            ->expects($this->any())
91
            ->method('refundCapturedReservation')
92
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
93
                /** @var RefundCapturedReservationPayload $val */
94
                $amount = $val->getAmount();
95
                $orderLines = $val->getOrderLines();
96
97
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
98
                    ->disableOriginalConstructor()
99
                    ->getMock()
100
                ;
101
102
                return $response;
103
            })
104
        ;
105
106
        $payment = $this->getPayment();
107
        $payment->setAltapayId('altapayid');
0 ignored issues
show
Bug introduced by
The method setAltapayId does only exist in Loevgaard\DandomainAltapayBundle\Entity\Payment, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
109
        $paymentLines = [];
0 ignored issues
show
Unused Code introduced by
$paymentLines 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
        /** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */
111
        $paymentLine = $this->getMockForAbstractClass(PaymentLine::class);
112
        $paymentLine->setPrice(79.96)
0 ignored issues
show
Bug introduced by
The method setPrice does only exist in Loevgaard\DandomainAltap...ndle\Entity\PaymentLine, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
113
            ->setVat(25)
114
            ->setQuantity(1)
115
            ->setName('name')
116
            ->setProductNumber('productnumber')
117
        ;
118
119
        $paymentHandler = $this->getPaymentHandler($altapayClient);
120
        $paymentHandler->refund($payment, [$paymentLine], 99.95);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 106 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...aymentHandler::refund() does only seem to accept object<Loevgaard\Dandoma...yBundle\Entity\Payment>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation introduced by
array($paymentLine) is of type array<integer,object<Loe...ckObject_MockObject>"}>, but the function expects a null|array<integer,objec...le\Entity\PaymentLine>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
122
        $this->assertSame(99.95, $amount);
123
124
        /** @var OrderLine $orderLine */
125
        $orderLine = $orderLines[0];
126
        $this->assertSame(99.95, $orderLine->getUnitPrice());
127
        $this->assertSame(25.0, $orderLine->getTaxPercent());
128
        $this->assertSame(1.0, $orderLine->getQuantity());
129
        $this->assertSame('name', $orderLine->getDescription());
130
        $this->assertSame('productnumber', $orderLine->getItemId());
131
    }
132
133 View Code Duplication
    public function testRefundAmountDoesNotMatchPaymentLineAmount()
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...
134
    {
135
        $amount = null;
136
        $orderLines = [];
137
138
        $altapayClient = $this->getAltapayClient();
139
        $altapayClient
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\AltaPay\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
140
            ->expects($this->any())
141
            ->method('refundCapturedReservation')
142
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
143
                /** @var RefundCapturedReservationPayload $val */
144
                $amount = $val->getAmount();
145
                $orderLines = $val->getOrderLines();
146
147
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
148
                    ->disableOriginalConstructor()
149
                    ->getMock()
150
                ;
151
152
                return $response;
153
            })
154
        ;
155
156
        $payment = $this->getPayment();
157
        $payment->setAltapayId('altapayid');
0 ignored issues
show
Bug introduced by
The method setAltapayId does only exist in Loevgaard\DandomainAltapayBundle\Entity\Payment, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
159
        $paymentLines = [];
0 ignored issues
show
Unused Code introduced by
$paymentLines 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...
160
        /** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */
161
        $paymentLine = $this->getMockForAbstractClass(PaymentLine::class);
162
        $paymentLine->setPrice(100)
0 ignored issues
show
Bug introduced by
The method setPrice does only exist in Loevgaard\DandomainAltap...ndle\Entity\PaymentLine, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
163
            ->setVat(25)
164
            ->setQuantity(1)
165
            ->setName('name')
166
            ->setProductNumber('productnumber')
167
        ;
168
169
        $paymentHandler = $this->getPaymentHandler($altapayClient);
170
        $paymentHandler->refund($payment, [$paymentLine], 80);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 156 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...aymentHandler::refund() does only seem to accept object<Loevgaard\Dandoma...yBundle\Entity\Payment>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation introduced by
array($paymentLine) is of type array<integer,object<Loe...ckObject_MockObject>"}>, but the function expects a null|array<integer,objec...le\Entity\PaymentLine>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
171
172
        $this->assertSame(80.0, $amount);
173
174
        /** @var OrderLine $orderLine */
175
        $orderLine = $orderLines[0];
176
        $this->assertSame(80.0, $orderLine->getUnitPrice());
177
        $this->assertSame(1.0, $orderLine->getQuantity());
178
        $this->assertSame('refund', $orderLine->getDescription());
179
        $this->assertSame('refund', $orderLine->getItemId());
180
    }
181
182
    /**
183
     * @return Payment|\PHPUnit_Framework_MockObject_MockObject
184
     */
185
    private function getPayment()
186
    {
187
        return $this->getMockForAbstractClass(Payment::class);
188
    }
189
190
    /**
191
     * @return Client|\PHPUnit_Framework_MockObject_MockObject
192
     */
193
    private function getAltapayClient()
194
    {
195
        /** @var Client|\PHPUnit_Framework_MockObject_MockObject $altapayClient */
196
        $altapayClient = $this->getMockBuilder(Client::class)
197
            ->disableOriginalConstructor()
198
            ->getMock()
199
        ;
200
201
        return $altapayClient;
202
    }
203
204
    /**
205
     * @return PaymentHandler
206
     */
207
    private function getPaymentHandler($altapayClient): PaymentHandler
208
    {
209
        /** @var PaymentManager|\PHPUnit_Framework_MockObject_MockObject $paymentManager */
210
        $paymentManager = $this->getMockBuilder(PaymentManager::class)
211
            ->disableOriginalConstructor()
212
            ->getMock()
213
        ;
214
215
        return new PaymentHandler($altapayClient, $paymentManager);
216
    }
217
}
218