-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #14958 - move sales sequence removal to external class
- Loading branch information
1 parent
78bfbc8
commit 3ff5c06
Showing
4 changed files
with
251 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SalesSequence\Model\Sequence; | ||
|
||
use Magento\Framework\App\ResourceConnection as AppResource; | ||
use Magento\SalesSequence\Model\MetaFactory; | ||
use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata; | ||
use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
|
||
/** | ||
* Class DeleteByStore | ||
* @api | ||
*/ | ||
class DeleteByStore | ||
{ | ||
/** | ||
* @var resourceMetadata | ||
*/ | ||
protected $resourceMetadata; | ||
|
||
/** | ||
* @var ResourceProfile | ||
*/ | ||
private $resourceProfile; | ||
|
||
/** | ||
* @var MetaFactory | ||
*/ | ||
protected $metaFactory; | ||
|
||
/** | ||
* @var AppResource | ||
*/ | ||
protected $appResource; | ||
|
||
/** | ||
* @param ResourceMetadata $resourceMetadata | ||
* @param ResourceProfile $resourceProfile | ||
* @param MetaFactory $metaFactory | ||
* @param AppResource $appResource | ||
*/ | ||
public function __construct( | ||
ResourceMetadata $resourceMetadata, | ||
ResourceProfile $resourceProfile, | ||
MetaFactory $metaFactory, | ||
AppResource $appResource | ||
) { | ||
$this->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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SalesSequence\Test\Unit\Model\Sequence; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\SalesSequence\Model\Meta; | ||
use Magento\SalesSequence\Model\MetaFactory; | ||
use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMeta; | ||
use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile; | ||
use Magento\SalesSequence\Model\Sequence\DeleteByStore; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class DeleteByStoreTest | ||
*/ | ||
class DeleteByStoreTest extends TestCase | ||
{ | ||
/** | ||
* @var DeleteByStore | ||
*/ | ||
private $deleteByStore; | ||
|
||
/** | ||
* @var ResourceMeta | MockObject | ||
*/ | ||
private $resourceSequenceMeta; | ||
|
||
/** | ||
* @var ResourceProfile | MockObject | ||
*/ | ||
private $resourceSequenceProfile; | ||
|
||
/** | ||
* @var Meta | MockObject | ||
*/ | ||
private $meta; | ||
|
||
/** | ||
* @var MetaFactory | MockObject | ||
*/ | ||
private $metaFactory; | ||
|
||
/** | ||
* @var AdapterInterface | MockObject | ||
*/ | ||
private $connectionMock; | ||
|
||
/** | ||
* @var ResourceConnection | MockObject | ||
*/ | ||
private $resourceMock; | ||
|
||
/** | ||
* @var StoreInterface | MockObject | ||
*/ | ||
private $store; | ||
|
||
protected function setUp() | ||
{ | ||
$this->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); | ||
} | ||
} |