Skip to content

Commit

Permalink
Add 6.5.7.3 and missing test traits from shopware core
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Müller committed Dec 27, 2023
1 parent 67e8e05 commit ad57c38
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
matrix:
php-versions: [ '8.1', '8.2' ]
mysql-versions: [ '5.7', '8.0' ]
shopware-versions: [ 'v6.5.0.0', 'v6.5.1.0', 'v6.5.2.0', 'v6.5.3.0', 'v6.5.4.0', 'v6.5.5.0', 'v6.5.6.0' ]
shopware-versions: [ 'v6.5.0.0', 'v6.5.1.0', 'v6.5.2.0', 'v6.5.3.0', 'v6.5.4.0', 'v6.5.5.0', 'v6.5.6.0', 'v6.5.7.3' ]
services:
mysql:
image: mysql:${{ matrix.mysql-versions }}
Expand Down
1 change: 0 additions & 1 deletion tests/TestCaseBase/PayoneTestBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\Test\TestDefaults;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
use Shopware\Storefront\Test\Page\StorefrontPageTestBehaviour;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down
325 changes: 325 additions & 0 deletions tests/TestCaseBase/StorefrontPageTestBehaviour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
<?php

declare(strict_types=1);

namespace PayonePayment\TestCaseBase;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Cart\CartRuleLoader;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\Framework\Struct\Struct;
use Shopware\Core\Framework\Test\TestCaseBase\TaxAddToSalesChannelTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Test\TestDefaults;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Pagelet\PageletLoadedEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Copied from https://github.com/shopware/shopware/blob/trunk/tests/integration/Storefront/Page/StorefrontPageTestBehaviour.php
*/
trait StorefrontPageTestBehaviour
{
use TaxAddToSalesChannelTestBehaviour;

/**
* @param class-string<object> $expectedClass
*/
public static function assertPageEvent(
string $expectedClass,
PageLoadedEvent $event,
SalesChannelContext $salesChannelContext,
Request $request,
Struct $page
): void {
TestCase::assertInstanceOf($expectedClass, $event);
TestCase::assertSame($salesChannelContext, $event->getSalesChannelContext());
TestCase::assertSame($salesChannelContext->getContext(), $event->getContext());
TestCase::assertSame($request, $event->getRequest());
TestCase::assertSame($page, $event->getPage());
}

/**
* @param class-string<object> $expectedClass
*/
public static function assertPageletEvent(
string $expectedClass,
PageletLoadedEvent $event,
SalesChannelContext $salesChannelContext,
Request $request,
Struct $page
): void {
TestCase::assertInstanceOf($expectedClass, $event);
TestCase::assertSame($salesChannelContext, $event->getSalesChannelContext());
TestCase::assertSame($salesChannelContext->getContext(), $event->getContext());
TestCase::assertSame($request, $event->getRequest());
TestCase::assertSame($page, $event->getPagelet());
}

abstract protected function getPageLoader();

protected function expectParamMissingException(string $paramName): void
{
$this->expectException(RoutingException::class);
$this->expectExceptionMessage('Parameter "' . $paramName . '" is missing');
}

protected function placeRandomOrder(SalesChannelContext $context): string
{
$product = $this->getRandomProduct($context);

$lineItem = (new LineItem($product->getId(), LineItem::PRODUCT_LINE_ITEM_TYPE, $product->getId()))
->setRemovable(true)
->setStackable(true);

$cartService = $this->getContainer()->get(CartService::class);
$cart = $cartService->getCart($context->getToken(), $context);
$cart->add($lineItem);

return $cartService->order($cart, $context, new RequestDataBag());
}

/**
* @param array<int|string, mixed> $config
*/
protected function getRandomProduct(SalesChannelContext $context, ?int $stock = 1, ?bool $isCloseout = false, array $config = []): ProductEntity
{
$id = Uuid::randomHex();
$productNumber = Uuid::randomHex();
$productRepository = $this->getContainer()->get('product.repository');

$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],
'active' => true,
'isCloseout' => $isCloseout,
'categories' => [
['id' => Uuid::randomHex(), 'name' => 'asd'],
],
'visibilities' => [
['salesChannelId' => $context->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL],
],
];

$data = array_merge_recursive($data, $config);

$productRepository->create([$data], $context->getContext());
$this->addTaxDataToSalesChannel($context, $data['tax']);

