-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathVenmoCheckoutDataServiceTest.php
155 lines (131 loc) · 5.67 KB
/
VenmoCheckoutDataServiceTest.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\PayPal\Test\Storefront\Data\Service;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\Country\Aggregate\CountryState\CountryStateEntity;
use Shopware\Core\System\Country\CountryEntity;
use Shopware\Core\System\Currency\CurrencyEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Test\Generator;
use Swag\PayPal\Checkout\SalesChannel\CustomerVaultTokenRoute;
use Swag\PayPal\Checkout\TokenResponse;
use Swag\PayPal\Setting\Service\CredentialsUtilInterface;
use Swag\PayPal\Setting\Settings;
use Swag\PayPal\Storefront\Data\Service\VenmoCheckoutDataService;
use Swag\PayPal\Test\Helper\CartTrait;
use Swag\PayPal\Test\Helper\ServicesTrait;
use Swag\PayPal\Test\Mock\Setting\Service\SystemConfigServiceMock;
use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
use Swag\PayPal\Util\Lifecycle\Method\VenmoMethodData;
use Swag\PayPal\Util\LocaleCodeProvider;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Routing\RouterInterface;
/**
* @internal
*/
#[Package('checkout')]
#[CoversClass(VenmoCheckoutDataService::class)]
class VenmoCheckoutDataServiceTest extends TestCase
{
use CartTrait;
use ServicesTrait;
protected VenmoCheckoutDataService $checkoutDataService;
protected SystemConfigServiceMock $systemConfigService;
protected CustomerVaultTokenRoute&MockObject $customerVaultTokenRoute;
protected PaymentMethodDataRegistry&MockObject $paymentMethodDataRegistry;
protected function setUp(): void
{
$this->systemConfigService = $this->createSystemConfigServiceMock([
Settings::VAULTING_ENABLED_VENMO => false,
]);
$this->customerVaultTokenRoute = $this->createMock(CustomerVaultTokenRoute::class);
$this->paymentMethodDataRegistry = $this->createMock(PaymentMethodDataRegistry::class);
$container = $this->createMock(Container::class);
$container
->method('get')
->willReturnCallback(function (string $service): mixed {
return match ($service) {
SystemConfigService::class => $this->systemConfigService,
default => static::fail('Getting service "' . $service . '" is not handled'),
};
});
$this->paymentMethodDataRegistry
->expects(static::once())
->method('getPaymentMethod')
->with(VenmoMethodData::class)
->willReturn(new VenmoMethodData($container));
$this->checkoutDataService = new VenmoCheckoutDataService(
$this->paymentMethodDataRegistry,
$this->createMock(LocaleCodeProvider::class),
$this->createMock(RouterInterface::class),
$this->systemConfigService,
$this->createMock(CredentialsUtilInterface::class),
$this->customerVaultTokenRoute,
);
}
#[DataProvider('providerTestSetUserIdToken')]
public function testSetUserIdToken(?string $expected, bool $guest, bool $enabled): void
{
$this->systemConfigService->set(Settings::VAULTING_ENABLED_VENMO, $enabled);
$context = Generator::createSalesChannelContext(currency: $this->createCurrency(), customer: $this->createCustomer($guest));
$this->customerVaultTokenRoute
->expects(static::exactly((int) (!$guest && $enabled)))
->method('getVaultToken')
->willReturn(new TokenResponse('user-id-token'));
$data = $this->checkoutDataService->buildCheckoutData(
$context,
$this->createCart(Uuid::randomHex()),
);
static::assertSame($expected, $data?->getUserIdToken());
}
public static function providerTestSetUserIdToken(): \Generator
{
yield 'non-guest, setting enabled' => ['user-id-token', false, true];
yield 'guest, setting enabled' => [null, true, true];
yield 'non-guest, setting disabled' => [null, false, false];
yield 'guest, setting disabled' => [null, true, false];
}
private function createCustomer(bool $guest): CustomerEntity
{
$customer = new CustomerEntity();
$customer->setId(Uuid::randomHex());
$customer->setGuest($guest);
$customer->setActiveBillingAddress($this->createCustomerAddress());
return $customer;
}
private function createCurrency(): CurrencyEntity
{
$currency = new CurrencyEntity();
$currency->setId(Uuid::randomHex());
$currency->setIsoCode('EUR');
return $currency;
}
private function createCustomerAddress(): CustomerAddressEntity
{
$country = new CountryEntity();
$country->setIso3('DEU');
$country->setIso('DE');
$countryState = new CountryStateEntity();
$countryState->setName('Test Country State');
$address = new CustomerAddressEntity();
$address->setStreet('Test street');
$address->setCountryState($countryState);
$address->setCountry($country);
$address->setFirstName('Max');
$address->setLastName('Mustermann');
$address->setCity('Testhausen');
$address->setZipcode('44444');
return $address;
}
}