From 1e4af8a4a19b0b1fc21d65f1bcc71be1b2f76535 Mon Sep 17 00:00:00 2001 From: Benjamin Wittwer Date: Mon, 22 Apr 2024 09:09:34 +0200 Subject: [PATCH] Fix migration saleschannel test --- ...4-04-22-fix-migration-saleschannel-test.md | 9 ++++++ ...faultDomainForHeadlessSaleschannelTest.php | 32 ++++++++++++------- 2 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 changelog/_unreleased/2024-04-22-fix-migration-saleschannel-test.md diff --git a/changelog/_unreleased/2024-04-22-fix-migration-saleschannel-test.md b/changelog/_unreleased/2024-04-22-fix-migration-saleschannel-test.md new file mode 100644 index 00000000000..909f356b0d1 --- /dev/null +++ b/changelog/_unreleased/2024-04-22-fix-migration-saleschannel-test.md @@ -0,0 +1,9 @@ +--- +title: Fix migration saleschannel test +issue: NEXT-00000 +author: Benjamin Wittwer +author_email: dev@a-k-f.de +author_github: akf-bw +--- +# Core +* Changed `Migration1620820321AddDefaultDomainForHeadlessSaleschannelTest` to catch `ForeignKeyConstraintViolationException` diff --git a/src/Core/Migration/Test/Migration1620820321AddDefaultDomainForHeadlessSaleschannelTest.php b/src/Core/Migration/Test/Migration1620820321AddDefaultDomainForHeadlessSaleschannelTest.php index ac20d5379e7..36426283258 100644 --- a/src/Core/Migration/Test/Migration1620820321AddDefaultDomainForHeadlessSaleschannelTest.php +++ b/src/Core/Migration/Test/Migration1620820321AddDefaultDomainForHeadlessSaleschannelTest.php @@ -3,12 +3,12 @@ namespace Shopware\Core\Migration\Test; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; use PHPUnit\Framework\Attributes\RunClassInSeparateProcess; use PHPUnit\Framework\TestCase; use Shopware\Core\Defaults; use Shopware\Core\Framework\Api\Util\AccessKeyHelper; use Shopware\Core\Framework\Context; -use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\Framework\Log\Package; use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour; use Shopware\Core\Framework\Uuid\Uuid; @@ -77,8 +77,15 @@ public function testItAddsDefaultDomainToMultipleApiSalesChannel(): void public function testItDoesNotBreakIfNoHeadlessSalesChannelIsPresent(): void { - $salesChannelRepository = $this->getContainer()->get('sales_channel.repository'); - $salesChannelRepository->delete([['id' => TestDefaults::SALES_CHANNEL]], Context::createDefaultContext()); + try { + $salesChannelRepository = $this->getContainer()->get('sales_channel.repository'); + $salesChannelRepository->delete([['id' => TestDefaults::SALES_CHANNEL]], Context::createDefaultContext()); + } catch (\Exception $e) { + if (!($e instanceof ForeignKeyConstraintViolationException)) { + static::fail(sprintf('%s Trace: %s', $e->getMessage(), $e->getTraceAsString())); + } + // ignore error because it is possible that other tests (e.g. order tests that refer to the saleschannel) block the deletion of the sales channel + } (new Migration1620820321AddDefaultDomainForHeadlessSaleschannel())->update($this->connection); } @@ -93,14 +100,17 @@ private function removeAddedDefaultDomains(): void private function removeAddedSalesChannel(): void { - $ids = $this->connection->fetchAllAssociative(' - SELECT id FROM `sales_channel` - WHERE `short_name` = "API Test" - '); - - /** @var EntityRepository $salesChannelRepository */ - $salesChannelRepository = $this->getContainer()->get('sales_channel.repository'); - // $salesChannelRepository->delete([$ids], Context::createDefaultContext()); + try { + $this->connection->executeStatement(' + DELETE FROM `sales_channel` + WHERE `short_name` = "API Test" + '); + } catch (\Exception $e) { + if (!($e instanceof ForeignKeyConstraintViolationException)) { + static::fail(sprintf('%s Trace: %s', $e->getMessage(), $e->getTraceAsString())); + } + // ignore error because it is possible that other tests (e.g. order tests that refer to the saleschannel) block the deletion of the sales channel + } } private function addSalesChannel(string $salesChannelType): string