PaymentHandlerTest::testCapture()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Handler;
4
5
use Loevgaard\AltaPay\Client\Client;
6
use Loevgaard\AltaPay\Entity\Transaction;
7
use Loevgaard\AltaPay\Payload\OrderLine;
8
use Loevgaard\AltaPay\Payload\RefundCapturedReservation as RefundCapturedReservationPayload;
9
use Loevgaard\AltaPay\Response\CaptureReservation as CaptureReservationResponse;
10
use Loevgaard\AltaPay\Response\RefundCapturedReservation as RefundCapturedReservationResponse;
11
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
12
use Loevgaard\DandomainAltapayBundle\Entity\PaymentLine;
13
use Loevgaard\DandomainAltapayBundle\Entity\PaymentRepository;
14
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler;
15
use Money\Currency;
16
use Money\Money;
17
use PHPUnit\Framework\TestCase;
18
19
final class PaymentHandlerTest extends TestCase
20
{
21
    public function testCapture()
22
    {
23
        $payment = $this->getPayment();
24
        $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...
25
26
        $altapayClient = $this->getAltapayClient();
27
        $altapayClient
28
            ->method('captureReservation')
29
            ->willReturnCallback(function () {
30
                $response = $this->getMockBuilder(CaptureReservationResponse::class)
31
                    ->disableOriginalConstructor()
32
                    ->getMock()
33
                ;
34
                $response->method('isSuccessful')->willReturn(true);
35
36
                return $response;
37
            })
38
        ;
39
40
        $paymentHandler = $this->getPaymentHandler($altapayClient);
41
        $res = $paymentHandler->capture($payment, new Money(9995, new Currency('DKK')));
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 23 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...ymentHandler::capture() 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...
42
43
        $this->assertInstanceOf(CaptureReservationResponse::class, $res);
44
    }
45
46
    public function testRefund1()
47
    {
48
        $amount = null;
49
        $orderLines = [];
50
51
        $altapayClient = $this->getAltapayClient();
52
        $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\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...
53
            ->expects($this->any())
54
            ->method('refundCapturedReservation')
55
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
56
                /** @var RefundCapturedReservationPayload $val */
57
                $amount = $val->getAmount();
58
                $orderLines = $val->getOrderLines();
59
60
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
61
                    ->disableOriginalConstructor()
62
                    ->getMock()
63
                ;
64
65
                return $response;
66
            })
67
        ;
68
69
        $payment = $this->getPayment();
70
        $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...
71
72
        $paymentHandler = $this->getPaymentHandler($altapayClient);
73
        $paymentHandler->refund($payment);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 69 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...
74
75
        $this->assertSame(null, $amount);
76
        $this->assertSame([], $orderLines);
77
    }
78
79
    public function testRefund2()
80
    {
81
        $amount = null;
82
        $orderLines = [];
83
84
        $altapayClient = $this->getAltapayClient();
85
        $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\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...
86
            ->expects($this->any())
87
            ->method('refundCapturedReservation')
88
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
89
                /** @var RefundCapturedReservationPayload $val */
90
                $amount = $val->getAmount();
91
                $orderLines = $val->getOrderLines();
92
93
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
94
                    ->disableOriginalConstructor()
95
                    ->getMock()
96
                ;
97
                $response->method('isSuccessful')->willReturn(true);
98
99
                $transaction = new Transaction();
100
                $response->method('getTransactions')->willReturn([$transaction]);
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
        $paymentHandler = $this->getPaymentHandler($altapayClient);
110
        $paymentHandler->refund($payment, new Money(10055, new Currency('DKK')));
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...
111
112
        $this->assertEquals(new Money(10055, new Currency('DKK')), $amount);
113
        $this->assertSame([], $orderLines);
114
    }
115
116 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...
117
    {
118
        $amount = null;
119
        $orderLines = [];
120
121
        $altapayClient = $this->getAltapayClient();
122
        $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\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...
123
            ->expects($this->any())
124
            ->method('refundCapturedReservation')
125
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
126
                /** @var RefundCapturedReservationPayload $val */
127
                $amount = $val->getAmount();
128
                $orderLines = $val->getOrderLines();
129
130
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
131
                    ->disableOriginalConstructor()
132
                    ->getMock()
133
                ;
134
135
                return $response;
136
            })