/** @var SalesChannelRepository $storefrontProductRepository */
$storefrontProductRepository = $this->getContainer()->get('sales_channel.product.repository');
$searchResult = $storefrontProductRepository->search(new Criteria([$id]), $context);

/** @var ProductEntity $product */
$product = $searchResult->first();

return $product;
}

protected function createSalesChannelContextWithNavigation(): SalesChannelContext
{
$paymentMethodId = $this->getValidPaymentMethodId();
$shippingMethodId = $this->getAvailableShippingMethod()->getId();
$countryId = $this->getValidCountryId();
$snippetSetId = $this->getSnippetSetIdForLocale('en-GB');
$data = [
'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
'name' => 'store front',
'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'),
'languageId' => Defaults::LANGUAGE_SYSTEM,
'snippetSetId' => $snippetSetId,
'currencyId' => Defaults::CURRENCY,
'currencyVersionId' => Defaults::LIVE_VERSION,
'paymentMethodId' => $paymentMethodId,
'paymentMethodVersionId' => Defaults::LIVE_VERSION,
'shippingMethodId' => $shippingMethodId,
'shippingMethodVersionId' => Defaults::LIVE_VERSION,
'navigationCategoryId' => $this->getValidCategoryId(),
'navigationCategoryVersionId' => Defaults::LIVE_VERSION,
'countryId' => $countryId,
'countryVersionId' => Defaults::LIVE_VERSION,
'currencies' => [['id' => Defaults::CURRENCY]],
'languages' => [['id' => Defaults::LANGUAGE_SYSTEM]],
'paymentMethods' => [['id' => $paymentMethodId]],
'shippingMethods' => [['id' => $shippingMethodId]],
'countries' => [['id' => $countryId]],
'domains' => [
['url' => 'http://test.com/' . Uuid::randomHex(), 'currencyId' => Defaults::CURRENCY, 'languageId' => Defaults::LANGUAGE_SYSTEM, 'snippetSetId' => $snippetSetId],
],
];

return $this->createContext($data, []);
}

protected function createSalesChannelContextWithLoggedInCustomerAndWithNavigation(): SalesChannelContext
{
$paymentMethodId = $this->getValidPaymentMethodId();
$shippingMethodId = $this->getAvailableShippingMethod()->getId();
$countryId = $this->getValidCountryId();
$snippetSetId = $this->getSnippetSetIdForLocale('en-GB');
$data = [
'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
'name' => 'store front',
'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'),
'languageId' => Defaults::LANGUAGE_SYSTEM,
'snippetSetId' => $snippetSetId,
'currencyId' => Defaults::CURRENCY,
'currencyVersionId' => Defaults::LIVE_VERSION,
'paymentMethodId' => $paymentMethodId,
'paymentMethodVersionId' => Defaults::LIVE_VERSION,
'shippingMethodId' => $shippingMethodId,
'shippingMethodVersionId' => Defaults::LIVE_VERSION,
'navigationCategoryId' => $this->getValidCategoryId(),
'countryId' => $countryId,
'countryVersionId' => Defaults::LIVE_VERSION,
'currencies' => [['id' => Defaults::CURRENCY]],
'languages' => [['id' => Defaults::LANGUAGE_SYSTEM]],
'paymentMethods' => [['id' => $paymentMethodId]],
'shippingMethods' => [['id' => $shippingMethodId]],
'countries' => [['id' => $countryId]],
'domains' => [
['url' => 'http://test.com/' . Uuid::randomHex(), 'currencyId' => Defaults::CURRENCY, 'languageId' => Defaults::LANGUAGE_SYSTEM, 'snippetSetId' => $snippetSetId],
],
];

return $this->createContext($data, [
SalesChannelContextService::CUSTOMER_ID => $this->createCustomer()->getId(),
]);
}

