From 3ff5c067af467c2ee3e36fcaf0cbc478e1022ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szubert?= Date: Mon, 15 Apr 2019 23:14:43 +0200 Subject: [PATCH] Fix #14958 - move sales sequence removal to external class --- .../Magento/SalesSequence/Model/Builder.php | 44 +---- .../Model/Sequence/DeleteByStore.php | 90 +++++++++++ .../Observer/SequenceRemovalObserver.php | 16 +- .../Unit/Model/Sequence/DeleteByStoreTest.php | 152 ++++++++++++++++++ 4 files changed, 251 insertions(+), 51 deletions(-) create mode 100644 app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php create mode 100644 app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php diff --git a/app/code/Magento/SalesSequence/Model/Builder.php b/app/code/Magento/SalesSequence/Model/Builder.php index b07e687ec25d9..443892b420def 100644 --- a/app/code/Magento/SalesSequence/Model/Builder.php +++ b/app/code/Magento/SalesSequence/Model/Builder.php @@ -5,12 +5,10 @@ */ namespace Magento\SalesSequence\Model; -use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ResourceConnection as AppResource; use Magento\Framework\DB\Ddl\Sequence as DdlSequence; use Magento\Framework\Webapi\Exception; use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata; -use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile; use Psr\Log\LoggerInterface as Logger; /** @@ -85,11 +83,6 @@ class Builder */ protected $logger; - /** - * @var ResourceProfile - */ - private $resourceProfile; - /** * @param ResourceMetadata $resourceMetadata * @param MetaFactory $metaFactory @@ -97,7 +90,6 @@ class Builder * @param AppResource $appResource * @param DdlSequence $ddlSequence * @param Logger $logger - * @param ResourceProfile|null $resourceProfile */ public function __construct( ResourceMetadata $resourceMetadata, @@ -105,8 +97,7 @@ public function __construct( ProfileFactory $profileFactory, AppResource $appResource, DdlSequence $ddlSequence, - Logger $logger, - ResourceProfile $resourceProfile = null + Logger $logger ) { $this->resourceMetadata = $resourceMetadata; $this->metaFactory = $metaFactory; @@ -114,7 +105,6 @@ public function __construct( $this->appResource = $appResource; $this->ddlSequence = $ddlSequence; $this->logger = $logger; - $this->resourceProfile = $resourceProfile ?: ObjectManager::getInstance()->get(ResourceProfile::class); $this->data = array_flip($this->pattern); } @@ -274,36 +264,4 @@ public function create() } $this->data = array_flip($this->pattern); } - - /** - * Deletes all sequence linked entites - * - * @param $storeId - * - * @return void - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function deleteByStoreId($storeId) - { - $metadataIds = $this->resourceMetadata->getIdsByStore($storeId); - $profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds); - - $this->appResource->getConnection()->delete( - $this->appResource->getTableName('sales_sequence_profile'), - ['profile_id IN (?)' => $profileIds] - ); - - foreach ($metadataIds as $metadataId) { - $metadata = $this->metaFactory->create(); - $this->resourceMetadata->load($metadata, $metadataId); - if (!$metadata->getId()) { - continue; - } - - $this->appResource->getConnection()->dropTable( - $metadata->getSequenceTable() - ); - $this->resourceMetadata->delete($metadata); - } - } } diff --git a/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php b/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php new file mode 100644 index 0000000000000..df2160a35396c --- /dev/null +++ b/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php @@ -0,0 +1,90 @@ +resourceMetadata = $resourceMetadata; + $this->resourceProfile = $resourceProfile; + $this->metaFactory = $metaFactory; + $this->appResource = $appResource; + } + + /** + * Deletes all sequence linked entites + * + * @param StoreInterface $store + * @return void + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function execute(StoreInterface $store): void + { + $metadataIds = $this->resourceMetadata->getIdsByStore($store->getId()); + $profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds); + + $this->appResource->getConnection()->delete( + $this->appResource->getTableName('sales_sequence_profile'), + ['profile_id IN (?)' => $profileIds] + ); + + foreach ($metadataIds as $metadataId) { + $metadata = $this->metaFactory->create(); + $this->resourceMetadata->load($metadata, $metadataId); + if (!$metadata->getId()) { + continue; + } + + $this->appResource->getConnection()->dropTable( + $metadata->getSequenceTable() + ); + $this->resourceMetadata->delete($metadata); + } + } +} diff --git a/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php b/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php index 4b25bb4c51cee..1e439c1776080 100644 --- a/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php +++ b/app/code/Magento/SalesSequence/Observer/SequenceRemovalObserver.php @@ -4,11 +4,12 @@ * See COPYING.txt for license details. */ declare(strict_types=1); + namespace Magento\SalesSequence\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer as EventObserver; -use Magento\SalesSequence\Model\Builder; +use Magento\SalesSequence\Model\Sequence\DeleteByStore; /** * Class SequenceRemovalObserver @@ -16,17 +17,17 @@ class SequenceRemovalObserver implements ObserverInterface { /** - * @var Builder + * @var DeleteByStore */ - private $sequenceBuilder; + private $deleteByStore; /** - * @param Builder $sequenceBuilder + * @param DeleteByStore $deleteByStore */ public function __construct( - Builder $sequenceBuilder + DeleteByStore $deleteByStore ) { - $this->sequenceBuilder = $sequenceBuilder; + $this->deleteByStore = $deleteByStore; } /** @@ -36,8 +37,7 @@ public function __construct( */ public function execute(EventObserver $observer) { - $storeId = $observer->getData('store')->getId(); - $this->sequenceBuilder->deleteByStoreId($storeId); + $this->deleteByStore->execute($observer->getData('store')); return $this; } diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php new file mode 100644 index 0000000000000..711bdbb2bdc92 --- /dev/null +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php @@ -0,0 +1,152 @@ +connectionMock = $this->getMockForAbstractClass( + AdapterInterface::class, + [], + '', + false, + false, + true, + ['delete'] + ); + $this->resourceSequenceMeta = $this->createPartialMock( + ResourceMeta::class, + ['getIdsByStore', 'load', 'delete'] + ); + $this->resourceSequenceProfile = $this->createPartialMock( + ResourceProfile::class, + ['getProfileIdsByMetadataIds'] + ); + $this->meta = $this->createPartialMock( + Meta::class, + ['getSequenceTable'] + ); + $this->resourceMock = $this->createMock(ResourceConnection::class); + $this->metaFactory = $this->createPartialMock(MetaFactory::class, ['create']); + $this->metaFactory->expects($this->any())->method('create')->willReturn($this->meta); + $this->store = $this->getMockForAbstractClass( + StoreInterface::class, + [], + '', + false, + false, + true, + ['getId'] + ); + + $helper = new ObjectManager($this); + $this->deleteByStore = $helper->getObject( + DeleteByStore::class, + [ + 'resourceMetadata' => $this->resourceSequenceMeta, + 'resourceProfile' => $this->resourceSequenceProfile, + 'metaFactory' => $this->metaFactory, + 'appResource' => $this->resourceMock, + ] + ); + } + + public function testExecute() + { + $storeId = 1; + $metadataIds = [1, 2]; + $profileIds = [10, 11]; + $tableName = 'sales_sequence_profile'; + $this->store->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + $this->resourceSequenceMeta->expects($this->once()) + ->method('getIdsByStore') + ->with($storeId) + ->willReturn($metadataIds); + $this->resourceSequenceProfile->expects($this->once()) + ->method('getProfileIdsByMetadataIds') + ->with($metadataIds) + ->willReturn($profileIds); + $this->resourceMock->expects($this->once()) + ->method('getTableName') + ->with($tableName) + ->willReturn($tableName); + $this->resourceMock->expects($this->any()) + ->method('getConnection') + ->willReturn($this->connectionMock); + $this->connectionMock->expects($this->once()) + ->method('delete') + ->with($tableName, ['profile_id IN (?)' => $profileIds]) + ->willReturn(2); + $this->resourceSequenceMeta->expects($this->any()) + ->method('load') + ->willReturn($this->meta); + $this->connectionMock->expects($this->any()) + ->method('dropTable') + ->willReturn(true); + $this->resourceSequenceMeta->expects($this->any()) + ->method('delete') + ->willReturn($this->resourceSequenceMeta); + $this->deleteByStore->execute($this->store); + } +}