From 1af39c6935caa12c9ca66913b6f17ceab63e7a71 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 31 Oct 2024 08:49:47 +0100 Subject: [PATCH 1/3] [Behat] Dehardcode the use of AvatarImage entity --- .../Behat/Context/Setup/AdminUserContext.php | 22 ++++++++++++------- .../config/services/contexts/setup.xml | 1 + 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Sylius/Behat/Context/Setup/AdminUserContext.php b/src/Sylius/Behat/Context/Setup/AdminUserContext.php index 310d658711..0926eaa6ee 100644 --- a/src/Sylius/Behat/Context/Setup/AdminUserContext.php +++ b/src/Sylius/Behat/Context/Setup/AdminUserContext.php @@ -18,20 +18,26 @@ use Doctrine\Persistence\ObjectManager; use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; use Sylius\Component\Core\Model\AdminUserInterface; -use Sylius\Component\Core\Model\AvatarImage; +use Sylius\Component\Core\Model\AvatarImageInterface; use Sylius\Component\Core\Uploader\ImageUploaderInterface; use Sylius\Component\User\Repository\UserRepositoryInterface; +use Sylius\Resource\Factory\FactoryInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; final class AdminUserContext implements Context { + /** + * @param UserRepositoryInterface $userRepository + * @param FactoryInterface $avatarImageFactory + */ public function __construct( - private SharedStorageInterface $sharedStorage, - private ExampleFactoryInterface $userFactory, - private UserRepositoryInterface $userRepository, - private ImageUploaderInterface $imageUploader, - private ObjectManager $objectManager, - private \ArrayAccess $minkParameters, + private readonly SharedStorageInterface $sharedStorage, + private readonly ExampleFactoryInterface $userFactory, + private readonly UserRepositoryInterface $userRepository, + private readonly ImageUploaderInterface $imageUploader, + private readonly ObjectManager $objectManager, + private readonly \ArrayAccess $minkParameters, + private readonly FactoryInterface $avatarImageFactory, ) { } @@ -99,7 +105,7 @@ final class AdminUserContext implements Context { $filesPath = $this->minkParameters['files_path']; - $avatar = new AvatarImage(); + $avatar = $this->avatarImageFactory->createNew(); $avatar->setFile(new UploadedFile($filesPath . $avatarPath, basename($avatarPath))); $this->imageUploader->upload($avatar); diff --git a/src/Sylius/Behat/Resources/config/services/contexts/setup.xml b/src/Sylius/Behat/Resources/config/services/contexts/setup.xml index 81bc4980f4..b68cf825aa 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/setup.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/setup.xml @@ -28,6 +28,7 @@ + From 5617c2d36af8c48cda9d95f785abe5bac7be14cd Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 31 Oct 2024 08:55:48 +0100 Subject: [PATCH 2/3] [Behat] Dehardcode the use of ShopBillingData entity --- .../Behat/Context/Setup/ChannelContext.php | 19 ++++++++++-------- .../config/services/contexts/setup.xml | 1 + .../Resources/config/test_services.xml | 2 ++ .../Test/Services/DefaultChannelFactory.php | 20 ++++++++++--------- .../Services/DefaultChannelFactorySpec.php | 10 ++++++++-- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/Sylius/Behat/Context/Setup/ChannelContext.php b/src/Sylius/Behat/Context/Setup/ChannelContext.php index ea66be1fd2..fe869ae3b6 100644 --- a/src/Sylius/Behat/Context/Setup/ChannelContext.php +++ b/src/Sylius/Behat/Context/Setup/ChannelContext.php @@ -23,23 +23,26 @@ use Sylius\Component\Addressing\Model\ZoneInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Formatter\StringInflector; use Sylius\Component\Core\Model\ChannelInterface; -use Sylius\Component\Core\Model\ShopBillingData; +use Sylius\Component\Core\Model\ShopBillingDataInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Test\Services\DefaultChannelFactoryInterface; use Sylius\Component\Locale\Model\LocaleInterface; +use Sylius\Resource\Factory\FactoryInterface; final class ChannelContext implements Context { /** * @param ChannelRepositoryInterface $channelRepository + * @param FactoryInterface $shopBillingDataFactory */ public function __construct( - private SharedStorageInterface $sharedStorage, - private ChannelContextSetterInterface $channelContextSetter, - private DefaultChannelFactoryInterface $unitedStatesChannelFactory, - private DefaultChannelFactoryInterface $defaultChannelFactory, - private ChannelRepositoryInterface $channelRepository, - private ObjectManager $channelManager, + private readonly SharedStorageInterface $sharedStorage, + private readonly ChannelContextSetterInterface $channelContextSetter, + private readonly DefaultChannelFactoryInterface $unitedStatesChannelFactory, + private readonly DefaultChannelFactoryInterface $defaultChannelFactory, + private readonly ChannelRepositoryInterface $channelRepository, + private readonly ObjectManager $channelManager, + private readonly FactoryInterface $shopBillingDataFactory, ) { } @@ -243,7 +246,7 @@ final class ChannelContext implements Context CountryInterface $country, string $taxId, ): void { - $shopBillingData = new ShopBillingData(); + $shopBillingData = $this->shopBillingDataFactory->createNew(); $shopBillingData->setCompany($company); $shopBillingData->setStreet($street); $shopBillingData->setPostcode($postcode); diff --git a/src/Sylius/Behat/Resources/config/services/contexts/setup.xml b/src/Sylius/Behat/Resources/config/services/contexts/setup.xml index b68cf825aa..f6f83df241 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/setup.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/setup.xml @@ -55,6 +55,7 @@ + diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml index 8268f9f01a..c3ae861b2a 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml @@ -36,6 +36,7 @@ %locale% + @@ -44,6 +45,7 @@ %locale% + diff --git a/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php b/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php index 782c38fc75..b2565eac32 100644 --- a/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php +++ b/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php @@ -15,7 +15,7 @@ namespace Sylius\Component\Core\Test\Services; use Sylius\Component\Channel\Factory\ChannelFactoryInterface; use Sylius\Component\Core\Model\ChannelInterface; -use Sylius\Component\Core\Model\ShopBillingData; +use Sylius\Component\Core\Model\ShopBillingDataInterface; use Sylius\Component\Currency\Model\CurrencyInterface; use Sylius\Component\Locale\Model\LocaleInterface; use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; @@ -35,15 +35,17 @@ final class DefaultChannelFactory implements DefaultChannelFactoryInterface * @param RepositoryInterface $channelRepository * @param RepositoryInterface $currencyRepository * @param RepositoryInterface $localeRepository + * @param FactoryInterface $shopBillingDataFactory */ public function __construct( - private ChannelFactoryInterface $channelFactory, - private FactoryInterface $currencyFactory, - private FactoryInterface $localeFactory, - private RepositoryInterface $channelRepository, - private RepositoryInterface $currencyRepository, - private RepositoryInterface $localeRepository, - private string $defaultLocaleCode, + private readonly ChannelFactoryInterface $channelFactory, + private readonly FactoryInterface $currencyFactory, + private readonly FactoryInterface $localeFactory, + private readonly RepositoryInterface $channelRepository, + private readonly RepositoryInterface $currencyRepository, + private readonly RepositoryInterface $localeRepository, + private readonly string $defaultLocaleCode, + private readonly FactoryInterface $shopBillingDataFactory, ) { } @@ -63,7 +65,7 @@ final class DefaultChannelFactory implements DefaultChannelFactoryInterface $channel->addLocale($locale); $channel->setDefaultLocale($locale); if ($channel->getShopBillingData() === null) { - $channel->setShopBillingData(new ShopBillingData()); + $channel->setShopBillingData($this->shopBillingDataFactory->createNew()); } $this->channelRepository->add($channel); diff --git a/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php b/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php index 6219c0d50c..6649e6f41c 100644 --- a/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php +++ b/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php @@ -16,7 +16,7 @@ namespace spec\Sylius\Component\Core\Test\Services; use PhpSpec\ObjectBehavior; use Sylius\Component\Channel\Factory\ChannelFactoryInterface; use Sylius\Component\Core\Model\ChannelInterface; -use Sylius\Component\Core\Model\ShopBillingData; +use Sylius\Component\Core\Model\ShopBillingDataInterface; use Sylius\Component\Core\Test\Services\DefaultChannelFactoryInterface; use Sylius\Component\Currency\Model\CurrencyInterface; use Sylius\Component\Locale\Model\LocaleInterface; @@ -32,6 +32,7 @@ final class DefaultChannelFactorySpec extends ObjectBehavior RepositoryInterface $channelRepository, RepositoryInterface $currencyRepository, RepositoryInterface $localeRepository, + FactoryInterface $shopBillingDataFactory, ): void { $this->beConstructedWith( $channelFactory, @@ -41,6 +42,7 @@ final class DefaultChannelFactorySpec extends ObjectBehavior $currencyRepository, $localeRepository, 'en_US', + $shopBillingDataFactory, ); } @@ -56,9 +58,11 @@ final class DefaultChannelFactorySpec extends ObjectBehavior RepositoryInterface $channelRepository, RepositoryInterface $currencyRepository, RepositoryInterface $localeRepository, + FactoryInterface $shopBillingDataFactory, ChannelInterface $channel, CurrencyInterface $currency, LocaleInterface $locale, + ShopBillingDataInterface $shopBillingData, ): void { $localeFactory->createNew()->willReturn($locale); $locale->setCode('en_US')->shouldBeCalled(); @@ -68,6 +72,8 @@ final class DefaultChannelFactorySpec extends ObjectBehavior $channelFactory->createNamed('Default')->willReturn($channel); + $shopBillingDataFactory->createNew()->willReturn($shopBillingData); + $channel->setCode('DEFAULT')->shouldBeCalled(); $channel->setTaxCalculationStrategy('order_items_based')->shouldBeCalled(); @@ -76,7 +82,7 @@ final class DefaultChannelFactorySpec extends ObjectBehavior $channel->addLocale($locale)->shouldBeCalled(); $channel->setDefaultLocale($locale)->shouldBeCalled(); $channel->getShopBillingData()->willReturn(null); - $channel->setShopBillingData(new ShopBillingData())->shouldBeCalled(); + $channel->setShopBillingData($shopBillingData)->shouldBeCalled(); $currencyRepository->findOneBy(['code' => 'USD'])->willReturn(null); $localeRepository->findOneBy(['code' => 'en_US'])->willReturn(null); From 47bbefe7957477978fef660806cbbe58ec2c832a Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 31 Oct 2024 12:00:04 +0100 Subject: [PATCH 3/3] Improve DefaultChannelFactory test service + add note about change --- UPGRADE-1.14.md | 24 +++++++++++++++++++ .../Resources/config/test_services.xml | 2 +- .../Test/Services/DefaultChannelFactory.php | 4 ++-- .../Services/DefaultChannelFactorySpec.php | 4 ++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/UPGRADE-1.14.md b/UPGRADE-1.14.md index 1e27353ff4..19a5950129 100644 --- a/UPGRADE-1.14.md +++ b/UPGRADE-1.14.md @@ -1035,3 +1035,27 @@ * `sylius.{user_type}_user.pin_generator.password_reset` * `sylius.{user_type}_user.pin_uniqueness_checker.password_reset` + +### Testing Suite + +1. The constructor signature of `Sylius\Component\Core\Test\Services\DefaultChannelFactory` has been changed: + + ```diff + public function __construct( + - private ChannelFactoryInterface $channelFactory, + - private FactoryInterface $currencyFactory, + - private FactoryInterface $localeFactory, + - private RepositoryInterface $channelRepository, + - private RepositoryInterface $currencyRepository, + - private RepositoryInterface $localeRepository, + - private string $defaultLocaleCode, + + private readonly ChannelFactoryInterface $channelFactory, + + private readonly FactoryInterface $currencyFactory, + + private readonly FactoryInterface $localeFactory, + + private readonly FactoryInterface $shopBillingDataFactory, + + private readonly RepositoryInterface $channelRepository, + + private readonly RepositoryInterface $currencyRepository, + + private readonly RepositoryInterface $localeRepository, + + private readonly string $defaultLocaleCode, + ) + ``` diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml index c3ae861b2a..da7a29dd2e 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/test_services.xml @@ -41,11 +41,11 @@ + %locale% - diff --git a/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php b/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php index b2565eac32..cc8954f2ac 100644 --- a/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php +++ b/src/Sylius/Component/Core/Test/Services/DefaultChannelFactory.php @@ -32,20 +32,20 @@ final class DefaultChannelFactory implements DefaultChannelFactoryInterface /** * @param FactoryInterface $currencyFactory * @param FactoryInterface $localeFactory + * @param FactoryInterface $shopBillingDataFactory * @param RepositoryInterface $channelRepository * @param RepositoryInterface $currencyRepository * @param RepositoryInterface $localeRepository - * @param FactoryInterface $shopBillingDataFactory */ public function __construct( private readonly ChannelFactoryInterface $channelFactory, private readonly FactoryInterface $currencyFactory, private readonly FactoryInterface $localeFactory, + private readonly FactoryInterface $shopBillingDataFactory, private readonly RepositoryInterface $channelRepository, private readonly RepositoryInterface $currencyRepository, private readonly RepositoryInterface $localeRepository, private readonly string $defaultLocaleCode, - private readonly FactoryInterface $shopBillingDataFactory, ) { } diff --git a/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php b/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php index 6649e6f41c..b325df44ac 100644 --- a/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php +++ b/src/Sylius/Component/Core/spec/Test/Services/DefaultChannelFactorySpec.php @@ -38,11 +38,11 @@ final class DefaultChannelFactorySpec extends ObjectBehavior $channelFactory, $currencyFactory, $localeFactory, + $shopBillingDataFactory, $channelRepository, $currencyRepository, $localeRepository, 'en_US', - $shopBillingDataFactory, ); } @@ -55,10 +55,10 @@ final class DefaultChannelFactorySpec extends ObjectBehavior ChannelFactoryInterface $channelFactory, FactoryInterface $currencyFactory, FactoryInterface $localeFactory, + FactoryInterface $shopBillingDataFactory, RepositoryInterface $channelRepository, RepositoryInterface $currencyRepository, RepositoryInterface $localeRepository, - FactoryInterface $shopBillingDataFactory, ChannelInterface $channel, CurrencyInterface $currency, LocaleInterface $locale,