137
        ;
138
139
        $payment = $this->getPayment();
140
        $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...
141
142
        /** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */
143
        $paymentLine = new PaymentLine('productnumber', 'name', 1, new Money('7996', new Currency('DKK')), 25);
144
145
        $paymentHandler = $this->getPaymentHandler($altapayClient);
146
        $paymentHandler->refund($payment, new Money(9995, new Currency('DKK')), [$paymentLine]);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 139 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...
147
148
        $this->assertEquals(new Money(9995, new Currency('DKK')), $amount);
149
150
        /** @var OrderLine $orderLine */
151
        $orderLine = $orderLines[0];
152
        $this->assertEquals(new Money(9995, new Currency('DKK')), $orderLine->getUnitPrice());
153
        $this->assertSame(25.0, $orderLine->getTaxPercent());
154
        $this->assertSame(1.0, $orderLine->getQuantity());
155
        $this->assertSame('name', $orderLine->getDescription());
156
        $this->assertSame('productnumber', $orderLine->getItemId());
157
    }
158
159 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...
160
    {
161
        $amount = null;
162
        $orderLines = [];
163
164
        $altapayClient = $this->getAltapayClient();
165
        $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\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...
166
            ->expects($this->any())
167
            ->method('refundCapturedReservation')
168
            ->willReturnCallback(function ($val) use (&$amount, &$orderLines) {
169
                /** @var RefundCapturedReservationPayload $val */
170
                $amount = $val->getAmount();
171
                $orderLines = $val->getOrderLines();
172
173
                $response = $this->getMockBuilder(RefundCapturedReservationResponse::class)
174
                    ->disableOriginalConstructor()
175
                    ->getMock()
176
                ;
177
178
                return $response;
179
            })
180
        ;
181
182
        $payment = $this->getPayment();
183
        $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...
184
185
        $paymentLine = new PaymentLine('productnumber', 'name', 1, new Money('10000', new Currency('DKK')), 25);
186
187
        $paymentHandler = $this->getPaymentHandler($altapayClient);
188
        $paymentHandler->refund($payment, new Money(8000, new Currency('DKK')), [$paymentLine]);
0 ignored issues
show
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 182 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...
189
190
        $this->assertEquals(new Money(8000, new Currency('DKK')), $amount);
191
192
        /** @var OrderLine $orderLine */
193
        $orderLine = $orderLines[0];
194
        $this->assertEquals(new Money(8000, new Currency('DKK')), $orderLine->getUnitPrice());
195
        $this->assertSame(1.0, $orderLine->getQuantity());
196
        $this->assertSame('refund', $orderLine->getDescription());
197
        $this->assertSame('refund', $orderLine->getItemId());
198
    }
199
200
    /**
201
     * @return Payment|\PHPUnit_Framework_MockObject_MockObject
202
     */
203
    private function getPayment()
204
    {
205
        return $this->getMockForAbstractClass(Payment::class);
206
    }
207
208
    /**
209
     * @return Client|\PHPUnit_Framework_MockObject_MockObject
210
     */
211
    private function getAltapayClient()
212
    {
213
        /** @var Client|\PHPUnit_Framework_MockObject_MockObject $altapayClient */
214
        $altapayClient = $this->getMockBuilder(Client::class)
215
            ->disableOriginalConstructor()
216
            ->getMock()
217
        ;
218
219
        return $altapayClient;
220
    }
221
222
    /**
223
     * @param $altapayClient
224
     *
225
     * @return PaymentHandler
226
     */
227
    private function getPaymentHandler($altapayClient): PaymentHandler
228
    {
229
        /** @var PaymentRepository|\PHPUnit_Framework_MockObject_MockObject $paymentRepository */
230
        $paymentRepository = $this->getMockBuilder(PaymentRepository::class)
231
            ->disableOriginalConstructor()
232
            ->getMock()
233
        ;
234
235
        return new PaymentHandler($altapayClient, $paymentRepository);
0 ignored issues
show
Bug introduced by
It seems like $paymentRepository defined by $this->getMockBuilder(\L...onstructor()->getMock() on line 230 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...tHandler::__construct() does only seem to accept object<Loevgaard\Dandoma...tity\PaymentRepository>, 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...
236
    }
237
}
238