/**
* @param array<string, mixed>|null $salesChannelData
*/
protected function createSalesChannelContext(?array $salesChannelData = null): SalesChannelContext
{
$paymentMethodId = $this->getValidPaymentMethodId();
$shippingMethodId = $this->getAvailableShippingMethod()->getId();
$countryId = $this->getValidCountryId();
$snippetSetId = $this->getSnippetSetIdForLocale('en-GB');
$data = [
'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
'name' => 'store front',
'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'),
'languageId' => Defaults::LANGUAGE_SYSTEM,
'snippetSetId' => $snippetSetId,
'currencyId' => Defaults::CURRENCY,
'currencyVersionId' => Defaults::LIVE_VERSION,
'paymentMethodId' => $paymentMethodId,
'paymentMethodVersionId' => Defaults::LIVE_VERSION,
'shippingMethodId' => $shippingMethodId,
'shippingMethodVersionId' => Defaults::LIVE_VERSION,
'navigationCategoryId' => $this->getValidCategoryId(),
'navigationCategoryVersionId' => Defaults::LIVE_VERSION,
'countryId' => $countryId,
'countryVersionId' => Defaults::LIVE_VERSION,
'currencies' => [['id' => Defaults::CURRENCY]],
'languages' => [['id' => Defaults::LANGUAGE_SYSTEM]],
'paymentMethods' => [['id' => $paymentMethodId]],
'shippingMethods' => [['id' => $shippingMethodId]],
'countries' => [['id' => $countryId]],
'domains' => [
['url' => 'http://test.com/' . Uuid::randomHex(), 'currencyId' => Defaults::CURRENCY, 'languageId' => Defaults::LANGUAGE_SYSTEM, 'snippetSetId' => $snippetSetId],
],
];

if ($salesChannelData) {
$data = array_merge($data, $salesChannelData);
}

return $this->createContext($data, []);
}

protected function catchEvent(string $eventName, ?object &$eventResult): void
{
$this->addEventListener($this->getContainer()->get('event_dispatcher'), $eventName, static function ($event) use (&$eventResult): void {
$eventResult = $event;
});
}

abstract protected static function getContainer(): ContainerInterface;

private function createCustomer(): CustomerEntity
{
$customerId = Uuid::randomHex();
$addressId = Uuid::randomHex();

$data = [
[
'id' => $customerId,
'salesChannelId' => TestDefaults::SALES_CHANNEL,
'defaultShippingAddress' => [
'id' => $addressId,
'firstName' => 'Max',
'lastName' => 'Mustermann',
'street' => 'Musterstraße 1',
'city' => 'Schöppingen',
'zipcode' => '12345',
'salutationId' => $this->getValidSalutationId(),
'country' => ['id' => $this->getValidCountryId()],
],
'defaultBillingAddressId' => $addressId,
'defaultPaymentMethodId' => $this->getValidPaymentMethodId(),
'groupId' => TestDefaults::FALLBACK_CUSTOMER_GROUP,
'email' => 'foo@bar.de',
'password' => 'password',
'firstName' => 'Max',
'lastName' => 'Mustermann',
'salutationId' => $this->getValidSalutationId(),
'customerNumber' => '12345',
],
];

$repo = $this->getContainer()->get('customer.repository');

$repo->create($data, Context::createDefaultContext());

$result = $repo->search(new Criteria([$customerId]), Context::createDefaultContext());
/** @var CustomerEntity $customer */
$customer = $result->first();

return $customer;
}

/**
* @param array<string, mixed> $salesChannel
* @param array<string, mixed> $options
*/
private function createContext(array $salesChannel, array $options): SalesChannelContext
{
$factory = $this->getContainer()->get(SalesChannelContextFactory::class);
$salesChannelRepository = $this->getContainer()->get('sales_channel.repository');

$salesChannelId = Uuid::randomHex();
$salesChannel['id'] = $salesChannelId;
$salesChannel['customerGroupId'] = TestDefaults::FALLBACK_CUSTOMER_GROUP;

$salesChannelRepository->create([$salesChannel], Context::createDefaultContext());

$context = $factory->create(Uuid::randomHex(), $salesChannelId, $options);

$ruleLoader = $this->getContainer()->get(CartRuleLoader::class);
$ruleLoader->loadByToken($context, $context->getToken());

return $context;
}
}
29 changes: 29 additions & 0 deletions tests/TestCaseBase/StorefrontPageTestConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PayonePayment\TestCaseBase;

/**
* Copied from https://github.com/shopware/shopware/blob/trunk/tests/integration/Storefront/Page/StorefrontPageTestConstants.php
*/
final class StorefrontPageTestConstants
{
public const CUSTOMER_FIRSTNAME = 'Max';

public const PAYMENT_METHOD_COUNT = 1;

public const PRODUCT_NAME = 'test';

public const SHIPPING_METHOD_COUNT = 2;

public const AVAILABLE_SHIPPING_METHOD_COUNT = 1;

public const AVAILABLE_PAYMENT_METHOD_COUNT = 1;

public const COUNTRY_COUNT = 1;

private function __construct()
{
}
}

0 comments on commit ad57c38

Please sign in to comment.