-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathAbstractAdyenTestCase.php
More file actions
137 lines (114 loc) · 5.28 KB
/
AbstractAdyenTestCase.php
File metadata and controls
137 lines (114 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php declare(strict_types=1);
/**
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2022 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Test\Unit;
use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Model\ResourceModel\Order\Payment\Collection;
use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory as OrderPaymentCollectionFactory;
use Magento\Sales\Model\Order as MagentoOrder;
use Magento\Sales\Model\ResourceModel\Order\Status\Collection as OrderStatusCollection;
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory as OrderStatusCollectionFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
abstract class AbstractAdyenTestCase extends TestCase
{
/**
* Create a mock with a mix of methods that already exist and others that do not exist.
* If conditions are requireed so that MockBuilder does not set $this->emptyMethodsArray = 1
* This was done since setMethods is deprecated
*/
protected function createMockWithMethods(string $originalClassName, array $existingMethods, array $nonExistingMethods): MockObject
{
$mockBuilder = $this->getMockBuilder($originalClassName)->disableOriginalConstructor();
if (!empty($existingMethods)) {
$mockBuilder = $mockBuilder->onlyMethods($existingMethods);
}
if (!empty($nonExistingMethods)) {
$mockBuilder = $mockBuilder->addMethods($nonExistingMethods);
}
return $mockBuilder->getMock();
}
/**
* @psalm-template RealInstanceType of object
*
* @psalm-param class-string<RealInstanceType> $originalClassName
*
* @psalm-return MockObject&RealInstanceType
*/
protected function createGeneratedMock(
string $originalClassName,
array $existingMethods = [],
array $nonExistingMethods = []
): MockObject {
$mockBuilder = $this->getMockBuilder($originalClassName);
if (!empty($existingMethods)) {
$mockBuilder = $mockBuilder->onlyMethods($existingMethods);
}
if (!empty($nonExistingMethods)) {
$mockBuilder = $mockBuilder->addMethods($nonExistingMethods);
}
return $mockBuilder->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
}
protected function createOrder(?string $status = null)
{
$orderPaymentMock = $this->createConfiguredMock(MagentoOrder\Payment::class, ['getMethod' => 'adyen_cc']);
return $this->createConfiguredMock(MagentoOrder::class, [
'getStatus' => $status,
'getPayment' => $orderPaymentMock,
]);
}
protected function createWebhook(
?string $originalReference = null,
?string $pspReference = null,
?int $value = 1000
) {
return $this->createConfiguredMock(Notification::class, [
'getAmountValue' => $value,
'getEventCode' => 'AUTHORISATION',
'getAmountCurrency' => 'EUR',
'getOriginalReference' => $originalReference,
'getPspreference' => $pspReference
]);
}
protected function createOrderStatusCollection($state): MockObject
{
$orderStatus = $this->createMockWithMethods(MagentoOrder\Status::class, [], ['getState']);
$orderStatus->method('getState')->willReturn($state);
$orderStatusCollection = $this->createConfiguredMock(OrderStatusCollection::class, []);
$orderStatusCollection->method('addFieldToFilter')->willReturn($orderStatusCollection);
$orderStatusCollection->method('joinStates')->willReturn($orderStatusCollection);
$orderStatusCollection->method('addStateFilter')->willReturn($orderStatusCollection);
$orderStatusCollection->method('getFirstItem')->willReturn($orderStatus);
$orderStatusCollectionFactory = $this->createGeneratedMock(OrderStatusCollectionFactory::class, ['create']);
$orderStatusCollectionFactory->method('create')->willReturn($orderStatusCollection);
return $orderStatusCollectionFactory;
}
protected function createAdyenOrderPaymentCollection(?int $entityId = null): MockObject
{
$adyenOrderPayment = $this->createConfiguredMock(OrderPaymentInterface::class, ['getEntityId' => $entityId]);
$adyenOrderPaymentCollection = $this->createConfiguredMock(Collection::class, [
'getFirstItem' => $adyenOrderPayment
]);
$adyenOrderPaymentCollection->method('addFieldToFilter')->willReturn($adyenOrderPaymentCollection);
$adyenOrderPaymentCollectionFactory = $this->createGeneratedMock(OrderPaymentCollectionFactory::class, ['create']);
$adyenOrderPaymentCollectionFactory->method('create')->willReturn($adyenOrderPaymentCollection);
return $adyenOrderPaymentCollectionFactory;
}
protected function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}