diff --git a/tests/Components/Ratepay/Installment/InstallmentServiceTest.php b/tests/Components/Ratepay/Installment/InstallmentServiceTest.php index a4437ba0e..2200d0b9d 100644 --- a/tests/Components/Ratepay/Installment/InstallmentServiceTest.php +++ b/tests/Components/Ratepay/Installment/InstallmentServiceTest.php @@ -37,7 +37,7 @@ public function testItReturnsDefaultCalculatorData(): void $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); $installmentService = $this->getInstallmentService(); - $this->fillCart($salesChannelContext->getToken(), 100); + $this->fillCart($salesChannelContext, 100); $calculatorData = $installmentService->getInstallmentCalculatorData($salesChannelContext); @@ -69,7 +69,7 @@ public function testItReturnsCalculatorDataByTime(): void $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); $installmentService = $this->getInstallmentService(); - $this->fillCart($salesChannelContext->getToken(), 100); + $this->fillCart($salesChannelContext, 100); $dataBag = new RequestDataBag([ 'ratepayInstallmentType' => CalculationRequestParameterBuilder::INSTALLMENT_TYPE_TIME, @@ -106,7 +106,7 @@ public function testItReturnsCalculatorDataByRate(): void $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); $installmentService = $this->getInstallmentService(); - $this->fillCart($salesChannelContext->getToken(), 100); + $this->fillCart($salesChannelContext, 100); $dataBag = new RequestDataBag([ 'ratepayInstallmentType' => CalculationRequestParameterBuilder::INSTALLMENT_TYPE_RATE, diff --git a/tests/Components/SecuredInstallment/InstallmentServiceTest.php b/tests/Components/SecuredInstallment/InstallmentServiceTest.php index c82106da1..70bc3b439 100644 --- a/tests/Components/SecuredInstallment/InstallmentServiceTest.php +++ b/tests/Components/SecuredInstallment/InstallmentServiceTest.php @@ -25,7 +25,7 @@ public function testItReturnsInstallmentOptions(): void $installmentService = $this->getInstallmentService($client); $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $this->fillCart($salesChannelContext->getToken(), 510.50); + $this->fillCart($salesChannelContext); $installmentOptions = $installmentService->getInstallmentOptions($salesChannelContext); @@ -61,7 +61,7 @@ public function testItReturnsInstallmentOptionsWithout0Index(): void $installmentService = $this->getInstallmentService($client); $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $this->fillCart($salesChannelContext->getToken(), 510.50); + $this->fillCart($salesChannelContext); $installmentOptions = $installmentService->getInstallmentOptions($salesChannelContext); @@ -97,7 +97,7 @@ public function testItReturnsEmptyOptionsOnFailedRequest(): void $installmentService = $this->getInstallmentService($client); $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $this->fillCart($salesChannelContext->getToken(), 510.50); + $this->fillCart($salesChannelContext); $installmentOptions = $installmentService->getInstallmentOptions($salesChannelContext); diff --git a/tests/Constants.php b/tests/Constants.php index 327684adb..db8a86690 100644 --- a/tests/Constants.php +++ b/tests/Constants.php @@ -27,4 +27,8 @@ interface Constants public const PAYONE_TRANSACTION_ID = 'test-transaction-id'; public const COUNTRY_ID = 'ea3a25f690f848359a4d64c1c46077ea'; public const SALUTATION_ID = 'c954f5baf0894e95a7ed8b172e59b145'; + + public const DEFAULT_PRODUCT_PRICE = 119; + + public const CUSTOMER_ID = '8c7b6ac55c4647bd997451fa3892aa8d'; } diff --git a/tests/EventListener/CheckoutConfirmRatepayEventListenerTest.php b/tests/EventListener/CheckoutConfirmRatepayEventListenerTest.php index cf49888be..d9e0bd02e 100644 --- a/tests/EventListener/CheckoutConfirmRatepayEventListenerTest.php +++ b/tests/EventListener/CheckoutConfirmRatepayEventListenerTest.php @@ -113,7 +113,7 @@ public function testItFiltersPaymentMethodsByProfilesOnCheckoutConfirmPage(strin $this->setPaymentMethods($page); $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $this->fillCart($salesChannelContext->getToken(), $cartValue); + $this->fillCart($salesChannelContext, $cartValue); $event = new CheckoutConfirmPageLoadedEvent($page, $salesChannelContext, new Request()); $listener = $this->getContainer()->get(CheckoutConfirmRatepayEventListener::class); diff --git a/tests/Functional/Payment/AmazonPayExpress/CreateCheckoutSessionRequestParametersTest.php b/tests/Functional/Payment/AmazonPayExpress/CreateCheckoutSessionRequestParametersTest.php index 863c27055..0d49139b7 100644 --- a/tests/Functional/Payment/AmazonPayExpress/CreateCheckoutSessionRequestParametersTest.php +++ b/tests/Functional/Payment/AmazonPayExpress/CreateCheckoutSessionRequestParametersTest.php @@ -6,6 +6,7 @@ use PayonePayment\Components\ConfigReader\ConfigReader; use PayonePayment\Components\GenericExpressCheckout\Struct\CreateExpressCheckoutSessionStruct; +use PayonePayment\Constants; use PayonePayment\PaymentHandler\PayoneAmazonPayExpressPaymentHandler; use PayonePayment\Payone\RequestParameter\RequestParameterFactory; use PayonePayment\TestCaseBase\PayoneTestBehavior; @@ -28,7 +29,12 @@ public function testIfParametersGotCreatedSuccessfulWithoutRestriction(): void $context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); // cart got stored automatically in the CartService, which is reused in the plugin - $this->createCartWithProduct($context, 200, 2); + $this->createCartWithProduct($context); + + /** @var SystemConfigService $configService */ + $configService = $this->getContainer()->get(SystemConfigService::class); + $configService->set(ConfigReader::getConfigKeyByPaymentHandler(PayoneAmazonPayExpressPaymentHandler::class, 'RestrictPOBoxes'), false); + $configService->set(ConfigReader::getConfigKeyByPaymentHandler(PayoneAmazonPayExpressPaymentHandler::class, 'RestrictPackstations'), false); $requestParams = $factory->getRequestParameter(new CreateExpressCheckoutSessionStruct( $context, @@ -42,7 +48,7 @@ public function testIfParametersGotCreatedSuccessfulWithoutRestriction(): void static::assertArrayHasKey('wallettype', $requestParams); static::assertEquals('AMP', $requestParams['wallettype']); static::assertArrayHasKey('amount', $requestParams); - static::assertEquals(400 * 100, $requestParams['amount']); + static::assertEquals(Constants::DEFAULT_PRODUCT_PRICE * 100, $requestParams['amount']); static::assertArrayHasKey('currency', $requestParams); static::assertArrayHasKey('add_paydata[action]', $requestParams); static::assertEquals('createCheckoutSessionPayload', $requestParams['add_paydata[action]']); @@ -74,7 +80,7 @@ public function testIfParametersGotCreatedSuccessfulWithRestriction(array $restr } // cart got stored automatically in the CartService, which is reused in the plugin - $this->createCartWithProduct($context, 200, 2); + $this->createCartWithProduct($context); $requestParams = $factory->getRequestParameter(new CreateExpressCheckoutSessionStruct( $context, @@ -86,7 +92,7 @@ public function testIfParametersGotCreatedSuccessfulWithRestriction(array $restr static::assertArrayHasKey('wallettype', $requestParams); static::assertEquals('AMP', $requestParams['wallettype']); static::assertArrayHasKey('amount', $requestParams); - static::assertEquals(400 * 100, $requestParams['amount']); + static::assertEquals(Constants::DEFAULT_PRODUCT_PRICE * 100, $requestParams['amount']); static::assertArrayHasKey('currency', $requestParams); static::assertArrayHasKey('add_paydata[action]', $requestParams); static::assertEquals('createCheckoutSessionPayload', $requestParams['add_paydata[action]']); diff --git a/tests/Functional/Payment/AmazonPayExpress/UpdateCheckoutSessionParameterBuilderTest.php b/tests/Functional/Payment/AmazonPayExpress/UpdateCheckoutSessionParameterBuilderTest.php index a5ff810a4..1c780616c 100644 --- a/tests/Functional/Payment/AmazonPayExpress/UpdateCheckoutSessionParameterBuilderTest.php +++ b/tests/Functional/Payment/AmazonPayExpress/UpdateCheckoutSessionParameterBuilderTest.php @@ -21,7 +21,7 @@ public function testIfSuccessful(): void $context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); // cart got stored automatically in the CartService, which is reused in the plugin - $this->createCartWithProduct($context, 200, 2); + $this->createCartWithProduct($context); $struct = new AmazonPayExpressUpdateCheckoutSessionStruct($context, 'test-123'); diff --git a/tests/Functional/Payment/PaypalExpress/CreateCheckoutSessionRequestParametersTest.php b/tests/Functional/Payment/PaypalExpress/CreateCheckoutSessionRequestParametersTest.php index 300a167cb..78d5f1964 100644 --- a/tests/Functional/Payment/PaypalExpress/CreateCheckoutSessionRequestParametersTest.php +++ b/tests/Functional/Payment/PaypalExpress/CreateCheckoutSessionRequestParametersTest.php @@ -5,6 +5,7 @@ namespace PayonePayment\Functional\Payment\PaypalExpress; use PayonePayment\Components\GenericExpressCheckout\Struct\CreateExpressCheckoutSessionStruct; +use PayonePayment\Constants; use PayonePayment\PaymentHandler\PayonePaypalExpressPaymentHandler; use PayonePayment\Payone\RequestParameter\RequestParameterFactory; use PayonePayment\TestCaseBase\PayoneTestBehavior; @@ -25,7 +26,7 @@ public function testIfParametersGotCreatedSuccessful(): void $context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); // cart got stored automatically in the CartService, which is reused in the plugin - $this->createCartWithProduct($context, 200, 2); + $this->createCartWithProduct($context); $requestParams = $factory->getRequestParameter(new CreateExpressCheckoutSessionStruct( $context, @@ -37,7 +38,7 @@ public function testIfParametersGotCreatedSuccessful(): void static::assertArrayHasKey('wallettype', $requestParams); static::assertEquals('PPE', $requestParams['wallettype']); static::assertArrayHasKey('amount', $requestParams); - static::assertEquals(400 * 100, $requestParams['amount']); + static::assertEquals(Constants::DEFAULT_PRODUCT_PRICE * 100, $requestParams['amount']); static::assertArrayHasKey('currency', $requestParams); static::assertArrayHasKey('add_paydata[action]', $requestParams); static::assertEquals('setexpresscheckout', $requestParams['add_paydata[action]']); diff --git a/tests/Payone/RequestParameter/Builder/Klarna/CreateSessionRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/Klarna/CreateSessionRequestParameterBuilderTest.php index 177beb86e..2fbb95fb0 100644 --- a/tests/Payone/RequestParameter/Builder/Klarna/CreateSessionRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/Klarna/CreateSessionRequestParameterBuilderTest.php @@ -17,7 +17,7 @@ public function testItAddsCorrectCreateSessionParameters(): void { $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $this->createCartWithProduct($salesChannelContext, 123.45, 4); + $this->createCartWithProduct($salesChannelContext); $struct = new KlarnaCreateSessionStruct($salesChannelContext, PayoneKlarnaInvoicePaymentHandler::class); diff --git a/tests/Payone/RequestParameter/Builder/PayolutionInstallment/PreCheckRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/PayolutionInstallment/PreCheckRequestParameterBuilderTest.php index d30239ff3..7899831c1 100644 --- a/tests/Payone/RequestParameter/Builder/PayolutionInstallment/PreCheckRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/PayolutionInstallment/PreCheckRequestParameterBuilderTest.php @@ -5,6 +5,7 @@ namespace PayonePayment\Payone\RequestParameter\Builder\PayolutionInstallment; use DMS\PHPUnitExtensions\ArraySubset\Assert; +use PayonePayment\Constants; use PayonePayment\PaymentHandler\AbstractPayonePaymentHandler; use PayonePayment\PaymentHandler\PayonePayolutionInstallmentPaymentHandler; use PayonePayment\PaymentHandler\PayoneRatepayDebitPaymentHandler; @@ -81,7 +82,7 @@ public function testItAddsCorrectPreCheckParameters(): void 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PYS, 'add_paydata[action]' => 'pre_check', 'add_paydata[payment_type]' => 'Payolution-Installment', - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', 'workorderid' => '', 'birthday' => '20000101', @@ -95,7 +96,7 @@ protected function getPayolutionAdditionalActionStruct( string $requestAction = AbstractRequestParameterBuilder::REQUEST_ACTION_PAYOLUTION_PRE_CHECK ): PayolutionAdditionalActionStruct { $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $cart = $this->fillCart($salesChannelContext->getToken(), 100); + $cart = $this->fillCart($salesChannelContext); $dataBag = new RequestDataBag([ 'payoneBirthday' => '2000-01-01', diff --git a/tests/Payone/RequestParameter/Builder/PayolutionInvoicing/PreCheckRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/PayolutionInvoicing/PreCheckRequestParameterBuilderTest.php index 3e9357c8f..63d5f9963 100644 --- a/tests/Payone/RequestParameter/Builder/PayolutionInvoicing/PreCheckRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/PayolutionInvoicing/PreCheckRequestParameterBuilderTest.php @@ -5,6 +5,7 @@ namespace PayonePayment\Payone\RequestParameter\Builder\PayolutionInvoicing; use DMS\PHPUnitExtensions\ArraySubset\Assert; +use PayonePayment\Constants; use PayonePayment\Installer\ConfigInstaller; use PayonePayment\PaymentHandler\AbstractPayonePaymentHandler; use PayonePayment\PaymentHandler\PayonePayolutionInvoicingPaymentHandler; @@ -82,7 +83,7 @@ public function testItAddsCorrectPreCheckParameters(): void 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PYV, 'add_paydata[action]' => 'pre_check', 'add_paydata[payment_type]' => 'Payolution-Invoicing', - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', 'workorderid' => '', 'birthday' => '20000101', @@ -112,7 +113,7 @@ public function testItAddsCorrectCompanyParametersByBillingAddress(): void 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PYV, 'add_paydata[action]' => 'pre_check', 'add_paydata[payment_type]' => 'Payolution-Invoicing', - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', 'workorderid' => '', 'birthday' => '20000101', @@ -144,7 +145,7 @@ public function testItAddsCorrectCompanyParametersByCustomer(): void 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PYV, 'add_paydata[action]' => 'pre_check', 'add_paydata[payment_type]' => 'Payolution-Invoicing', - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', 'workorderid' => '', 'birthday' => '20000101', @@ -179,7 +180,7 @@ protected function getPayolutionAdditionalActionStruct( string $requestAction = AbstractRequestParameterBuilder::REQUEST_ACTION_PAYOLUTION_PRE_CHECK ): PayolutionAdditionalActionStruct { $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $cart = $this->fillCart($salesChannelContext->getToken(), 100); + $cart = $this->fillCart($salesChannelContext); $dataBag = new RequestDataBag([ 'payoneBirthday' => '2000-01-01', diff --git a/tests/Payone/RequestParameter/Builder/RatepayInstallment/CalculationRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/RatepayInstallment/CalculationRequestParameterBuilderTest.php index 6fb0f6d87..edbdc3777 100644 --- a/tests/Payone/RequestParameter/Builder/RatepayInstallment/CalculationRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/RatepayInstallment/CalculationRequestParameterBuilderTest.php @@ -6,6 +6,7 @@ use DMS\PHPUnitExtensions\ArraySubset\Assert; use PayonePayment\Components\Ratepay\Profile\ProfileService; +use PayonePayment\Constants; use PayonePayment\PaymentHandler\AbstractPayonePaymentHandler; use PayonePayment\PaymentHandler\PayoneRatepayDebitPaymentHandler; use PayonePayment\PaymentHandler\PayoneRatepayInstallmentPaymentHandler; @@ -95,7 +96,7 @@ public function testItAddsTheInstallmentCalculationRequestParametersByRate(): vo 'add_paydata[customer_allow_credit_inquiry]' => 'yes', 'add_paydata[calculation_type]' => 'calculation-by-rate', 'add_paydata[rate]' => 1000, - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', ], $parameters @@ -119,7 +120,7 @@ public function testItAddsTheInstallmentCalculationRequestParametersByTime(): vo 'add_paydata[customer_allow_credit_inquiry]' => 'yes', 'add_paydata[calculation_type]' => 'calculation-by-time', 'add_paydata[month]' => 10, - 'amount' => 10000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', ], $parameters @@ -133,7 +134,7 @@ protected function getRatepayCalculationStruct( string $requestAction = AbstractRequestParameterBuilder::REQUEST_ACTION_RATEPAY_CALCULATION ): RatepayCalculationStruct { $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $cart = $this->fillCart($salesChannelContext->getToken(), 100); + $cart = $this->fillCart($salesChannelContext); $profile = $this->getContainer()->get(ProfileService::class)->getProfileBySalesChannelContext( $salesChannelContext, $paymentHandler diff --git a/tests/Payone/RequestParameter/Builder/SecuredInstallment/InstallmentOptionsRequestParameterBuilderTest.php b/tests/Payone/RequestParameter/Builder/SecuredInstallment/InstallmentOptionsRequestParameterBuilderTest.php index a534d607b..2ecb78f89 100644 --- a/tests/Payone/RequestParameter/Builder/SecuredInstallment/InstallmentOptionsRequestParameterBuilderTest.php +++ b/tests/Payone/RequestParameter/Builder/SecuredInstallment/InstallmentOptionsRequestParameterBuilderTest.php @@ -5,6 +5,7 @@ namespace PayonePayment\Payone\RequestParameter\Builder\SecuredInstallment; use DMS\PHPUnitExtensions\ArraySubset\Assert; +use PayonePayment\Constants; use PayonePayment\PaymentHandler\AbstractPayonePaymentHandler; use PayonePayment\PaymentHandler\PayoneSecuredInstallmentPaymentHandler; use PayonePayment\PaymentHandler\PayoneSecuredInvoicePaymentHandler; @@ -76,7 +77,7 @@ public function testItAddsTheInstallmentOptionsRequestParameters(): void 'financingtype' => AbstractPayonePaymentHandler::PAYONE_FINANCING_PIN, 'add_paydata[action]' => 'installment_options', 'add_paydata[businessRelation]' => 'b2c', - 'amount' => 30000, + 'amount' => Constants::DEFAULT_PRODUCT_PRICE * 100, 'currency' => 'EUR', ], $parameters @@ -88,7 +89,7 @@ protected function getSecuredInstallmentOptionsStruct( string $requestAction = AbstractRequestParameterBuilder::REQUEST_ACTION_SECURED_INSTALLMENT_OPTIONS ): SecuredInstallmentOptionsStruct { $salesChannelContext = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); - $cart = $this->fillCart($salesChannelContext->getToken(), 300); + $cart = $this->fillCart($salesChannelContext); $dataBag = new RequestDataBag([]); diff --git a/tests/TestCaseBase/PayoneTestBehavior.php b/tests/TestCaseBase/PayoneTestBehavior.php index 30c82437e..e055bca54 100644 --- a/tests/TestCaseBase/PayoneTestBehavior.php +++ b/tests/TestCaseBase/PayoneTestBehavior.php @@ -31,9 +31,7 @@ use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity; use Shopware\Core\Checkout\Order\OrderEntity; use Shopware\Core\Checkout\Payment\PaymentMethodEntity; -use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; use Shopware\Core\Defaults; -use Shopware\Core\Framework\Context; use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour; use Shopware\Core\Framework\Test\TestCaseBase\SessionTestBehaviour; use Shopware\Core\Framework\Uuid\Uuid; @@ -55,11 +53,14 @@ trait PayoneTestBehavior /** * @deprecated use `createCartWithProduct` */ - protected function fillCart(string $contextToken, float $totalPrice): Cart + protected function fillCart(SalesChannelContext $context, ?float $totalPrice = Constants::DEFAULT_PRODUCT_PRICE): Cart { - $cart = static::getContainer()->get(CartService::class)->createNew($contextToken); + $cart = static::getContainer()->get(CartService::class)->createNew($context->getToken()); + + $productId = $this->getRandomProduct($context, 1, false, $totalPrice ? [ + 'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => $totalPrice, 'net' => $totalPrice / 1.19, 'linked' => false]], + ] : [])->getId(); - $productId = $this->createProduct($totalPrice); $cart->add(new LineItem('lineItem1', LineItem::PRODUCT_LINE_ITEM_TYPE, $productId)); $cart->setPrice($this->createPrice($totalPrice)); @@ -70,14 +71,16 @@ protected function fillCart(string $contextToken, float $totalPrice): Cart /** * creates a cart with calculated sums */ - protected function createCartWithProduct(SalesChannelContext $context, float $itemPrice, int $qty): Cart + protected function createCartWithProduct(SalesChannelContext $context, float $itemPrice = Constants::DEFAULT_PRODUCT_PRICE, int $qty = 1): Cart { $cartService = static::getContainer()->get(CartService::class); $cartItemCalculator = static::getContainer()->get(Calculator::class); $cart = $cartService->createNew($context->getToken()); - $productId = $this->createProduct($itemPrice); + $productId = $this->getRandomProduct($context, $qty, false, [ + 'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => $itemPrice, 'net' => $itemPrice / 1.19, 'linked' => false]], + ])->getId(); $lineItem = (new LineItem('lineItem1', LineItem::PRODUCT_LINE_ITEM_TYPE, $productId, $qty)) ->setPriceDefinition(new QuantityPriceDefinition($itemPrice, new TaxRuleCollection([]), $qty)) @@ -92,37 +95,6 @@ protected function createCartWithProduct(SalesChannelContext $context, float $it return $cart; } - protected function createProduct(float $price): string - { - $productId = Uuid::randomHex(); - - $product = [ - 'id' => $productId, - 'name' => 'Test product', - 'productNumber' => $productId, - 'stock' => 1, - 'price' => [ - ['currencyId' => Defaults::CURRENCY, 'gross' => $price, 'net' => $price, 'linked' => false], - ], - 'manufacturer' => ['id' => $productId, 'name' => 'shopware AG'], - 'tax' => ['id' => $productId, 'name' => 'testTaxRate', 'taxRate' => 0], - 'categories' => [ - ['id' => $productId, 'name' => 'Test category'], - ], - 'visibilities' => [ - [ - 'id' => $productId, - 'salesChannelId' => TestDefaults::SALES_CHANNEL, - 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL, - ], - ], - ]; - - static::getContainer()->get('product.repository')->create([$product], Context::createDefaultContext()); - - return $productId; - } - protected function createPrice(float $price): CartPrice { return new CartPrice( diff --git a/tests/TestCaseBase/StorefrontPageTestBehaviour.php b/tests/TestCaseBase/StorefrontPageTestBehaviour.php index 046b005a8..fed7a0c5b 100644 --- a/tests/TestCaseBase/StorefrontPageTestBehaviour.php +++ b/tests/TestCaseBase/StorefrontPageTestBehaviour.php @@ -4,6 +4,7 @@ namespace PayonePayment\TestCaseBase; +use PayonePayment\Constants; use PHPUnit\Framework\TestCase; use Shopware\Core\Checkout\Cart\CartRuleLoader; use Shopware\Core\Checkout\Cart\LineItem\LineItem; @@ -14,7 +15,9 @@ use Shopware\Core\Defaults; use Shopware\Core\Framework\Api\Util\AccessKeyHelper; use Shopware\Core\Framework\Context; +use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; +use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Routing\RoutingException; use Shopware\Core\Framework\Struct\Struct; use Shopware\Core\Framework\Test\TestCaseBase\TaxAddToSalesChannelTestBehaviour; @@ -24,6 +27,7 @@ use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService; use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository; use Shopware\Core\System\SalesChannel\SalesChannelContext; +use Shopware\Core\System\SalesChannel\SalesChannelEntity; use Shopware\Core\Test\TestDefaults; use Shopware\Storefront\Page\PageLoadedEvent; use Shopware\Storefront\Pagelet\PageletLoadedEvent; @@ -99,23 +103,30 @@ protected function placeRandomOrder(SalesChannelContext $context): string */ protected function getRandomProduct(SalesChannelContext $context, ?int $stock = 1, ?bool $isCloseout = false, array $config = []): ProductEntity { - $id = Uuid::randomHex(); - $productNumber = Uuid::randomHex(); + $productNumber = 'phpunit-' . md5(json_encode([$stock, $isCloseout, $config])); + $productRepository = $this->getContainer()->get('product.repository'); + $criteria = new Criteria(); + $criteria->addFilter(new EqualsFilter('productNumber', $productNumber)); + $criteria->setLimit(1); + $product = $productRepository->search($criteria, $context->getContext())->first(); + + if ($product instanceof ProductEntity) { + return $product; + } + + $id = Uuid::randomHex(); + $itemPrice = Constants::DEFAULT_PRODUCT_PRICE; $data = [ 'id' => $id, 'productNumber' => $productNumber, 'stock' => $stock, 'name' => StorefrontPageTestConstants::PRODUCT_NAME, - 'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false]], - 'manufacturer' => ['name' => 'test'], - 'tax' => ['id' => Uuid::randomHex(), 'name' => 'test', 'taxRate' => 15], + 'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => $itemPrice, 'net' => $itemPrice / 1.19, 'linked' => false]], + 'tax' => ['id' => '2abf31f1c4d94effb0e42c9bdcab0dd0', 'name' => 'test', 'taxRate' => 19], 'active' => true, 'isCloseout' => $isCloseout, - 'categories' => [ - ['id' => Uuid::randomHex(), 'name' => 'asd'], - ], 'visibilities' => [ ['salesChannelId' => $context->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL], ], @@ -143,6 +154,7 @@ protected function createSalesChannelContextWithNavigation(): SalesChannelContex $countryId = $this->getValidCountryId(); $snippetSetId = $this->getSnippetSetIdForLocale('en-GB'); $data = [ + 'id' => 'd70c06716f8848b685f8faa7ff1bbeae', 'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT, 'name' => 'store front', 'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'), @@ -178,6 +190,7 @@ protected function createSalesChannelContextWithLoggedInCustomerAndWithNavigatio $countryId = $this->getValidCountryId(); $snippetSetId = $this->getSnippetSetIdForLocale('en-GB'); $data = [ + 'id' => '5339bf0dc99f4579a69119a021453085', 'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT, 'name' => 'store front', 'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'), @@ -207,16 +220,14 @@ protected function createSalesChannelContextWithLoggedInCustomerAndWithNavigatio ]); } - /** - * @param array|null $salesChannelData - */ - protected function createSalesChannelContext(?array $salesChannelData = null): SalesChannelContext + protected function createSalesChannelContext(): SalesChannelContext { $paymentMethodId = $this->getValidPaymentMethodId(); $shippingMethodId = $this->getAvailableShippingMethod()->getId(); $countryId = $this->getValidCountryId(); $snippetSetId = $this->getSnippetSetIdForLocale('en-GB'); $data = [ + 'id' => '626f01c28ba640bfb49c42ff41762e57', 'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT, 'name' => 'store front', 'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'), @@ -242,10 +253,6 @@ protected function createSalesChannelContext(?array $salesChannelData = null): S ], ]; - if ($salesChannelData) { - $data = array_merge($data, $salesChannelData); - } - return $this->createContext($data, []); } @@ -260,12 +267,37 @@ abstract protected static function getContainer(): ContainerInterface; private function createCustomer(): CustomerEntity { - $customerId = Uuid::randomHex(); - $addressId = Uuid::randomHex(); + $context = Context::createDefaultContext(); + $addressId = '43419bb9d20d436ab096a902c5452544'; + + /** @var EntityRepository $customerRepo */ + $customerRepo = $this->getContainer()->get('customer.repository'); + + $criteria = new Criteria([Constants::CUSTOMER_ID]); + $criteria->addAssociation('defaultBillingAddress'); + $customer = $customerRepo->search($criteria, $context)->first(); + + if ($customer instanceof CustomerEntity) { + // make sure that phoneNumber is not set (which may have been happened through other tests. + if ($customer->getDefaultBillingAddress()->getPhoneNumber()) { + /** @var EntityRepository $addressRepo */ + $addressRepo = $this->getContainer()->get('customer_address.repository'); + $addressRepo->upsert([['id' => $customer->getDefaultBillingAddressId(), 'phoneNumber' => null]], $context); + $customer->getDefaultBillingAddress()->assign(['phoneNumber' => null]); + } + + // make sure that birthday is not set (which may have been happened through other tests. + if ($customer->getBirthday()) { + $customerRepo->upsert([['id' => $customer->getId(), 'birthday' => null]], $context); + $customer->assign(['birthday' => null]); + } + + return $customer; + } $data = [ [ - 'id' => $customerId, + 'id' => Constants::CUSTOMER_ID, 'salesChannelId' => TestDefaults::SALES_CHANNEL, 'defaultShippingAddress' => [ 'id' => $addressId, @@ -289,13 +321,10 @@ private function createCustomer(): CustomerEntity ], ]; - $repo = $this->getContainer()->get('customer.repository'); - - $repo->create($data, Context::createDefaultContext()); + $customerRepo->create($data, $context); - $result = $repo->search(new Criteria([$customerId]), Context::createDefaultContext()); /** @var CustomerEntity $customer */ - $customer = $result->first(); + $customer = $customerRepo->search(new Criteria([Constants::CUSTOMER_ID]), Context::createDefaultContext())->first(); return $customer; } @@ -307,13 +336,22 @@ private function createCustomer(): CustomerEntity private function createContext(array $salesChannel, array $options): SalesChannelContext { $factory = $this->getContainer()->get(SalesChannelContextFactory::class); + /** @var EntityRepository $salesChannelRepository */ $salesChannelRepository = $this->getContainer()->get('sales_channel.repository'); - $salesChannelId = Uuid::randomHex(); - $salesChannel['id'] = $salesChannelId; - $salesChannel['customerGroupId'] = TestDefaults::FALLBACK_CUSTOMER_GROUP; + $salesChannelEntity = null; + if (isset($salesChannel['id'])) { + $salesChannelEntity = $salesChannelRepository->search(new Criteria([$salesChannel['id']]), Context::createDefaultContext())->first(); + } + + $salesChannelId = $salesChannel['id'] ?? Uuid::randomHex(); - $salesChannelRepository->create([$salesChannel], Context::createDefaultContext()); + if (!$salesChannelEntity instanceof SalesChannelEntity) { + $salesChannel['id'] = $salesChannelId; + $salesChannel['customerGroupId'] = TestDefaults::FALLBACK_CUSTOMER_GROUP; + + $salesChannelRepository->create([$salesChannel], Context::createDefaultContext()); + } $context = $factory->create(Uuid::randomHex(), $salesChannelId, $options);