diff --git a/.github/workflows/infection.yml b/.github/workflows/infection.yml index 9d48438..d0de74e 100644 --- a/.github/workflows/infection.yml +++ b/.github/workflows/infection.yml @@ -28,6 +28,6 @@ jobs: run: composer update --no-ansi --no-interaction --no-progress - name: Infection PHP - run: vendor/bin/infection --min-msi=90 --min-covered-msi=95 --threads=4 + run: vendor/bin/infection --min-covered-msi=95 --threads=4 env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/composer.json b/composer.json index 13487cb..8ba42ca 100644 --- a/composer.json +++ b/composer.json @@ -17,17 +17,17 @@ }, "require": { "php": "~8.1.0 || ~8.2.0 || ~8.3.0", + "cuyz/valinor": "^1.12", "lcobucci/jwt": "^4.0 || ^5.0", - "lcobucci/clock": "^3", "php-http/discovery": "^1.17", + "psr/clock-implementation": "*", "psr/event-dispatcher": "^1.0", "psr/http-client": "^1.0", "psr/http-client-implementation": "*", "psr/http-factory": "^1.0", "psr/http-factory-implementation": "*", "psr/http-message": "^1.0 || ^2.0", - "psr/simple-cache": "^3.0", - "psr/clock-implementation": "*" + "psr/simple-cache": "^3.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.16", diff --git a/devenv.nix b/devenv.nix index 948b0d3..929f78e 100644 --- a/devenv.nix +++ b/devenv.nix @@ -3,4 +3,6 @@ { languages.php.enable = true; languages.php.extensions = [ "xdebug" ]; + + processes = {}; } diff --git a/phpunit.xml b/phpunit.xml index 1879ab8..971108d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,7 +5,6 @@ cacheDirectory=".phpunit.cache" executionOrder="depends,defects" requireCoverageMetadata="true" - beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true"> diff --git a/src/Authentication/RequestVerifier.php b/src/Authentication/RequestVerifier.php index 978030c..127fc6f 100644 --- a/src/Authentication/RequestVerifier.php +++ b/src/Authentication/RequestVerifier.php @@ -115,7 +115,7 @@ public function authenticateStorefrontRequest(RequestInterface $request, ShopInt */ private function getSignatureFromQuery(RequestInterface $request): string { - parse_str($request->getUri()->getQuery(), $queries); + \parse_str($request->getUri()->getQuery(), $queries); if (!isset($queries[self::SHOPWARE_SHOP_SIGNATURE_HEADER])) { throw new SignatureNotFoundException($request); @@ -123,7 +123,7 @@ private function getSignatureFromQuery(RequestInterface $request): string $header = $queries[self::SHOPWARE_SHOP_SIGNATURE_HEADER]; - if (!is_string($header)) { + if (!\is_string($header)) { /** @infection-ignore-all */ throw new SignatureNotFoundException($request); } diff --git a/src/Context/Cart/CalculatedPrice.php b/src/Context/Cart/CalculatedPrice.php index 997f837..7e2b661 100644 --- a/src/Context/Cart/CalculatedPrice.php +++ b/src/Context/Cart/CalculatedPrice.php @@ -5,6 +5,7 @@ namespace Shopware\App\SDK\Context\Cart; use Shopware\App\SDK\Context\ArrayStruct; +use Shopware\App\SDK\Framework\Collection; class CalculatedPrice extends ArrayStruct { @@ -27,38 +28,40 @@ public function getQuantity(): int } /** - * @return array + * @return Collection */ - public function getCalculatedTaxes(): array + public function getCalculatedTaxes(): Collection { \assert(is_array($this->data['calculatedTaxes'])); - return array_map(static function (array $calculatedTax): CalculatedTax { + + return new Collection(\array_map(static function (array $calculatedTax): CalculatedTax { return new CalculatedTax($calculatedTax); - }, $this->data['calculatedTaxes']); + }, $this->data['calculatedTaxes'])); } /** - * @return array + * @return Collection */ - public function getTaxRules(): array + public function getTaxRules(): Collection { \assert(is_array($this->data['taxRules'])); - return array_map(static function (array $taxRule): TaxRule { + + return new Collection(\array_map(static function (array $taxRule): TaxRule { return new TaxRule($taxRule); - }, $this->data['taxRules']); + }, $this->data['taxRules'])); } /** - * @param array $prices + * @param Collection $prices */ - public static function sum(array $prices): CalculatedPrice + public static function sum(Collection $prices): CalculatedPrice { return new CalculatedPrice([ - 'unitPrice' => array_sum(array_map(static fn (CalculatedPrice $price): float => $price->getUnitPrice(), $prices)), - 'totalPrice' => array_sum(array_map(static fn (CalculatedPrice $price): float => $price->getTotalPrice(), $prices)), + 'unitPrice' => \array_sum($prices->map(static fn (CalculatedPrice $price): float => $price->getUnitPrice())), + 'totalPrice' => \array_sum($prices->map(static fn (CalculatedPrice $price): float => $price->getTotalPrice())), 'quantity' => 1, - 'calculatedTaxes' => array_map(static fn (CalculatedTax $tax) => $tax->toArray(), CalculatedTax::sum(array_merge(...array_map(static fn (CalculatedPrice $price): array => $price->getCalculatedTaxes(), $prices)))), - 'taxRules' => array_map(static fn (TaxRule $rule) => $rule->toArray(), array_merge(...array_map(static fn (CalculatedPrice $price): array => $price->getTaxRules(), $prices))), + 'calculatedTaxes' => $prices->map(static fn (CalculatedPrice $price) => CalculatedTax::sum($price->getCalculatedTaxes())), + 'taxRules' => $prices->map(static fn (CalculatedPrice $price) => $price->getTaxRules()), ]); } } diff --git a/src/Context/Cart/CalculatedTax.php b/src/Context/Cart/CalculatedTax.php index f66ba5c..21b8c2a 100644 --- a/src/Context/Cart/CalculatedTax.php +++ b/src/Context/Cart/CalculatedTax.php @@ -5,6 +5,7 @@ namespace Shopware\App\SDK\Context\Cart; use Shopware\App\SDK\Context\ArrayStruct; +use Shopware\App\SDK\Framework\Collection; class CalculatedTax extends ArrayStruct { @@ -27,28 +28,33 @@ public function getTax(): float } /** - * @param array $calculatedTaxes - * @return array + * @param Collection $calculatedTaxes + * @return array */ - public static function sum(array $calculatedTaxes): array + public static function sum(Collection $calculatedTaxes): array { - $new = []; + $new = new Collection(); foreach ($calculatedTaxes as $calculatedTax) { - $exists = isset($new[$calculatedTax->getTaxRate()]); - if (!$exists) { - $new[$calculatedTax->getTaxRate()] = $calculatedTax; + if (!$new->has((string) $calculatedTax->getTaxRate())) { + $new->set((string) $calculatedTax->getTaxRate(), $calculatedTax); continue; } - $new[$calculatedTax->getTaxRate()] = new CalculatedTax([ - 'taxRate' => $calculatedTax->getTaxRate(), - 'price' => $new[$calculatedTax->getTaxRate()]->getPrice() + $calculatedTax->getPrice(), - 'tax' => $new[$calculatedTax->getTaxRate()]->getTax() + $calculatedTax->getTax(), - ]); + $new->set( + (string) $calculatedTax->getTaxRate(), + new CalculatedTax([ + 'taxRate' => $calculatedTax->getTaxRate(), + 'price' => $new->get((string) $calculatedTax->getTaxRate())->getPrice() + $calculatedTax->getPrice(), + 'tax' => $new->get((string) $calculatedTax->getTaxRate())->getTax() + $calculatedTax->getTax(), + ]) + ); + } - return $new; + var_dump($new->()); + + return $new->all(); } } diff --git a/src/Context/Cart/Cart.php b/src/Context/Cart/Cart.php index 03c5105..5a8c2c4 100644 --- a/src/Context/Cart/Cart.php +++ b/src/Context/Cart/Cart.php @@ -5,6 +5,7 @@ namespace Shopware\App\SDK\Context\Cart; use Shopware\App\SDK\Context\ArrayStruct; +use Shopware\App\SDK\Framework\Collection; class Cart extends ArrayStruct { @@ -33,39 +34,42 @@ public function getCampaignCode(): ?string } /** - * @return array + * @return Collection */ - public function getLineItems(): array + public function getLineItems(): Collection { - \assert(is_array($this->data['lineItems'])); - return array_map( + \assert(\is_array($this->data['lineItems'])); + + return new Collection(\array_map( static fn (array $lineItem) => new LineItem($lineItem), $this->data['lineItems'] - ); + )); } /** - * @return array + * @return Collection */ - public function getDeliveries(): array + public function getDeliveries(): Collection { - \assert(is_array($this->data['deliveries'])); - return array_map( + \assert(\is_array($this->data['deliveries'])); + + return new Collection(\array_map( static fn (array $delivery) => new Delivery($delivery), $this->data['deliveries'] - ); + )); } /** - * @return array + * @return Collection */ - public function getTransactions(): array + public function getTransactions(): Collection { - \assert(is_array($this->data['transactions'])); - return array_map( + \assert(\is_array($this->data['transactions'])); + + return new Collection(\array_map( static fn (array $transaction) => new CartTransaction($transaction), $this->data['transactions'] - ); + )); } public function getPrice(): CartPrice diff --git a/src/Context/Cart/CartPrice.php b/src/Context/Cart/CartPrice.php index 58db91a..53720ef 100644 --- a/src/Context/Cart/CartPrice.php +++ b/src/Context/Cart/CartPrice.php @@ -5,6 +5,7 @@ namespace Shopware\App\SDK\Context\Cart; use Shopware\App\SDK\Context\ArrayStruct; +use Shopware\App\SDK\Framework\Collection; class CartPrice extends ArrayStruct { @@ -25,14 +26,15 @@ public function getTotalPrice(): float } /** - * @return array + * @return Collection */ - public function getCalculatedTaxes(): array + public function getCalculatedTaxes(): Collection { \assert(is_array($this->data['calculatedTaxes'])); - return array_map(static function (array $calculatedTax): CalculatedTax { + + return new Collection(\array_map(static function (array $calculatedTax): CalculatedTax { return new CalculatedTax($calculatedTax); - }, $this->data['calculatedTaxes']); + }, $this->data['calculatedTaxes'])); } public function getTaxStatus(): string @@ -42,15 +44,15 @@ public function getTaxStatus(): string } /** - * @return array + * @return Collection */ - public function getTaxRules(): array + public function getTaxRules(): Collection { - \assert(is_array($this->data['taxRules'])); + \assert(\is_array($this->data['taxRules'])); - return array_map(static function (array $taxRule): TaxRule { + return new Collection(\array_map(static function (array $taxRule): TaxRule { return new TaxRule($taxRule); - }, $this->data['taxRules']); + }, $this->data['taxRules'])); } public function getPositionPrice(): float diff --git a/src/Context/Cart/Delivery.php b/src/Context/Cart/Delivery.php index 2970d0b..1fd867c 100644 --- a/src/Context/Cart/Delivery.php +++ b/src/Context/Cart/Delivery.php @@ -7,19 +7,20 @@ use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\SalesChannelContext\ShippingLocation; use Shopware\App\SDK\Context\SalesChannelContext\ShippingMethod; +use Shopware\App\SDK\Framework\Collection; class Delivery extends ArrayStruct { /** - * @return array + * @return Collection */ - public function getPositions(): array + public function getPositions(): Collection { \assert(is_array($this->data['positions'])); - return array_map(static function (array $position) { + return new Collection(\array_map(static function (array $position) { return new DeliveryPosition($position); - }, $this->data['positions']); + }, $this->data['positions'])); } public function getLocation(): ShippingLocation diff --git a/src/Context/Cart/Error.php b/src/Context/Cart/Error.php new file mode 100644 index 0000000..0b6268e --- /dev/null +++ b/src/Context/Cart/Error.php @@ -0,0 +1,12 @@ + + * @return Collection */ - public function getChildren(): array + public function getChildren(): Collection { \assert(is_array($this->data['children'])); - return array_map(static fn (array $child): LineItem => new LineItem($child), $this->data['children']); + return new Collection(\array_map(static fn (array $child): LineItem => new LineItem($child), $this->data['children'])); } } diff --git a/src/Context/Order/Order.php b/src/Context/Order/Order.php index 0f8d018..cfbe574 100644 --- a/src/Context/Order/Order.php +++ b/src/Context/Order/Order.php @@ -11,6 +11,7 @@ use Shopware\App\SDK\Context\SalesChannelContext\Currency; use Shopware\App\SDK\Context\SalesChannelContext\RoundingConfig; use Shopware\App\SDK\Context\Trait\CustomFieldsAware; +use Shopware\App\SDK\Framework\Collection; class Order extends ArrayStruct { @@ -101,14 +102,15 @@ public function getBillingAddress(): Address } /** - * @return array + * @return Collection */ - public function getLineItems(): array + public function getLineItems(): Collection { \assert(\is_array($this->data['lineItems'])); - return array_map(static function (array $lineItem): OrderLineItem { + + return new Collection(\array_map(static function (array $lineItem): OrderLineItem { return new OrderLineItem($lineItem); - }, $this->data['lineItems']); + }, $this->data['lineItems'])); } public function getItemRounding(): RoundingConfig @@ -136,24 +138,26 @@ public function getSalesChannelId(): string } /** - * @return array + * @return Collection */ - public function getDeliveries(): array + public function getDeliveries(): Collection { \assert(\is_array($this->data['deliveries'])); - return array_map(static function (array $delivery): OrderDelivery { + + return new Collection(\array_map(static function (array $delivery): OrderDelivery { return new OrderDelivery($delivery); - }, $this->data['deliveries']); + }, $this->data['deliveries'])); } /** - * @return array + * @return Collection */ - public function getTransactions(): array + public function getTransactions(): Collection { \assert(\is_array($this->data['transactions'])); - return array_map(static function (array $transaction): OrderTransaction { + + return new Collection(\array_map(static function (array $transaction): OrderTransaction { return new OrderTransaction($transaction); - }, $this->data['transactions']); + }, $this->data['transactions'])); } } diff --git a/src/Context/SalesChannelContext/SalesChannel.php b/src/Context/SalesChannelContext/SalesChannel.php index 77bef48..5b77558 100644 --- a/src/Context/SalesChannelContext/SalesChannel.php +++ b/src/Context/SalesChannelContext/SalesChannel.php @@ -5,6 +5,7 @@ namespace Shopware\App\SDK\Context\SalesChannelContext; use Shopware\App\SDK\Context\ArrayStruct; +use Shopware\App\SDK\Framework\Collection; class SalesChannel extends ArrayStruct { @@ -39,13 +40,14 @@ public function getCurrency(): Currency } /** - * @return array + * @return Collection */ - public function getDomains(): array + public function getDomains(): Collection { \assert(is_array($this->data['domains'])); - return array_map(static function (array $domain): SalesChannelDomain { + + return new Collection(\array_map(static function (array $domain): SalesChannelDomain { return new SalesChannelDomain($domain); - }, $this->data['domains']); + }, $this->data['domains'])); } } diff --git a/src/Context/TaxProvider/TaxProviderAction.php b/src/Context/TaxProvider/TaxProviderAction.php index 216e207..a098f5e 100644 --- a/src/Context/TaxProvider/TaxProviderAction.php +++ b/src/Context/TaxProvider/TaxProviderAction.php @@ -12,10 +12,10 @@ class TaxProviderAction { public function __construct( - public readonly ShopInterface $shop, - public readonly ActionSource $source, + public readonly ShopInterface $shop, + public readonly ActionSource $source, public readonly SalesChannelContext $context, - public readonly Cart $cart, + public readonly Cart $cart, ) { } } diff --git a/src/Framework/Collection.php b/src/Framework/Collection.php new file mode 100644 index 0000000..6dd5a5c --- /dev/null +++ b/src/Framework/Collection.php @@ -0,0 +1,146 @@ + + */ +final class Collection implements Countable, IteratorAggregate, JsonSerializable +{ + /** + * @var array + */ + protected array $elements = []; + + /** + * @param iterable $elements + */ + public function __construct(iterable $elements = []) + { + var_dump($elements); + + /** + * @var array-key|null $key + */ + foreach ($elements as $key => $element) { + $this->set($key, $element); + } + } + + /** + * @return array + */ + public function all(): array + { + return $this->elements; + } + + /** + * @param array-key|null $key + * @param TElement $element + */ + public function set(int|string|null $key, $element): void + { + $key === null ? $this->elements[] = $element : $this->elements[$key] = $element; + } + + /** + * @param TElement $element + */ + public function add($element): void + { + $this->elements[] = $element; + } + + /** + * @param array-key $key + * + * @return TElement|null + */ + public function get(int|string $key): mixed + { + if ($this->has($key)) { + return $this->elements[$key]; + } + + return null; + } + + /** + * @return TElement|null + */ + public function first(): mixed + { + return $this->elements[\array_key_first($this->elements)] ?? null; + } + + /** + * @return TElement|null + */ + public function last(): mixed + { + return $this->elements[array_key_last($this->elements)] ?? null; + } + + /** + * @param array-key $key + */ + public function remove(int|string $key): void + { + unset($this->elements[$key]); + } + + /** + * @param array-key $key + */ + public function has(int|string $key): bool + { + return \array_key_exists($key, $this->elements); + } + + /** + * @return array + */ + public function map(\Closure $closure): array + { + return \array_map($closure, $this->elements); + } + + /** + * @return self + */ + public function filter(\Closure $closure): self + { + return new Collection(\array_filter($this->elements, $closure)); + } + + /** + * @return Traversable + */ + public function getIterator(): Traversable + { + yield from $this->elements; + } + + public function count(): int + { + return \count($this->elements); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->all(); + } +} diff --git a/src/Gateway/Checkout/CheckoutGatewayCommand.php b/src/Gateway/Checkout/CheckoutGatewayCommand.php new file mode 100644 index 0000000..356d691 --- /dev/null +++ b/src/Gateway/Checkout/CheckoutGatewayCommand.php @@ -0,0 +1,45 @@ + + */ + public array $payload = []; + + public string $keyName; + + public function setPayloadValue(string $key, mixed $value): void + { + $this->payload[$key] = $value; + } + + public function hasPayloadValue(string $key): bool + { + return isset($this->payload[$key]); + } + + public function getPayloadValue(string $key): mixed + { + if (!$this->hasPayloadValue($key)) { + return null; + } + + return $this->payload[$key]; + } + + /** + * @return array{command: string, payload: array} + */ + public function jsonSerialize(): array + { + return [ + 'command' => $this->keyName, + 'payload' => $this->payload, + ]; + } +} diff --git a/src/Gateway/Checkout/Command/AddCartErrorCommand.php b/src/Gateway/Checkout/Command/AddCartErrorCommand.php new file mode 100644 index 0000000..76a8c64 --- /dev/null +++ b/src/Gateway/Checkout/Command/AddCartErrorCommand.php @@ -0,0 +1,24 @@ +keyName = self::KEY; + $this->setPayloadValue('message', $message); + $this->setPayloadValue('blocking', $blocking); + $this->setPayloadValue('level', $level); + } +} diff --git a/src/Gateway/Checkout/Command/AddPaymentMethodCommand.php b/src/Gateway/Checkout/Command/AddPaymentMethodCommand.php new file mode 100644 index 0000000..bcdaf94 --- /dev/null +++ b/src/Gateway/Checkout/Command/AddPaymentMethodCommand.php @@ -0,0 +1,21 @@ +keyName = self::KEY; + $this->setPayloadValue('paymentMethodTechnicalName', $paymentMethodTechnicalName); + } +} diff --git a/src/Gateway/Checkout/Command/AddPaymentMethodExtensionCommand.php b/src/Gateway/Checkout/Command/AddPaymentMethodExtensionCommand.php new file mode 100644 index 0000000..e2ab27c --- /dev/null +++ b/src/Gateway/Checkout/Command/AddPaymentMethodExtensionCommand.php @@ -0,0 +1,26 @@ + $extensionsPayload + */ + public function __construct( + public readonly string $paymentMethodTechnicalName, + public readonly string $extensionKey, + public readonly array $extensionsPayload, + ) { + $this->keyName = self::KEY; + $this->setPayloadValue('paymentMethodTechnicalName', $paymentMethodTechnicalName); + $this->setPayloadValue('extensionKey', $extensionKey); + $this->setPayloadValue('extensionsPayload', $extensionsPayload); + } +} diff --git a/src/Gateway/Checkout/Command/AddShippingMethodCommand.php b/src/Gateway/Checkout/Command/AddShippingMethodCommand.php new file mode 100644 index 0000000..f272c57 --- /dev/null +++ b/src/Gateway/Checkout/Command/AddShippingMethodCommand.php @@ -0,0 +1,21 @@ +keyName = self::KEY; + $this->setPayloadValue('shippingMethodTechnicalName', $shippingMethodTechnicalName); + } +} diff --git a/src/Gateway/Checkout/Command/AddShippingMethodExtensionCommand.php b/src/Gateway/Checkout/Command/AddShippingMethodExtensionCommand.php new file mode 100644 index 0000000..87eb9cf --- /dev/null +++ b/src/Gateway/Checkout/Command/AddShippingMethodExtensionCommand.php @@ -0,0 +1,26 @@ + $extensionsPayload + */ + public function __construct( + public readonly string $shippingMethodTechnicalName, + public readonly string $extensionKey, + public readonly array $extensionsPayload, + ) { + $this->keyName = self::KEY; + $this->setPayloadValue('shippingMethodTechnicalName', $shippingMethodTechnicalName); + $this->setPayloadValue('extensionKey', $extensionKey); + $this->setPayloadValue('extensionsPayload', $extensionsPayload); + } +} diff --git a/src/Gateway/Checkout/Command/RemovePaymentMethodCommand.php b/src/Gateway/Checkout/Command/RemovePaymentMethodCommand.php new file mode 100644 index 0000000..ed78631 --- /dev/null +++ b/src/Gateway/Checkout/Command/RemovePaymentMethodCommand.php @@ -0,0 +1,18 @@ +keyName = self::KEY; + $this->setPayloadValue('paymentMethodTechnicalName', $paymentMethodTechnicalName); + } +} diff --git a/src/Gateway/Checkout/Command/RemoveShippingMethodCommand.php b/src/Gateway/Checkout/Command/RemoveShippingMethodCommand.php new file mode 100644 index 0000000..27399ad --- /dev/null +++ b/src/Gateway/Checkout/Command/RemoveShippingMethodCommand.php @@ -0,0 +1,18 @@ +keyName = self::KEY; + $this->setPayloadValue('shippingMethodTechnicalName', $shippingMethodTechnicalName); + } +} diff --git a/src/Response/GatewayResponse.php b/src/Response/GatewayResponse.php new file mode 100644 index 0000000..8c19f8c --- /dev/null +++ b/src/Response/GatewayResponse.php @@ -0,0 +1,34 @@ + $checkoutCommands + */ + public static function createCheckoutGatewayResponse(Collection $checkoutCommands): ResponseInterface + { + return self::createResponse($checkoutCommands->jsonSerialize()); + } + + /** + * @param array $data + */ + private static function createResponse(array $data): ResponseInterface + { + $psr = new Psr17Factory(); + + return $psr + ->createResponse(200) + ->withHeader('Content-Type', 'application/json') + ->withBody($psr->createStream(\json_encode($data, \JSON_THROW_ON_ERROR))); + } +} diff --git a/src/TaxProvider/CalculatedTax.php b/src/TaxProvider/CalculatedTax.php index caf9790..f5870ee 100644 --- a/src/TaxProvider/CalculatedTax.php +++ b/src/TaxProvider/CalculatedTax.php @@ -18,6 +18,6 @@ public function __construct( */ public function jsonSerialize(): array { - return get_object_vars($this); + return \get_object_vars($this); } } diff --git a/src/TaxProvider/TaxProviderResponseBuilder.php b/src/TaxProvider/TaxProviderResponseBuilder.php index 99cc7ab..456ece1 100644 --- a/src/TaxProvider/TaxProviderResponseBuilder.php +++ b/src/TaxProvider/TaxProviderResponseBuilder.php @@ -6,52 +6,58 @@ use Http\Discovery\Psr17Factory; use Psr\Http\Message\ResponseInterface; +use Shopware\App\SDK\Framework\Collection; class TaxProviderResponseBuilder { /** - * @var array> + * @var Collection */ - protected array $lineItemTaxes = []; + protected Collection $lineItemTaxes; /** - * @var array> + * @var Collection */ - protected array $deliveryTaxes = []; + protected Collection $deliveryTaxes; /** - * @var array + * @var Collection */ - protected array $cartPriceTaxes = []; + protected Collection $cartPriceTaxes; public function __construct() { + $this->cartPriceTaxes = new Collection(); + $this->deliveryTaxes = new Collection(); + $this->lineItemTaxes = new Collection(); } public function addLineItemTax(string $uniqueIdentifier, CalculatedTax $tax): self { - $this->lineItemTaxes[$uniqueIdentifier][] = $tax; + $this->lineItemTaxes->set($uniqueIdentifier, $tax); return $this; } public function addDeliveryTax(string $uniqueIdentifier, CalculatedTax $tax): self { - $this->deliveryTaxes[$uniqueIdentifier][] = $tax; + $this->deliveryTaxes->set($uniqueIdentifier, $tax); return $this; } public function addCartTax(CalculatedTax $tax): self { - $this->cartPriceTaxes[] = $tax; - + $this->cartPriceTaxes->add($tax); return $this; } public function buildPayload(): string { - return \json_encode(get_object_vars($this), JSON_THROW_ON_ERROR); + return \json_encode(\get_object_vars($this), \JSON_THROW_ON_ERROR); } + /** + * @throws \JsonException + */ public function build(): ResponseInterface { $psrFactory = new Psr17Factory(); diff --git a/tests/AppLifecycleTest.php b/tests/AppLifecycleTest.php index bd00653..b5f4f7d 100644 --- a/tests/AppLifecycleTest.php +++ b/tests/AppLifecycleTest.php @@ -13,31 +13,18 @@ use Shopware\App\SDK\AppLifecycle; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Authentication\RequestVerifier; -use Shopware\App\SDK\Event\AbstractAppLifecycleEvent; use Shopware\App\SDK\Event\BeforeShopActivateEvent; use Shopware\App\SDK\Event\BeforeShopDeactivatedEvent; use Shopware\App\SDK\Event\BeforeShopDeletionEvent; use Shopware\App\SDK\Event\ShopActivatedEvent; use Shopware\App\SDK\Event\ShopDeactivatedEvent; use Shopware\App\SDK\Event\ShopDeletedEvent; -use Shopware\App\SDK\Exception\ShopNotFoundException; use Shopware\App\SDK\Registration\RegistrationService; use Shopware\App\SDK\Shop\ShopResolver; use Shopware\App\SDK\Test\MockShop; use Shopware\App\SDK\Test\MockShopRepository; #[CoversClass(AppLifecycle::class)] -#[CoversClass(ShopNotFoundException::class)] -#[CoversClass(AbstractAppLifecycleEvent::class)] -#[CoversClass(BeforeShopDeletionEvent::class)] -#[CoversClass(ShopDeletedEvent::class)] -#[CoversClass(BeforeShopActivateEvent::class)] -#[CoversClass(BeforeShopDeactivatedEvent::class)] -#[CoversClass(ShopDeactivatedEvent::class)] -#[CoversClass(ShopActivatedEvent::class)] -#[CoversClass(ShopResolver::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(MockShopRepository::class)] class AppLifecycleTest extends TestCase { private MockShopRepository $shopRepository; diff --git a/tests/Authentication/RequestVerifierTest.php b/tests/Authentication/RequestVerifierTest.php index 955e2df..f888730 100644 --- a/tests/Authentication/RequestVerifierTest.php +++ b/tests/Authentication/RequestVerifierTest.php @@ -20,11 +20,6 @@ use Shopware\App\SDK\Test\MockShop; #[CoversClass(RequestVerifier::class)] -#[CoversClass(SignatureNotFoundException::class)] -#[CoversClass(SignatureInvalidException::class)] -#[CoversClass(ShopNotFoundException::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(AppConfiguration::class)] class RequestVerifierTest extends TestCase { public function testAuthenticateRegistrationRequestMissingHeader(): void diff --git a/tests/Authentication/ResponseSignerTest.php b/tests/Authentication/ResponseSignerTest.php index 8167c70..e0edaf7 100644 --- a/tests/Authentication/ResponseSignerTest.php +++ b/tests/Authentication/ResponseSignerTest.php @@ -13,8 +13,6 @@ use Shopware\App\SDK\Test\MockShop; #[CoversClass(ResponseSigner::class)] -#[CoversClass(AppConfiguration::class)] -#[CoversClass(MockShop::class)] class ResponseSignerTest extends TestCase { private ResponseSigner $signer; diff --git a/tests/Context/ActionButton/ActionButtonActionTest.php b/tests/Context/ActionButton/ActionButtonActionTest.php new file mode 100644 index 0000000..d081bdf --- /dev/null +++ b/tests/Context/ActionButton/ActionButtonActionTest.php @@ -0,0 +1,38 @@ +createMock(ShopInterface::class); + $mockActionSource = $this->createMock(ActionSource::class); + $ids = ['1', '2', '3']; + $entity = 'testEntity'; + $action = 'testAction'; + + $actionButtonAction = new ActionButtonAction( + $mockShop, + $mockActionSource, + $ids, + $entity, + $action + ); + + static::assertSame($mockShop, $actionButtonAction->shop); + static::assertSame($mockActionSource, $actionButtonAction->source); + static::assertSame($ids, $actionButtonAction->ids); + static::assertSame($entity, $actionButtonAction->entity); + static::assertSame($action, $actionButtonAction->action); + } +} diff --git a/tests/Context/Cart/CalculatedPriceTest.php b/tests/Context/Cart/CalculatedPriceTest.php index 7b16cdd..828ddcd 100644 --- a/tests/Context/Cart/CalculatedPriceTest.php +++ b/tests/Context/Cart/CalculatedPriceTest.php @@ -5,15 +5,11 @@ namespace Shopware\App\SDK\Tests\Context\Cart; use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\Cart\CalculatedPrice; -use Shopware\App\SDK\Context\Cart\CalculatedTax; +use Shopware\App\SDK\Framework\Collection; #[CoversClass(CalculatedPrice::class)] -#[CoversClass(CalculatedTax::class)] -#[UsesClass(ArrayStruct::class)] class CalculatedPriceTest extends TestCase { public function testSum(): void @@ -67,21 +63,21 @@ public function testSum(): void ]); - $sum = CalculatedPrice::sum([ + $sum = CalculatedPrice::sum(new Collection([ $calculatedPrice1, $calculatedPrice2, - ]); + ])); static::assertSame(25.0, $sum->getUnitPrice()); static::assertSame(65.0, $sum->getTotalPrice()); static::assertSame(1, $sum->getQuantity()); static::assertCount(2, $sum->getCalculatedTaxes()); - static::assertSame(19.0, $sum->getCalculatedTaxes()[19.0]->getTaxRate()); - static::assertSame(2.75, $sum->getCalculatedTaxes()[19.0]->getTax()); - static::assertSame(15.0, $sum->getCalculatedTaxes()[19.0]->getPrice()); - static::assertSame(7.0, $sum->getCalculatedTaxes()[7.0]->getTaxRate()); - static::assertSame(0.7, $sum->getCalculatedTaxes()[7.0]->getTax()); - static::assertSame(10.0, $sum->getCalculatedTaxes()[7.0]->getPrice()); + static::assertSame(19.0, $sum->getCalculatedTaxes()->get('19.0')?->getTaxRate()); + static::assertSame(2.75, $sum->getCalculatedTaxes()->get('19.0')?->getTax()); + static::assertSame(15.0, $sum->getCalculatedTaxes()->get('19.0')?->getPrice()); + static::assertSame(7.0, $sum->getCalculatedTaxes()->get('7.0')?->getTaxRate()); + static::assertSame(0.7, $sum->getCalculatedTaxes()->get('7.0')?->getTax()); + static::assertSame(10.0, $sum->getCalculatedTaxes()->get('7.0')?->getPrice()); static::assertCount(3, $sum->getTaxRules()); } } diff --git a/tests/Context/Cart/LineItemTest.php b/tests/Context/Cart/LineItemTest.php index 6040d89..d0d2985 100644 --- a/tests/Context/Cart/LineItemTest.php +++ b/tests/Context/Cart/LineItemTest.php @@ -6,11 +6,9 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\Cart\LineItem; #[CoversClass(LineItem::class)] -#[CoversClass(ArrayStruct::class)] class LineItemTest extends TestCase { public function testGetChildren(): void diff --git a/tests/Context/ContextResolverTest.php b/tests/Context/ContextResolverTest.php index 049ebf7..c10bc04 100644 --- a/tests/Context/ContextResolverTest.php +++ b/tests/Context/ContextResolverTest.php @@ -9,109 +9,17 @@ use PHPUnit\Framework\Attributes\DataProvider; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\StreamInterface; -use Shopware\App\SDK\Context\ActionButton\ActionButtonAction; -use Shopware\App\SDK\Context\ActionSource; -use Shopware\App\SDK\Context\ArrayStruct; -use Shopware\App\SDK\Context\Cart\CalculatedTax; -use Shopware\App\SDK\Context\Cart\Cart; -use Shopware\App\SDK\Context\Cart\CartPrice; -use Shopware\App\SDK\Context\Cart\CartTransaction; -use Shopware\App\SDK\Context\Cart\Delivery; -use Shopware\App\SDK\Context\Cart\DeliveryDate; -use Shopware\App\SDK\Context\Cart\DeliveryPosition; -use Shopware\App\SDK\Context\Cart\LineItem; -use Shopware\App\SDK\Context\Cart\CalculatedPrice; -use Shopware\App\SDK\Context\Cart\TaxRule; use Shopware\App\SDK\Context\ContextResolver; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\Module\ModuleAction; -use Shopware\App\SDK\Context\Order\Order; -use Shopware\App\SDK\Context\Order\OrderCustomer; -use Shopware\App\SDK\Context\Order\OrderDelivery; -use Shopware\App\SDK\Context\Order\OrderLineItem; -use Shopware\App\SDK\Context\Order\OrderTransaction; -use Shopware\App\SDK\Context\Order\StateMachineState; use Shopware\App\SDK\Context\Payment\PaymentCaptureAction; use Shopware\App\SDK\Context\Payment\PaymentFinalizeAction; use Shopware\App\SDK\Context\Payment\PaymentPayAction; use Shopware\App\SDK\Context\Payment\PaymentRecurringAction; -use Shopware\App\SDK\Context\Payment\PaymentValidateAction; -use Shopware\App\SDK\Context\Payment\RecurringData; -use Shopware\App\SDK\Context\Payment\Refund; -use Shopware\App\SDK\Context\Payment\RefundAction; -use Shopware\App\SDK\Context\Payment\RefundTransactionCapture; -use Shopware\App\SDK\Context\SalesChannelContext\Address; -use Shopware\App\SDK\Context\SalesChannelContext\Country; -use Shopware\App\SDK\Context\SalesChannelContext\CountryState; -use Shopware\App\SDK\Context\SalesChannelContext\Currency; -use Shopware\App\SDK\Context\SalesChannelContext\Customer; -use Shopware\App\SDK\Context\SalesChannelContext\PaymentMethod; -use Shopware\App\SDK\Context\SalesChannelContext\RoundingConfig; -use Shopware\App\SDK\Context\SalesChannelContext\SalesChannel; -use Shopware\App\SDK\Context\SalesChannelContext\SalesChannelContext; -use Shopware\App\SDK\Context\SalesChannelContext\SalesChannelDomain; -use Shopware\App\SDK\Context\SalesChannelContext\Salutation; -use Shopware\App\SDK\Context\SalesChannelContext\ShippingLocation; -use Shopware\App\SDK\Context\SalesChannelContext\ShippingMethod; -use Shopware\App\SDK\Context\SalesChannelContext\TaxInfo; -use Shopware\App\SDK\Context\Storefront\StorefrontAction; -use Shopware\App\SDK\Context\Storefront\StorefrontClaims; -use Shopware\App\SDK\Context\TaxProvider\TaxProviderAction; -use Shopware\App\SDK\Context\Webhook\WebhookAction; use Shopware\App\SDK\Exception\MalformedWebhookBodyException; use Shopware\App\SDK\Shop\ShopInterface; use Shopware\App\SDK\Test\MockShop; #[CoversClass(ContextResolver::class)] -#[CoversClass(ActionSource::class)] -#[CoversClass(WebhookAction::class)] -#[CoversClass(ActionButtonAction::class)] -#[CoversClass(ModuleAction::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(MalformedWebhookBodyException::class)] -#[CoversClass(ArrayStruct::class)] -#[CoversClass(Cart::class)] -#[CoversClass(LineItem::class)] -#[CoversClass(CalculatedPrice::class)] -#[CoversClass(TaxProviderAction::class)] -#[CoversClass(CalculatedTax::class)] -#[CoversClass(TaxRule::class)] -#[CoversClass(CartPrice::class)] -#[CoversClass(Delivery::class)] -#[CoversClass(ShippingMethod::class)] -#[CoversClass(DeliveryDate::class)] -#[CoversClass(DeliveryPosition::class)] -#[CoversClass(Country::class)] -#[CoversClass(ShippingLocation::class)] -#[CoversClass(CartTransaction::class)] -#[CoversClass(SalesChannelContext::class)] -#[CoversClass(Currency::class)] -#[CoversClass(RoundingConfig::class)] -#[CoversClass(PaymentMethod::class)] -#[CoversClass(Customer::class)] -#[CoversClass(Salutation::class)] -#[CoversClass(Address::class)] -#[CoversClass(CountryState::class)] -#[CoversClass(TaxInfo::class)] -#[CoversClass(SalesChannel::class)] -#[CoversClass(SalesChannelDomain::class)] -#[CoversClass(PaymentPayAction::class)] -#[CoversClass(Order::class)] -#[CoversClass(OrderDelivery::class)] -#[CoversClass(OrderLineItem::class)] -#[CoversClass(OrderTransaction::class)] -#[CoversClass(StateMachineState::class)] -#[CoversClass(PaymentFinalizeAction::class)] -#[CoversClass(PaymentCaptureAction::class)] -#[CoversClass(PaymentValidateAction::class)] -#[CoversClass(PaymentRecurringAction::class)] -#[CoversClass(RefundAction::class)] -#[CoversClass(Refund::class)] -#[CoversClass(RefundTransactionCapture::class)] -#[CoversClass(RecurringData::class)] -#[CoversClass(StorefrontAction::class)] -#[CoversClass(StorefrontClaims::class)] -#[CoversClass(OrderCustomer::class)] class ContextResolverTest extends TestCase { public function testAssembleWebhookMalformed(): void @@ -252,20 +160,19 @@ public function testAssembleTaxProvider(): void $lineItems = $tax->cart->getLineItems(); static::assertCount(1, $lineItems); - static::assertArrayHasKey('0', $lineItems); - static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $lineItems['0']->getId()); - static::assertSame('a5209fb05f4f473f9702c3868ea2deac', $lineItems['0']->getUniqueIdentifier()); - static::assertSame('product', $lineItems['0']->getType()); - static::assertIsArray($lineItems['0']->getPayload()); - static::assertSame(1, $lineItems['0']->getQuantity()); - static::assertSame('Aerodynamic Bronze Resorcerer', $lineItems['0']->getLabel()); - static::assertSame(['is-physical'], $lineItems['0']->getStates()); - static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $lineItems['0']->getReferencedId()); - static::assertSame(true, $lineItems['0']->isGood()); - static::assertSame("A description", $lineItems['0']->getDescription()); - static::assertSame([], $lineItems['0']->getChildren()); - - $price = $lineItems['0']->getPrice(); + static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $lineItems->first()?->getId()); + static::assertSame('a5209fb05f4f473f9702c3868ea2deac', $lineItems->first()?->getUniqueIdentifier()); + static::assertSame('product', $lineItems->first()?->getType()); + static::assertIsArray($lineItems->first()?->getPayload()); + static::assertSame(1, $lineItems->first()?->getQuantity()); + static::assertSame('Aerodynamic Bronze Resorcerer', $lineItems->first()?->getLabel()); + static::assertSame(['is-physical'], $lineItems->first()?->getStates()); + static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $lineItems->first()?->getReferencedId()); + static::assertSame(true, $lineItems->first()?->isGood()); + static::assertSame("A description", $lineItems->first()?->getDescription()); + static::assertEmpty($lineItems->first()?->getChildren()); + + $price = $lineItems->first()?->getPrice(); static::assertSame(623.53, $price->getTotalPrice()); static::assertSame(623.53, $price->getUnitPrice()); @@ -273,18 +180,16 @@ public function testAssembleTaxProvider(): void $calculatedTaxes = $price->getCalculatedTaxes(); static::assertCount(1, $calculatedTaxes); - static::assertArrayHasKey('0', $calculatedTaxes); - static::assertSame(0.0, $calculatedTaxes['0']->getTaxRate()); - static::assertSame(0.0, $calculatedTaxes['0']->getTax()); - static::assertSame(623.53, $calculatedTaxes['0']->getPrice()); + static::assertSame(0.0, $calculatedTaxes->first()?->getTaxRate()); + static::assertSame(0.0, $calculatedTaxes->first()?->getTax()); + static::assertSame(623.53, $calculatedTaxes->first()?->getPrice()); $taxRules = $price->getTaxRules(); static::assertCount(1, $taxRules); - static::assertArrayHasKey('0', $taxRules); - $taxRule = $taxRules['0']; - static::assertSame(0.0, $taxRule->getTaxRate()); - static::assertSame(100.0, $taxRule->getPercentage()); + $taxRule = $taxRules->first(); + static::assertSame(0.0, $taxRule?->getTaxRate()); + static::assertSame(100.0, $taxRule?->getPercentage()); $price = $tax->cart->getPrice(); @@ -296,48 +201,45 @@ public function testAssembleTaxProvider(): void $taxRules = $price->getTaxRules(); static::assertCount(1, $taxRules); - static::assertArrayHasKey('0', $taxRules); - $taxRule = $taxRules['0']; - static::assertSame(0.0, $taxRule->getTaxRate()); - static::assertSame(100.0, $taxRule->getPercentage()); + $taxRule = $taxRules->first(); + + static::assertSame(0.0, $taxRule?->getTaxRate()); + static::assertSame(100.0, $taxRule?->getPercentage()); $taxRules = $price->getCalculatedTaxes(); static::assertCount(1, $taxRules); - static::assertArrayHasKey('0', $taxRules); - $taxRule = $taxRules['0']; + $taxRule = $taxRules->first(); - static::assertSame(0.0, $taxRule->getTaxRate()); - static::assertSame(0.0, $taxRule->getTax()); - static::assertSame(623.53, $taxRule->getPrice()); + static::assertSame(0.0, $taxRule?->getTaxRate()); + static::assertSame(0.0, $taxRule?->getTax()); + static::assertSame(623.53, $taxRule?->getPrice()); $deliveries = $tax->cart->getDeliveries(); static::assertCount(1, $deliveries); - static::assertArrayHasKey('0', $deliveries); - $delivery = $deliveries['0']; + $delivery = $deliveries->first(); - static::assertSame('Standard', $delivery->getShippingMethod()->getName()); + static::assertSame('Standard', $delivery?->getShippingMethod()->getName()); - $deliveryDate = $delivery->getDeliveryDate(); - static::assertSame('2023-05-03T16:00:00+00:00', $deliveryDate->getEarliest()->format(\DATE_ATOM)); - static::assertSame('2023-05-05T16:00:00+00:00', $deliveryDate->getLatest()->format(\DATE_ATOM)); + $deliveryDate = $delivery?->getDeliveryDate(); + static::assertSame('2023-05-03T16:00:00+00:00', $deliveryDate?->getEarliest()->format(\DATE_ATOM)); + static::assertSame('2023-05-05T16:00:00+00:00', $deliveryDate?->getLatest()->format(\DATE_ATOM)); $positions = $delivery->getPositions(); static::assertCount(1, $positions); - static::assertArrayHasKey('0', $positions); - $position = $positions['0']; + $position = $positions->first(); - static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $position->getIdentifier()); - static::assertSame(1, $position->getQuantity()); - static::assertSame(1683129600, $position->getDeliveryDate()->getEarliest()->getTimestamp()); - static::assertSame(1683302400, $position->getDeliveryDate()->getLatest()->getTimestamp()); + static::assertSame('91298e263c5b4bb88c3f51c873d7e76e', $position?->getIdentifier()); + static::assertSame(1, $position?->getQuantity()); + static::assertSame(1683129600, $position?->getDeliveryDate()->getEarliest()->getTimestamp()); + static::assertSame(1683302400, $position?->getDeliveryDate()->getLatest()->getTimestamp()); - static::assertSame('Aerodynamic Bronze Resorcerer', $position->getLineItem()->getLabel()); - static::assertSame(1, $position->getPrice()->getQuantity()); + static::assertSame('Aerodynamic Bronze Resorcerer', $position?->getLineItem()->getLabel()); + static::assertSame(1, $position?->getPrice()->getQuantity()); $location = $delivery->getLocation(); @@ -351,28 +253,26 @@ public function testAssembleTaxProvider(): void $calculatedTaxes = $shippingCosts->getCalculatedTaxes(); static::assertCount(1, $calculatedTaxes); - static::assertArrayHasKey('0', $calculatedTaxes); - static::assertSame(0.0, $calculatedTaxes['0']->getTaxRate()); - static::assertSame(0.0, $calculatedTaxes['0']->getTax()); - static::assertSame(0.0, $calculatedTaxes['0']->getPrice()); + + static::assertSame(0.0, $calculatedTaxes->first()?->getTaxRate()); + static::assertSame(0.0, $calculatedTaxes->first()?->getTax()); + static::assertSame(0.0, $calculatedTaxes->first()?->getPrice()); $taxRules = $shippingCosts->getTaxRules(); static::assertCount(1, $taxRules); - static::assertArrayHasKey('0', $taxRules); - $taxRule = $taxRules['0']; - static::assertSame(0.0, $taxRule->getTaxRate()); - static::assertSame(100.0, $taxRule->getPercentage()); + $taxRule = $taxRules->first(); + static::assertSame(0.0, $taxRule?->getTaxRate()); + static::assertSame(100.0, $taxRule?->getPercentage()); $transactions = $tax->cart->getTransactions(); static::assertCount(1, $transactions); - static::assertArrayHasKey('0', $transactions); - $transaction = $transactions['0']; + $transaction = $transactions->first(); - static::assertSame(623.53, $transaction->getAmount()->getTotalPrice()); - static::assertSame('20c5b5b9ec9d4f39b36816488cd58133', $transaction->getPaymentMethodId()); + static::assertSame(623.53, $transaction?->getAmount()->getTotalPrice()); + static::assertSame('20c5b5b9ec9d4f39b36816488cd58133', $transaction?->getPaymentMethodId()); $context = $tax->context; @@ -424,6 +324,7 @@ public function testAssembleTaxProvider(): void static::assertSame('::', $customer->getRemoteAddress()); static::assertSame('Max', $customer->getFirstName()); static::assertSame('Mustermann', $customer->getLastName()); + static::assertNotNull($customer->getSalutation()); static::assertSame('c0928c9d2c264e3aade1fab28a9262dd', $customer->getSalutation()->getId()); static::assertSame('mr', $customer->getSalutation()->getSalutationKey()); static::assertSame('Mr.', $customer->getSalutation()->getDisplayName()); @@ -461,6 +362,7 @@ public function testAssembleTaxProvider(): void static::assertSame(false, $billingAddress->getCountry()->getCustomerTax()->isEnabled()); static::assertSame(false, $billingAddress->getCountry()->getCompanyTax()->isEnabled()); static::assertSame('040cbcada23440ebb4f6e1bebc62e421', $billingAddress->getCountryState()?->getId()); + static::assertNotNull($billingAddress->getCountryState()); static::assertSame('Pennsylvania', $billingAddress->getCountryState()?->getName()); static::assertSame([], $billingAddress->getCountryState()?->getCustomFields()); static::assertSame(1, $billingAddress->getCountryState()?->getPosition()); @@ -475,12 +377,12 @@ public function testAssembleTaxProvider(): void $domains = $salesChannel->getDomains(); static::assertCount(1, $domains); - static::assertSame('ef8e67662fdb4b0fb9241d7bd75fe8bb', $domains[0]->getId()); - static::assertSame('http://localhost:8000', $domains[0]->getUrl()); - static::assertSame('b7d2554b0ce847cd82f3ac9bd1c0dfca', $domains[0]->getCurrencyId()); - static::assertSame([], $domains[0]->getCustomFields()); - static::assertSame('2fbb5fe2e29a4d70aa5854ce7ce3e20b', $domains[0]->getLanguageId()); - static::assertSame('684f9a80c59846228223cb76d7cb3577', $domains[0]->getSnippetSetId()); + static::assertSame('ef8e67662fdb4b0fb9241d7bd75fe8bb', $domains->first()?->getId()); + static::assertSame('http://localhost:8000', $domains->first()?->getUrl()); + static::assertSame('b7d2554b0ce847cd82f3ac9bd1c0dfca', $domains->first()?->getCurrencyId()); + static::assertSame([], $domains->first()?->getCustomFields()); + static::assertSame('2fbb5fe2e29a4d70aa5854ce7ce3e20b', $domains->first()?->getLanguageId()); + static::assertSame('684f9a80c59846228223cb76d7cb3577', $domains->first()?->getSnippetSetId()); $rounding = $context->getRounding(); static::assertSame(2, $rounding->getDecimals()); diff --git a/tests/Context/Order/OrderCustomerTest.php b/tests/Context/Order/OrderCustomerTest.php index edd0382..2a88b72 100644 --- a/tests/Context/Order/OrderCustomerTest.php +++ b/tests/Context/Order/OrderCustomerTest.php @@ -5,17 +5,10 @@ namespace Shopware\App\SDK\Tests\Context\Order; use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\Order\OrderCustomer; -use Shopware\App\SDK\Context\SalesChannelContext\Customer; -use Shopware\App\SDK\Context\SalesChannelContext\Salutation; #[CoversClass(OrderCustomer::class)] -#[UsesClass(ArrayStruct::class)] -#[UsesClass(Customer::class)] -#[UsesClass(Salutation::class)] class OrderCustomerTest extends TestCase { public function testGetId(): void diff --git a/tests/Context/SalesChannelContext/AddressTest.php b/tests/Context/SalesChannelContext/AddressTest.php index d0157a3..9efd30f 100644 --- a/tests/Context/SalesChannelContext/AddressTest.php +++ b/tests/Context/SalesChannelContext/AddressTest.php @@ -5,12 +5,10 @@ namespace Shopware\App\SDK\Tests\Context\SalesChannelContext; use PHPUnit\Framework\Attributes\CoversClass; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\SalesChannelContext\Address; use PHPUnit\Framework\TestCase; #[CoversClass(Address::class)] -#[CoversClass(ArrayStruct::class)] class AddressTest extends TestCase { public function testEmptySalutation(): void diff --git a/tests/Context/SalesChannelContext/CurrencyTest.php b/tests/Context/SalesChannelContext/CurrencyTest.php index ec492ce..3f6752a 100644 --- a/tests/Context/SalesChannelContext/CurrencyTest.php +++ b/tests/Context/SalesChannelContext/CurrencyTest.php @@ -6,11 +6,9 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\SalesChannelContext\Currency; #[CoversClass(Currency::class)] -#[CoversClass(ArrayStruct::class)] class CurrencyTest extends TestCase { public function testGetTaxFreeFrom(): void diff --git a/tests/Context/SalesChannelContext/CustomerTest.php b/tests/Context/SalesChannelContext/CustomerTest.php index c11ffe0..fb003e6 100644 --- a/tests/Context/SalesChannelContext/CustomerTest.php +++ b/tests/Context/SalesChannelContext/CustomerTest.php @@ -5,12 +5,10 @@ namespace Shopware\App\SDK\Tests\Context\SalesChannelContext; use PHPUnit\Framework\Attributes\CoversClass; -use Shopware\App\SDK\Context\ArrayStruct; use Shopware\App\SDK\Context\SalesChannelContext\Customer; use PHPUnit\Framework\TestCase; #[CoversClass(Customer::class)] -#[CoversClass(ArrayStruct::class)] class CustomerTest extends TestCase { public function testEmptySalutation(): void diff --git a/tests/Context/SalesChannelContext/SalesChannelContextTest.php b/tests/Context/SalesChannelContext/SalesChannelContextTest.php index 5e0541d..0c8459b 100644 --- a/tests/Context/SalesChannelContext/SalesChannelContextTest.php +++ b/tests/Context/SalesChannelContext/SalesChannelContextTest.php @@ -7,12 +7,9 @@ use PHPUnit\Framework\Attributes\CoversClass; use Shopware\App\SDK\Context\ArrayStruct; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\Context\SalesChannelContext\RoundingConfig; use Shopware\App\SDK\Context\SalesChannelContext\SalesChannelContext; #[CoversClass(ArrayStruct::class)] -#[CoversClass(RoundingConfig::class)] -#[CoversClass(SalesChannelContext::class)] class SalesChannelContextTest extends TestCase { public function testCustomer(): void diff --git a/tests/Context/Storefront/StorefrontClaimsTest.php b/tests/Context/Storefront/StorefrontClaimsTest.php index f4df65b..caf60cf 100644 --- a/tests/Context/Storefront/StorefrontClaimsTest.php +++ b/tests/Context/Storefront/StorefrontClaimsTest.php @@ -7,10 +7,8 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Context\Storefront\StorefrontClaims; -use Shopware\App\SDK\Exception\MissingClaimException; #[CoversClass(StorefrontClaims::class)] -#[CoversClass(MissingClaimException::class)] class StorefrontClaimsTest extends TestCase { public function testAllSet(): void diff --git a/tests/Context/Trait/CustomFieldsAwareTraitTest.php b/tests/Context/Trait/CustomFieldsAwareTraitTest.php index 05932ee..4397980 100644 --- a/tests/Context/Trait/CustomFieldsAwareTraitTest.php +++ b/tests/Context/Trait/CustomFieldsAwareTraitTest.php @@ -7,11 +7,9 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Context\ArrayStruct; -use Shopware\App\SDK\Context\Trait\CustomFieldsAware; use Shopware\App\SDK\Tests\Context\_fixtures\TestCustomFieldsAware; #[CoversClass(ArrayStruct::class)] -#[CoversClass(CustomFieldsAware::class)] class CustomFieldsAwareTraitTest extends TestCase { public function testGetCustomFields(): void diff --git a/tests/Event/AbstractAppLifecycleEventTest.php b/tests/Event/AbstractAppLifecycleEventTest.php new file mode 100644 index 0000000..dccc182 --- /dev/null +++ b/tests/Event/AbstractAppLifecycleEventTest.php @@ -0,0 +1,26 @@ +getRequest()); + static::assertSame($shop, $event->getShop()); + } +} diff --git a/tests/Events/BeforeRegistrationCompletedEventTest.php b/tests/Event/BeforeRegistrationCompletedEventTest.php similarity index 86% rename from tests/Events/BeforeRegistrationCompletedEventTest.php rename to tests/Event/BeforeRegistrationCompletedEventTest.php index 64d94bc..324a86f 100644 --- a/tests/Events/BeforeRegistrationCompletedEventTest.php +++ b/tests/Event/BeforeRegistrationCompletedEventTest.php @@ -2,18 +2,15 @@ declare(strict_types=1); -namespace Shopware\App\SDK\Tests\Events; +namespace Shopware\App\SDK\Tests\Event; use Nyholm\Psr7\Request; use PHPUnit\Framework\Attributes\CoversClass; -use Shopware\App\SDK\Event\AbstractAppLifecycleEvent; use Shopware\App\SDK\Event\BeforeRegistrationCompletedEvent; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Test\MockShop; #[CoversClass(BeforeRegistrationCompletedEvent::class)] -#[CoversClass(AbstractAppLifecycleEvent::class)] -#[CoversClass(MockShop::class)] class BeforeRegistrationCompletedEventTest extends TestCase { public function testEvent(): void diff --git a/tests/Events/RegistrationCompletedEventTest.php b/tests/Event/RegistrationCompletedEventTest.php similarity index 84% rename from tests/Events/RegistrationCompletedEventTest.php rename to tests/Event/RegistrationCompletedEventTest.php index a5a09c6..5d97671 100644 --- a/tests/Events/RegistrationCompletedEventTest.php +++ b/tests/Event/RegistrationCompletedEventTest.php @@ -2,18 +2,15 @@ declare(strict_types=1); -namespace Shopware\App\SDK\Tests\Events; +namespace Shopware\App\SDK\Tests\Event; use Nyholm\Psr7\Request; use PHPUnit\Framework\Attributes\CoversClass; -use Shopware\App\SDK\Event\AbstractAppLifecycleEvent; use Shopware\App\SDK\Event\RegistrationCompletedEvent; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Test\MockShop; #[CoversClass(RegistrationCompletedEvent::class)] -#[CoversClass(AbstractAppLifecycleEvent::class)] -#[CoversClass(MockShop::class)] class RegistrationCompletedEventTest extends TestCase { public function testEvent(): void diff --git a/tests/Exception/MissingClaimExceptionTest.php b/tests/Exception/MissingClaimExceptionTest.php new file mode 100644 index 0000000..fc7f3c1 --- /dev/null +++ b/tests/Exception/MissingClaimExceptionTest.php @@ -0,0 +1,20 @@ +getMessage()); + } +} diff --git a/tests/Framework/CollectionTest.php b/tests/Framework/CollectionTest.php new file mode 100644 index 0000000..ae52fcb --- /dev/null +++ b/tests/Framework/CollectionTest.php @@ -0,0 +1,148 @@ +all()); + } + + public function testAll(): void + { + $collection = new Collection(['1', 2.3, 'foo']); + + static::assertEquals(['1', 2.3, 'foo'], $collection->all()); + } + + public function testAdd(): void + { + $collection = new Collection(); + + $collection->add('foo'); + $collection->add('bar'); + $collection->add('baz'); + + static::assertEquals(['foo', 'bar', 'baz'], $collection->all()); + } + + public function testSet(): void + { + $collection = new Collection(); + + $collection->set(0, 'foo'); + $collection->set(1, 'bar'); + $collection->set(0, 'baz'); + $collection->set('foo', 'bar'); + + static::assertEquals(['baz', 'bar', 'foo' => 'bar'], $collection->all()); + } + + public function testGet(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertEquals('bar', $collection->get(1)); + static::assertEquals('qux', $collection->get('baz')); + + static::assertEquals(null, $collection->get(3)); + static::assertEquals(null, $collection->get('invalid')); + } + + public function testFirst(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertEquals('foo', $collection->first()); + } + + public function testLast(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertEquals('qux', $collection->last()); + } + + public function testRemove(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + $collection->remove(0); + $collection->remove('baz'); + + $collection->remove(3); + $collection->remove('invalid'); + + static::assertEquals([1 => 'bar'], $collection->all()); + } + + public function testHas(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertTrue($collection->has(0)); + static::assertTrue($collection->has(1)); + static::assertTrue($collection->has('baz')); + + static::assertFalse($collection->has(3)); + static::assertFalse($collection->has('invalid')); + } + + public function testMap(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + $new = $collection->map(function (string $value) { + return $value . '_new'; + }); + + static::assertEquals(['foo_new', 'bar_new', 'baz' => 'qux_new'], $new); + } + + public function testFilter(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + $new = $collection->filter(function (string $value) { + return $value === 'bar'; + }); + + static::assertEquals([1 => 'bar'], $new->all()); + } + + public function testGetIterator(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + $result = []; + foreach ($collection as $key => $value) { + $result[$key] = $value; + } + + static::assertEquals(['foo', 'bar', 'baz' => 'qux'], $result); + } + + public function testCount(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertEquals(3, $collection->count()); + } + + public function testJsonSerialize(): void + { + $collection = new Collection(['foo', 'bar', 'baz' => 'qux']); + + static::assertEquals(['foo', 'bar', 'baz' => 'qux'], $collection->jsonSerialize()); + } +} diff --git a/tests/Gateway/Checkout/CheckoutGatewayCommandTest.php b/tests/Gateway/Checkout/CheckoutGatewayCommandTest.php new file mode 100644 index 0000000..e2a225e --- /dev/null +++ b/tests/Gateway/Checkout/CheckoutGatewayCommandTest.php @@ -0,0 +1,67 @@ +getCommand(); + $command->setPayloadValue('key', 'value'); + + static::assertEquals('value', $command->getPayloadValue('key')); + } + + public function testHasPayloadValue(): void + { + $command = $this->getCommand(); + $command->setPayloadValue('key', 'value'); + + static::assertTrue($command->hasPayloadValue('key')); + } + + public function testNotHasPayloadValue(): void + { + $command = $this->getCommand(); + $command->setPayloadValue('key', 'value'); + + static::assertFalse($command->hasPayloadValue('foo')); + } + + public function testGetPayloadValue(): void + { + $command = $this->getCommand(); + $command->setPayloadValue('key', 'value'); + + static::assertEquals('value', $command->getPayloadValue('key')); + } + + public function testGetNonExistentPayloadValue(): void + { + $command = $this->getCommand(); + $command->setPayloadValue('key', 'value'); + + static::assertNull($command->getPayloadValue('foo')); + } + + public function testJsonSerialize(): void + { + $command = $this->getCommand(); + $command->keyName = 'key'; + $command->setPayloadValue('key', 'value'); + + static::assertEquals(['command' => 'key', 'payload' => ['key' => 'value']], $command->jsonSerialize()); + } + + private function getCommand(): CheckoutGatewayCommand + { + return new class () extends CheckoutGatewayCommand {}; + } +} diff --git a/tests/Gateway/Checkout/Command/AddCartErrorCommandTest.php b/tests/Gateway/Checkout/Command/AddCartErrorCommandTest.php new file mode 100644 index 0000000..9cc0c2e --- /dev/null +++ b/tests/Gateway/Checkout/Command/AddCartErrorCommandTest.php @@ -0,0 +1,48 @@ +message); + static::assertTrue($command->blocking); + static::assertSame(Error::LEVEL_ERROR, $command->level); + static::assertSame('add-cart-error', $command->keyName); + } + + public function testConstructWithDefaults(): void + { + $command = new AddCartErrorCommand('foo'); + + static::assertSame('foo', $command->message); + static::assertFalse($command->blocking); + static::assertSame(Error::LEVEL_WARNING, $command->level); + static::assertSame('add-cart-error', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new AddCartErrorCommand('foo', true, Error::LEVEL_ERROR); + + static::assertSame('foo', $command->getPayloadValue('message')); + static::assertTrue($command->getPayloadValue('blocking')); + static::assertSame(Error::LEVEL_ERROR, $command->getPayloadValue('level')); + } + + public function testKey(): void + { + static::assertSame('add-cart-error', AddCartErrorCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/AddPaymentMethodCommandTest.php b/tests/Gateway/Checkout/Command/AddPaymentMethodCommandTest.php new file mode 100644 index 0000000..5c621f3 --- /dev/null +++ b/tests/Gateway/Checkout/Command/AddPaymentMethodCommandTest.php @@ -0,0 +1,33 @@ +paymentMethodTechnicalName); + static::assertSame('add-payment-method', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new AddPaymentMethodCommand('paypal'); + + static::assertSame('paypal', $command->getPayloadValue('paymentMethodTechnicalName')); + } + + public function testKey(): void + { + static::assertSame('add-payment-method', AddPaymentMethodCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/AddPaymentMethodExtensionCommandTest.php b/tests/Gateway/Checkout/Command/AddPaymentMethodExtensionCommandTest.php new file mode 100644 index 0000000..9878840 --- /dev/null +++ b/tests/Gateway/Checkout/Command/AddPaymentMethodExtensionCommandTest.php @@ -0,0 +1,37 @@ +paymentMethodTechnicalName); + static::assertSame('foo', $command->extensionKey); + static::assertSame(['bar'], $command->extensionsPayload); + static::assertSame('add-payment-method-extension', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new AddPaymentMethodExtensionCommand('paypal', 'foo', ['bar']); + + static::assertSame('paypal', $command->getPayloadValue('paymentMethodTechnicalName')); + static::assertSame('foo', $command->getPayloadValue('extensionKey')); + static::assertSame(['bar'], $command->getPayloadValue('extensionsPayload')); + } + + public function testKey(): void + { + static::assertSame('add-payment-method-extension', AddPaymentMethodExtensionCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/AddShippingMethodCommandTest.php b/tests/Gateway/Checkout/Command/AddShippingMethodCommandTest.php new file mode 100644 index 0000000..28d0632 --- /dev/null +++ b/tests/Gateway/Checkout/Command/AddShippingMethodCommandTest.php @@ -0,0 +1,33 @@ +shippingMethodTechnicalName); + static::assertSame('add-shipping-method', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new AddShippingMethodCommand('dhl'); + + static::assertSame('dhl', $command->getPayloadValue('shippingMethodTechnicalName')); + } + + public function testKey(): void + { + static::assertSame('add-shipping-method', AddShippingMethodCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/AddShippingMethodExtensionCommandTest.php b/tests/Gateway/Checkout/Command/AddShippingMethodExtensionCommandTest.php new file mode 100644 index 0000000..9ab312c --- /dev/null +++ b/tests/Gateway/Checkout/Command/AddShippingMethodExtensionCommandTest.php @@ -0,0 +1,37 @@ +shippingMethodTechnicalName); + static::assertSame('foo', $command->extensionKey); + static::assertSame(['bar'], $command->extensionsPayload); + static::assertSame('add-shipping-method-extension', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new AddShippingMethodExtensionCommand('dhl', 'foo', ['bar']); + + static::assertSame('dhl', $command->getPayloadValue('shippingMethodTechnicalName')); + static::assertSame('foo', $command->getPayloadValue('extensionKey')); + static::assertSame(['bar'], $command->getPayloadValue('extensionsPayload')); + } + + public function testKey(): void + { + static::assertSame('add-shipping-method-extension', AddShippingMethodExtensionCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/RemovePaymentMethodCommandTest.php b/tests/Gateway/Checkout/Command/RemovePaymentMethodCommandTest.php new file mode 100644 index 0000000..ef995ca --- /dev/null +++ b/tests/Gateway/Checkout/Command/RemovePaymentMethodCommandTest.php @@ -0,0 +1,33 @@ +paymentMethodTechnicalName); + static::assertSame('remove-payment-method', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new RemovePaymentMethodCommand('paypal'); + + static::assertSame('paypal', $command->getPayloadValue('paymentMethodTechnicalName')); + } + + public function testKey(): void + { + static::assertSame('remove-payment-method', RemovePaymentMethodCommand::KEY); + } +} diff --git a/tests/Gateway/Checkout/Command/RemoveShippingMethodCommandTest.php b/tests/Gateway/Checkout/Command/RemoveShippingMethodCommandTest.php new file mode 100644 index 0000000..3f40d04 --- /dev/null +++ b/tests/Gateway/Checkout/Command/RemoveShippingMethodCommandTest.php @@ -0,0 +1,33 @@ +shippingMethodTechnicalName); + static::assertSame('remove-shipping-method', $command->keyName); + } + + public function testPayloadOnConstruct(): void + { + $command = new RemoveShippingMethodCommand('dhl'); + + static::assertSame('dhl', $command->getPayloadValue('shippingMethodTechnicalName')); + } + + public function testKey(): void + { + static::assertSame('remove-shipping-method', RemoveShippingMethodCommand::KEY); + } +} diff --git a/tests/HttpClient/AuthenticatedClientTest.php b/tests/HttpClient/AuthenticatedClientTest.php index fe41409..8e6e028 100644 --- a/tests/HttpClient/AuthenticatedClientTest.php +++ b/tests/HttpClient/AuthenticatedClientTest.php @@ -20,10 +20,6 @@ use Shopware\App\SDK\Test\MockShop; #[CoversClass(AuthenticatedClient::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(NullCache::class)] -#[CoversClass(AuthenticationFailedException::class)] -#[CoversClass(MockClient::class)] class AuthenticatedClientTest extends TestCase { public function testAuthenticationFails(): void diff --git a/tests/HttpClient/ClientFactoryTest.php b/tests/HttpClient/ClientFactoryTest.php index 287bf52..b08c3fb 100644 --- a/tests/HttpClient/ClientFactoryTest.php +++ b/tests/HttpClient/ClientFactoryTest.php @@ -9,20 +9,12 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use Psr\Http\Client\ClientInterface; -use Shopware\App\SDK\HttpClient\AuthenticatedClient; use Shopware\App\SDK\HttpClient\ClientFactory; use PHPUnit\Framework\TestCase; -use Shopware\App\SDK\HttpClient\LoggerClient; use Shopware\App\SDK\HttpClient\NullCache; -use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClient; use Shopware\App\SDK\Test\MockShop; #[CoversClass(ClientFactory::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(AuthenticatedClient::class)] -#[CoversClass(NullCache::class)] -#[CoversClass(LoggerClient::class)] -#[CoversClass(SimpleHttpClient::class)] class ClientFactoryTest extends TestCase { /** diff --git a/tests/HttpClient/LoggerClientTest.php b/tests/HttpClient/LoggerClientTest.php index 5f473b9..e5e197b 100644 --- a/tests/HttpClient/LoggerClientTest.php +++ b/tests/HttpClient/LoggerClientTest.php @@ -15,7 +15,6 @@ use Shopware\App\SDK\Test\MockClient; #[CoversClass(LoggerClient::class)] -#[CoversClass(MockClient::class)] class LoggerClientTest extends TestCase { public function testRequestGetsLogged(): void @@ -81,7 +80,7 @@ public function testBodyRewindIsCalled(): void $body = static::createMock(StreamInterface::class); $body - ->expects(static::once()) + ->expects(static::exactly(2)) ->method('rewind'); $body @@ -91,7 +90,7 @@ public function testBodyRewindIsCalled(): void $request = $request->withBody($body); $client = new LoggerClient(new MockClient([ - new Response(200, [], '{"foo": "bar"}') + new Response(200, [], $body) ]), static::createMock(LoggerInterface::class)); $client->sendRequest($request); diff --git a/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php index 5c347b6..a03c884 100644 --- a/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php +++ b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php @@ -9,14 +9,11 @@ use PHPUnit\Framework\Attributes\DataProvider; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; -use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClientResponse; use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClient; use PHPUnit\Framework\TestCase; use Shopware\App\SDK\Test\MockClient; #[CoversClass(SimpleHttpClient::class)] -#[CoversClass(SimpleHttpClientResponse::class)] -#[CoversClass(MockClient::class)] class SimpleHttpClientTest extends TestCase { public function testGet(): void diff --git a/tests/Registration/RegistrationServiceTest.php b/tests/Registration/RegistrationServiceTest.php index 228a7e0..03b3023 100644 --- a/tests/Registration/RegistrationServiceTest.php +++ b/tests/Registration/RegistrationServiceTest.php @@ -13,7 +13,6 @@ use Shopware\App\SDK\AppConfiguration; use Shopware\App\SDK\Authentication\RequestVerifier; use Shopware\App\SDK\Authentication\ResponseSigner; -use Shopware\App\SDK\Event\AbstractAppLifecycleEvent; use Shopware\App\SDK\Event\BeforeRegistrationCompletedEvent; use Shopware\App\SDK\Event\RegistrationCompletedEvent; use Shopware\App\SDK\Exception\MissingShopParameterException; @@ -25,15 +24,6 @@ use Shopware\App\SDK\Test\MockShopRepository; #[CoversClass(RegistrationService::class)] -#[CoversClass(AppConfiguration::class)] -#[CoversClass(ResponseSigner::class)] -#[CoversClass(MissingShopParameterException::class)] -#[CoversClass(ShopNotFoundException::class)] -#[CoversClass(AbstractAppLifecycleEvent::class)] -#[CoversClass(MockShop::class)] -#[CoversClass(MockShopRepository::class)] -#[CoversClass(BeforeRegistrationCompletedEvent::class)] -#[CoversClass(RegistrationCompletedEvent::class)] class RegistrationServiceTest extends TestCase { private RegistrationService $registerService; diff --git a/tests/Response/GatewayResponseTest.php b/tests/Response/GatewayResponseTest.php new file mode 100644 index 0000000..ce085a5 --- /dev/null +++ b/tests/Response/GatewayResponseTest.php @@ -0,0 +1,29 @@ +getStatusCode()); + static::assertSame('[{"command":"add-payment-method","payload":{"paymentMethodTechnicalName":"paypal"}},{"command":"remove-payment-method","payload":{"paymentMethodTechnicalName":"credit_card"}}]', $response->getBody()->getContents()); + } +} diff --git a/tests/Shop/ShopResolverTest.php b/tests/Shop/ShopResolverTest.php index 1977d06..978de06 100644 --- a/tests/Shop/ShopResolverTest.php +++ b/tests/Shop/ShopResolverTest.php @@ -17,10 +17,6 @@ use Shopware\App\SDK\Test\MockShopRepository; #[CoversClass(ShopResolver::class)] -#[CoversClass(MissingShopParameterException::class)] -#[CoversClass(ShopNotFoundException::class)] -#[CoversClass(MockShopRepository::class)] -#[CoversClass(MockShop::class)] class ShopResolverTest extends TestCase { private MockShopRepository $shopRepository; diff --git a/tests/TaxProvider/CalculatedTaxTest.php b/tests/TaxProvider/CalculatedTaxTest.php new file mode 100644 index 0000000..8d8ee55 --- /dev/null +++ b/tests/TaxProvider/CalculatedTaxTest.php @@ -0,0 +1,29 @@ +tax); + static::assertEquals(100.0, $tax->taxRate); + static::assertEquals(19.0, $tax->price); + } + + public function testJsonSerialize(): void + { + $tax = new CalculatedTax(19.0, 100.0, 19.0); + + static::assertEquals(['tax' => 19.0, 'taxRate' => 100.0, 'price' => 19.0], $tax->jsonSerialize()); + } +} diff --git a/tests/TaxProvider/TaxProviderResponseBuilderTest.php b/tests/TaxProvider/TaxProviderResponseBuilderTest.php index 13ceeb3..e65d3aa 100644 --- a/tests/TaxProvider/TaxProviderResponseBuilderTest.php +++ b/tests/TaxProvider/TaxProviderResponseBuilderTest.php @@ -10,7 +10,6 @@ use PHPUnit\Framework\TestCase; #[CoversClass(TaxProviderResponseBuilder::class)] -#[CoversClass(CalculatedTax::class)] class TaxProviderResponseBuilderTest extends TestCase { public function testGlobalTax(): void @@ -36,7 +35,7 @@ public function testLineItemTax(): void static::assertSame(200, $response->getStatusCode()); static::assertSame( - '{"lineItemTaxes":{"lineItem1":[{"tax":19,"taxRate":100,"price":19}]},"deliveryTaxes":[],"cartPriceTaxes":[]}', + '{"lineItemTaxes":{"lineItem1":{"tax":19,"taxRate":100,"price":19}},"deliveryTaxes":[],"cartPriceTaxes":[]}', $response->getBody()->getContents() ); } @@ -50,7 +49,7 @@ public function testDeliveryTax(): void static::assertSame(200, $response->getStatusCode()); static::assertSame( - '{"lineItemTaxes":[],"deliveryTaxes":{"delivery1":[{"tax":19,"taxRate":100,"price":19}]},"cartPriceTaxes":[]}', + '{"lineItemTaxes":[],"deliveryTaxes":{"delivery1":{"tax":19,"taxRate":100,"price":19}},"cartPriceTaxes":[]}', $response->getBody()->getContents() ); } diff --git a/tests/Test/MockClientTest.php b/tests/Test/MockClientTest.php index ad5b6a1..c7c1db2 100644 --- a/tests/Test/MockClientTest.php +++ b/tests/Test/MockClientTest.php @@ -39,4 +39,20 @@ public function testUsesQueue(): void static::assertSame(200, $response->getStatusCode()); static::assertSame('{"baz": "qux"}', $response->getBody()->getContents()); } + + public function testIsEmpty(): void + { + $client = new MockClient([]); + + static::assertTrue($client->isEmpty()); + } + + public function testIsNotEmpty(): void + { + $client = new MockClient([ + new Response(200, [], '{"foo": "bar"}'), + ]); + + static::assertFalse($client->isEmpty()); + } } diff --git a/tests/Test/MockShopRepositoryTest.php b/tests/Test/MockShopRepositoryTest.php index a3b0a5f..efe06e8 100644 --- a/tests/Test/MockShopRepositoryTest.php +++ b/tests/Test/MockShopRepositoryTest.php @@ -5,12 +5,10 @@ namespace Shopware\App\SDK\Tests\Test; use PHPUnit\Framework\Attributes\CoversClass; -use Shopware\App\SDK\Test\MockShop; use Shopware\App\SDK\Test\MockShopRepository; use PHPUnit\Framework\TestCase; #[CoversClass(MockShopRepository::class)] -#[CoversClass(MockShop::class)] class MockShopRepositoryTest extends TestCase { private MockShopRepository $repository; @@ -30,6 +28,12 @@ public function testLifecycle(): void static::assertSame($shop, $this->repository->getShopFromId('1')); + $shop->setShopUrl('https://example.org'); + + $this->repository->updateShop($shop); + + static::assertSame('https://example.org', $this->repository->getShopFromId('1')?->getShopUrl()); + $this->repository->deleteShop($shop->getShopId()); static::assertNull($this->repository->getShopFromId('1')); diff --git a/tests/Test/MockShopTest.php b/tests/Test/MockShopTest.php new file mode 100644 index 0000000..f7c272e --- /dev/null +++ b/tests/Test/MockShopTest.php @@ -0,0 +1,65 @@ +getShopId()); + static::assertSame('https://example.com', $shop->getShopUrl()); + static::assertSame('shop-secret', $shop->getShopSecret()); + static::assertFalse($shop->isShopActive()); + static::assertNull($shop->getShopClientId()); + static::assertNull($shop->getShopClientSecret()); + } + + public function testConstructWithCustomValues(): void + { + $shop = new MockShop('shop-id', 'https://example.com', 'shop-secret', true, 'client-id', 'client-secret'); + + static::assertSame('shop-id', $shop->getShopId()); + static::assertSame('https://example.com', $shop->getShopUrl()); + static::assertSame('shop-secret', $shop->getShopSecret()); + static::assertTrue($shop->isShopActive()); + static::assertSame('client-id', $shop->getShopClientId()); + static::assertSame('client-secret', $shop->getShopClientSecret()); + } + + public function testSetShopApiCredentials(): void + { + $shop = new MockShop('shop-id', 'https://example.com', 'shop-secret'); + + $shop->setShopApiCredentials('client-id', 'client-secret'); + + static::assertSame('client-id', $shop->getShopClientId()); + static::assertSame('client-secret', $shop->getShopClientSecret()); + } + + public function testSetShopUrl(): void + { + $shop = new MockShop('shop-id', 'https://example.com', 'shop-secret'); + + $shop->setShopUrl('https://example.org'); + + static::assertSame('https://example.org', $shop->getShopUrl()); + } + + public function testSetShopActive(): void + { + $shop = new MockShop('shop-id', 'https://example.com', 'shop-secret'); + + $shop->setShopActive(true); + + static::assertTrue($shop->isShopActive()); + } +}