Completed
Push — master ( e54a35...63249b )
by Joachim
14:15
created

PaymentRequestPayloadGeneratorTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 10
dl 0
loc 151
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getPayment() 0 6 1
A getChecksumHelper() 0 6 1
B testCreateOrderLine() 0 37 1
B testCreateCustomerInfo() 0 33 1
A testCreateConfig() 0 21 1
A getGenerator() 0 12 1
A getDandomainPayment() 0 6 1
A getTerminal() 0 4 1
A getRouter() 0 6 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\PayloadGenerator;
4
5
use Loevgaard\AltaPay\Payload\OrderLine as OrderLinePayload;
6
use Loevgaard\AltaPay\Payload\PaymentRequest\Config as ConfigPayload;
7
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo as CustomerInfoPayload;
8
use Loevgaard\Dandomain\Pay\Helper\ChecksumHelper;
9
use Loevgaard\Dandomain\Pay\Model\Payment as DandomainPayment;
10
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
11
use Loevgaard\DandomainAltapayBundle\Entity\Terminal;
12
use Loevgaard\DandomainAltapayBundle\Tests\PayloadGenerator\Fixture\Gateway;
13
use Money\Currency;
14
use Money\Money;
15
use PHPUnit\Framework\TestCase;
16
use Symfony\Component\Routing\RouterInterface;
17
18
final class PaymentRequestPayloadGeneratorTest extends TestCase
19
{
20
    public function testCreateOrderLine()
21
    {
22
        $generator = $this->getGenerator();
23
24
        $description = 'description';
25
        $itemId = 'itemid';
26
        $quantity = 1.0;
27
        $unitPrice = new Money(9995, new Currency('DKK'));
28
29
        // without optional parameters
30
        $payload = $generator->createOrderLine($description, $itemId, $quantity, $unitPrice);
31
        $this->assertInstanceOf(OrderLinePayload::class, $payload);
32
        $this->assertSame($description, $payload->getDescription());
33
        $this->assertSame($itemId, $payload->getItemId());
34
        $this->assertSame($quantity, $payload->getQuantity());
35
        $this->assertEquals($unitPrice, $payload->getUnitPrice());
36
37
        // with tax percent
38
        $taxPercent = 25.0;
39
        $payload = $generator->createOrderLine($description, $itemId, $quantity, $unitPrice, $taxPercent);
40
        $this->assertInstanceOf(OrderLinePayload::class, $payload);
41
        $this->assertSame($description, $payload->getDescription());
42
        $this->assertSame($itemId, $payload->getItemId());
43
        $this->assertSame($quantity, $payload->getQuantity());
44
        $this->assertEquals($unitPrice, $payload->getUnitPrice());
45
        $this->assertSame($taxPercent, $payload->getTaxPercent());
46
47
        // with goods type
48
        $goodsType = OrderLinePayload::GOODS_TYPE_ITEM;
49
        $payload = $generator->createOrderLine($description, $itemId, $quantity, $unitPrice, null, $goodsType);
50
        $this->assertInstanceOf(OrderLinePayload::class, $payload);
51
        $this->assertSame($description, $payload->getDescription());
52
        $this->assertSame($itemId, $payload->getItemId());
53
        $this->assertSame($quantity, $payload->getQuantity());
54
        $this->assertEquals($unitPrice, $payload->getUnitPrice());
55
        $this->assertSame($goodsType, $payload->getGoodsType());
56
    }
57
58
    public function testCreateCustomerInfo()
59
    {
60
        $generator = $this->getGenerator();
61
62
        $billingFirstName = 'billingfirstname';
63
        $billingLastName = 'billinglastname';
64
        $billingAddress = 'billingaddress';
65
        $billingPostal = 'billingpostal';
66
        $billingCity = 'billingcity';
67
        $billingCountry = 'billingcountry';
68
        $shippingFirstName = 'shippingfirstname';
69
        $shippingLastName = 'shippinglastname';
70
        $shippingAddress = 'shippingaddress';
71
        $shippingPostal = 'shippingpostal';
72
        $shippingCity = 'shippingcity';
73
        $shippingCountry = 'shippingcountry';
74
75
        // without optional parameters
76
        $payload = $generator->createCustomerInfo($billingFirstName, $billingLastName, $billingAddress, $billingPostal, $billingCity, $billingCountry, $shippingFirstName, $shippingLastName, $shippingAddress, $shippingPostal, $shippingCity, $shippingCountry);
77
        $this->assertInstanceOf(CustomerInfoPayload::class, $payload);
78
        $this->assertSame($billingFirstName, $payload->getBillingFirstName());
79
        $this->assertSame($billingLastName, $payload->getBillingLastName());
80
        $this->assertSame($billingAddress, $payload->getBillingAddress());
81
        $this->assertSame($billingPostal, $payload->getBillingPostal());
82
        $this->assertSame($billingCity, $payload->getBillingCity());
83
        $this->assertSame($billingCountry, $payload->getBillingCountry());
84
        $this->assertSame($shippingFirstName, $payload->getShippingFirstName());
85
        $this->assertSame($shippingLastName, $payload->getShippingLastName());
86
        $this->assertSame($shippingAddress, $payload->getShippingAddress());
87
        $this->assertSame($shippingPostal, $payload->getShippingPostal());
88
        $this->assertSame($shippingCity, $payload->getShippingCity());
89
        $this->assertSame($shippingCountry, $payload->getShippingCountry());
90
    }
91
92
    public function testCreateConfig()
93
    {
94
        $generator = $this->getGenerator();
95
96
        $callbackForm = 'form';
97
        $callbackOk = 'ok';
98
        $callbackFail = 'fail';
99
        $callbackRedirect = 'redirect';
100
        $callbackOpen = 'open';
101
        $callbackNotification = 'notification';
102
103
        // without optional parameters
104
        $payload = $generator->createConfig($callbackForm, $callbackOk, $callbackFail, $callbackRedirect, $callbackOpen, $callbackNotification);
105
        $this->assertInstanceOf(ConfigPayload::class, $payload);
106
        $this->assertSame($callbackForm, $payload->getCallbackForm());
107
        $this->assertSame($callbackOk, $payload->getCallbackOk());
108
        $this->assertSame($callbackFail, $payload->getCallbackFail());
109
        $this->assertSame($callbackRedirect, $payload->getCallbackRedirect());
110
        $this->assertSame($callbackOpen, $payload->getCallbackOpen());
111
        $this->assertSame($callbackNotification, $payload->getCallbackNotification());
112
    }
113
114
    private function getGenerator()
115
    {
116
        $router = $this->getRouter();
117
        $dandomainPayment = $this->getDandomainPayment();
118
        $terminal = $this->getTerminal();
119
        $payment = $this->getPayment();
120
        $handler = $this->getChecksumHelper($dandomainPayment);
121
122
        $generator = new Gateway($router, $dandomainPayment, $terminal, $payment, $handler, 'payment_id', 'checksum_complete');
0 ignored issues
show
Bug introduced by
It seems like $router defined by $this->getRouter() on line 116 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...enerator::__construct() does only seem to accept object<Symfony\Component\Routing\RouterInterface>, 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...
Bug introduced by
It seems like $payment defined by $this->getPayment() on line 119 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...enerator::__construct() 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...
123
124
        return $generator;
125
    }
126
127
    /**
128
     * @return \PHPUnit_Framework_MockObject_MockObject|RouterInterface
129
     */
130
    private function getRouter()
131
    {
132
        $router = $this->createMock(RouterInterface::class);
133
134
        return $router;
135
    }
136
137
    private function getDandomainPayment()
138
    {
139
        $paymentRequest = new DandomainPayment();
140
141
        return $paymentRequest;
142
    }
143
144
    /**
145
     * @return Terminal
146
     */
147
    private function getTerminal()
148
    {
149
        return new Terminal();
150
    }
151
152
    /**
153
     * @return Payment|\PHPUnit_Framework_MockObject_MockObject
154
     */
155
    private function getPayment()
156
    {
157
        $payment = $this->getMockForAbstractClass(Payment::class);
158
159
        return $payment;
160
    }
161
162
    private function getChecksumHelper(DandomainPayment $payment)
163
    {
164
        $checksumHelper = new ChecksumHelper($payment, 'sharedkey1', 'sharedkey2');
165
166
        return $checksumHelper;
167
    }
168
}
169