Skip to content

Commit

Permalink
Add test to check associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jasperP98 committed May 2, 2024
1 parent 77a66da commit 4e7de26
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/unit/Storefront/Controller/AccountOrderControllerTest.php
Expand Up @@ -5,8 +5,10 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Cart\Transaction\Struct\TransactionCollection;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderCollection;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Checkout\Order\OrderEntity;
Expand All @@ -28,6 +30,7 @@
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Test\Generator;
use Shopware\Storefront\Controller\AccountOrderController;
Expand Down Expand Up @@ -243,6 +246,60 @@ public function testCancelOrderRedirectsToCorrectRouteForGuestCustomer(): void
static::assertInstanceOf(RedirectResponse::class, $response);
static::assertEquals($expectedRedirectUrl, $response->getTargetUrl());
}

public function testTransactionsStateMachineAssociationIsLoadedOnOrderUpdate(): void
{
$container = new ContainerBuilder();
$container->set('event_dispatcher', static::createMock(EventDispatcherInterface::class));
$container->set('router', static::createMock(RouterInterface::class));

$this->controller->setContainer($container);

$ids = new IdsCollection();

$salesChannelContext = Generator::createSalesChannelContext();
$salesChannelContext->assign([
'currency' => (new CurrencyEntity())->assign([
'id' => $ids->get('currency'),
]),
]);

$criteria = new Criteria([$orderId = Uuid::randomHex()]);
$criteria->addAssociation('transactions.stateMachineState');

$order = (new OrderEntity())->assign([
'_uniqueIdentifier' => Uuid::randomHex(),
'currencyId' => $ids->get('currency'),
'deliveries' => new OrderDeliveryCollection(),
]);
$transactionMock = $this->createMock(OrderTransactionEntity::class);

Check failure on line 275 in tests/unit/Storefront/Controller/AccountOrderControllerTest.php

View workflow job for this annotation

GitHub Actions / PHP Analyse

Mocking of Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity is not allowed. The object is very basic and can be constructed
$transactionMock->method('getStateMachineState')->willReturn($this->createMock(StateMachineStateEntity::class));

Check failure on line 276 in tests/unit/Storefront/Controller/AccountOrderControllerTest.php

View workflow job for this annotation

GitHub Actions / PHP Analyse

Mocking of Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity is not allowed. The object is very basic and can be constructed

// Mock the OrderEntity with transactions
$orderMock = $this->createMock(OrderEntity::class);

Check failure on line 279 in tests/unit/Storefront/Controller/AccountOrderControllerTest.php

View workflow job for this annotation

GitHub Actions / PHP Analyse

Mocking of Shopware\Core\Checkout\Order\OrderEntity is not allowed. The object is very basic and can be constructed
$orderMock->method('getCurrencyId')->willReturn('currency_id');
$orderMock->method('getTransactions')->willReturn(new TransactionCollection([$transactionMock]));

Check failure on line 281 in tests/unit/Storefront/Controller/AccountOrderControllerTest.php

View workflow job for this annotation

GitHub Actions / PHP Analyse

Parameter #1 $elements of class Shopware\Core\Checkout\Cart\Transaction\Struct\TransactionCollection constructor expects iterable<Shopware\Core\Checkout\Cart\Transaction\Struct\Transaction>, array<int, PHPUnit\Framework\MockObject\MockObject&Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity> given.

$orders = new OrderCollection([$order]);

$accountRouteResponse = new OrderRouteResponse(
new EntitySearchResult(
OrderDefinition::ENTITY_NAME,
1,
$orders,
null,
new Criteria(),
Context::createDefaultContext()
)
);

$this->orderRouteMock->expects(static::once())
->method('load')
->with($request = new Request(), $salesChannelContext, $criteria)
->willReturn($accountRouteResponse);

$this->controller->updateOrder($orderId, $request, $salesChannelContext);
}
}

/**
Expand Down

0 comments on commit 4e7de26

Please sign in to comment.