From 328bc51d3838dcd713fe5e4d6b81cade75528708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szubert?= Date: Wed, 5 Jun 2019 00:15:21 +0200 Subject: [PATCH] Fix #14958 - apply requested changes --- .../Model/ResourceModel/Meta.php | 21 ---- .../Model/ResourceModel/Meta/Ids.php | 48 +++++++++ .../Model/ResourceModel/Profile.php | 17 ---- .../Model/ResourceModel/Profile/Ids.php | 42 ++++++++ .../Model/Sequence/DeleteByStore.php | 28 ++++-- .../Unit/Model/ResourceModel/Meta/IdsTest.php | 99 +++++++++++++++++++ .../Unit/Model/ResourceModel/MetaTest.php | 28 ------ .../Model/ResourceModel/Profile/IdsTest.php | 99 +++++++++++++++++++ .../Unit/Model/ResourceModel/ProfileTest.php | 28 ------ .../Unit/Model/Sequence/DeleteByStoreTest.php | 35 ++++--- 10 files changed, 329 insertions(+), 116 deletions(-) create mode 100644 app/code/Magento/SalesSequence/Model/ResourceModel/Meta/Ids.php create mode 100644 app/code/Magento/SalesSequence/Model/ResourceModel/Profile/Ids.php create mode 100644 app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Meta/IdsTest.php create mode 100644 app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Profile/IdsTest.php diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php index dd70a9d2393bb..8422123c0ca84 100644 --- a/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php @@ -92,27 +92,6 @@ public function loadByEntityTypeAndStore($entityType, $storeId) return $meta; } - /** - * Retrieves Metadata Ids by store id - * - * @param int $storeId - * @return int[] - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function getIdsByStore($storeId) - { - $connection = $this->getConnection(); - $bind = ['store_id' => $storeId]; - $select = $connection->select()->from( - $this->getMainTable(), - [$this->getIdFieldName()] - )->where( - 'store_id = :store_id' - ); - - return $connection->fetchCol($select, $bind); - } - /** * Using for load sequence profile and setting it into metadata * diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Meta/Ids.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta/Ids.php new file mode 100644 index 0000000000000..05df866128f21 --- /dev/null +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Meta/Ids.php @@ -0,0 +1,48 @@ +_init('sales_sequence_meta', 'meta_id'); + } + + /** + * Retrieves Metadata Ids by store id + * + * @param int $storeId + * @return int[] + * @throws LocalizedException + */ + public function getByStoreId($storeId) + { + $connection = $this->getConnection(); + $bind = ['store_id' => $storeId]; + $select = $connection->select()->from( + $this->getMainTable(), + [$this->getIdFieldName()] + )->where( + 'store_id = :store_id' + ); + + return $connection->fetchCol($select, $bind); + } +} diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php index f5e4e8e54eb4b..66f351625486b 100644 --- a/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php @@ -77,21 +77,4 @@ public function loadActiveProfile($metadataId) } return $profile; } - - /** - * Get profile ids by metadata ids - * - * @param int[] $metadataIds - * @return int[] - * @throws \Magento\Framework\Exception\LocalizedException - */ - public function getProfileIdsByMetadataIds(array $metadataIds) - { - $connection = $this->getConnection(); - $select = $connection->select() - ->from($this->getMainTable(), ['profile_id']) - ->where('meta_id IN (?)', $metadataIds); - - return $connection->fetchCol($select); - } } diff --git a/app/code/Magento/SalesSequence/Model/ResourceModel/Profile/Ids.php b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile/Ids.php new file mode 100644 index 0000000000000..219ed6b134668 --- /dev/null +++ b/app/code/Magento/SalesSequence/Model/ResourceModel/Profile/Ids.php @@ -0,0 +1,42 @@ +_init('sales_sequence_profile', 'profile_id'); + } + + /** + * Get profile ids by metadata ids + * + * @param int[] $metadataIds + * @return int[] + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function getByMetadataIds(array $metadataIds) + { + $connection = $this->getConnection(); + $select = $connection->select() + ->from($this->getMainTable(), ['profile_id']) + ->where('meta_id IN (?)', $metadataIds); + + return $connection->fetchCol($select); + } +} diff --git a/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php b/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php index d7c6d4f1559f6..e59bf5f656495 100644 --- a/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php +++ b/app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php @@ -10,24 +10,29 @@ 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\SalesSequence\Model\ResourceModel\Meta\Ids as ResourceMetadataIds; +use Magento\SalesSequence\Model\ResourceModel\Profile\Ids as ResourceProfileIds; use Magento\Store\Api\Data\StoreInterface; /** * Class DeleteByStore - * @api */ class DeleteByStore { /** - * @var resourceMetadata + * @var ResourceMetadata */ private $resourceMetadata; /** - * @var ResourceProfile + * @var ResourceMetadataIds */ - private $resourceProfile; + private $resourceMetadataIds; + + /** + * @var ResourceProfileIds + */ + private $resourceProfileIds; /** * @var MetaFactory @@ -41,18 +46,21 @@ class DeleteByStore /** * @param ResourceMetadata $resourceMetadata - * @param ResourceProfile $resourceProfile + * @param ResourceMetadataIds $resourceMetadataIds + * @param ResourceProfileIds $resourceProfileIds * @param MetaFactory $metaFactory * @param AppResource $appResource */ public function __construct( ResourceMetadata $resourceMetadata, - ResourceProfile $resourceProfile, + ResourceMetadataIds $resourceMetadataIds, + ResourceProfileIds $resourceProfileIds, MetaFactory $metaFactory, AppResource $appResource ) { $this->resourceMetadata = $resourceMetadata; - $this->resourceProfile = $resourceProfile; + $this->resourceMetadataIds = $resourceMetadataIds; + $this->resourceProfileIds = $resourceProfileIds; $this->metaFactory = $metaFactory; $this->appResource = $appResource; } @@ -66,8 +74,8 @@ public function __construct( */ public function execute(StoreInterface $store): void { - $metadataIds = $this->resourceMetadata->getIdsByStore($store->getId()); - $profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds); + $metadataIds = $this->resourceMetadataIds->getByStoreId($store->getId()); + $profileIds = $this->resourceProfileIds->getByMetadataIds($metadataIds); $this->appResource->getConnection()->delete( $this->appResource->getTableName('sales_sequence_profile'), diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Meta/IdsTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Meta/IdsTest.php new file mode 100644 index 0000000000000..dc8b0f90c1078 --- /dev/null +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Meta/IdsTest.php @@ -0,0 +1,99 @@ +connectionMock = $this->getMockForAbstractClass( + AdapterInterface::class, + [], + '', + false, + false, + true, + ['query'] + ); + $this->dbContext = $this->createMock(Context::class); + $this->resourceMock = $this->createPartialMock( + ResourceConnection::class, + ['getConnection', 'getTableName'] + ); + $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock); + $this->select = $this->createMock(Select::class); + $this->resource = new Ids( + $this->dbContext + ); + } + + public function testGetByStoreId() + { + $metaTableName = 'sequence_meta'; + $metaIdFieldName = 'meta_id'; + $storeId = 1; + $metaIds = [1, 2]; + $this->resourceMock->expects($this->any()) + ->method('getConnection') + ->willReturn($this->connectionMock); + $this->resourceMock->expects($this->once()) + ->method('getTableName') + ->willReturn($metaTableName); + $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select); + $this->select->expects($this->at(0)) + ->method('from') + ->with($metaTableName, [$metaIdFieldName]) + ->willReturn($this->select); + $this->select->expects($this->at(1)) + ->method('where') + ->with('store_id = :store_id') + ->willReturn($this->select); + $this->connectionMock->expects($this->once()) + ->method('fetchCol') + ->with($this->select, ['store_id' => $storeId]) + ->willReturn($metaIds); + $this->assertEquals($metaIds, $this->resource->getByStoreId($storeId)); + } +} diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/MetaTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/MetaTest.php index 2b3328fabc4b2..8efa1649a57f0 100644 --- a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/MetaTest.php +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/MetaTest.php @@ -128,34 +128,6 @@ public function testLoadBy() $this->assertEquals($this->meta, $this->resource->loadByEntityTypeAndStore($entityType, $storeId)); } - public function testGetIdsByStore() - { - $metaTableName = 'sequence_meta'; - $metaIdFieldName = 'meta_id'; - $storeId = 1; - $metaIds = [1, 2]; - $this->resourceMock->expects($this->any()) - ->method('getConnection') - ->willReturn($this->connectionMock); - $this->resourceMock->expects($this->once()) - ->method('getTableName') - ->willReturn($metaTableName); - $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select); - $this->select->expects($this->at(0)) - ->method('from') - ->with($metaTableName, [$metaIdFieldName]) - ->willReturn($this->select); - $this->select->expects($this->at(1)) - ->method('where') - ->with('store_id = :store_id') - ->willReturn($this->select); - $this->connectionMock->expects($this->once()) - ->method('fetchCol') - ->with($this->select, ['store_id' => $storeId]) - ->willReturn($metaIds); - $this->assertEquals($metaIds, $this->resource->getIdsByStore($storeId)); - } - /** * @param $metaData */ diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Profile/IdsTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Profile/IdsTest.php new file mode 100644 index 0000000000000..723fd6e8eea06 --- /dev/null +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/Profile/IdsTest.php @@ -0,0 +1,99 @@ +connectionMock = $this->getMockForAbstractClass( + AdapterInterface::class, + [], + '', + false, + false, + true, + ['query'] + ); + $this->dbContext = $this->createMock(Context::class); + $this->resourceMock = $this->createPartialMock( + ResourceConnection::class, + ['getConnection', 'getTableName'] + ); + $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock); + $this->select = $this->createMock(Select::class); + $this->resource = new Ids( + $this->dbContext + ); + } + + public function testGetByMetadataIds() + { + $profileTableName = 'sequence_profile'; + $profileIdFieldName = 'profile_id'; + $metadataIds = [1, 2]; + $profileIds = [10, 11]; + $this->resourceMock->expects($this->any()) + ->method('getConnection') + ->willReturn($this->connectionMock); + $this->resourceMock->expects($this->once()) + ->method('getTableName') + ->willReturn($profileTableName); + $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select); + $this->select->expects($this->at(0)) + ->method('from') + ->with($profileTableName, [$profileIdFieldName]) + ->willReturn($this->select); + $this->select->expects($this->at(1)) + ->method('where') + ->with('meta_id IN (?)', $metadataIds) + ->willReturn($this->select); + $this->connectionMock->expects($this->once()) + ->method('fetchCol') + ->with($this->select) + ->willReturn($profileIds); + $this->assertEquals($profileIds, $this->resource->getByMetadataIds($metadataIds)); + } +} diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/ProfileTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/ProfileTest.php index d1163a9403086..28204f01420c9 100644 --- a/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/ProfileTest.php +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/ResourceModel/ProfileTest.php @@ -129,32 +129,4 @@ public function testLoadActiveProfile() $this->profile->expects($this->at(1))->method('setData')->with($profileData); $this->assertEquals($this->profile, $this->resource->loadActiveProfile($metaId)); } - - public function testGetProfileIdsByMetadataIds() - { - $profileTableName = 'sequence_profile'; - $profileIdFieldName = 'profile_id'; - $metadataIds = [1, 2]; - $profileIds = [10, 11]; - $this->resourceMock->expects($this->any()) - ->method('getConnection') - ->willReturn($this->connectionMock); - $this->resourceMock->expects($this->once()) - ->method('getTableName') - ->willReturn($profileTableName); - $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select); - $this->select->expects($this->at(0)) - ->method('from') - ->with($profileTableName, [$profileIdFieldName]) - ->willReturn($this->select); - $this->select->expects($this->at(1)) - ->method('where') - ->with('meta_id IN (?)', $metadataIds) - ->willReturn($this->select); - $this->connectionMock->expects($this->once()) - ->method('fetchCol') - ->with($this->select) - ->willReturn($profileIds); - $this->assertEquals($profileIds, $this->resource->getProfileIdsByMetadataIds($metadataIds)); - } } diff --git a/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php b/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php index 711bdbb2bdc92..d52e6e797c1b8 100644 --- a/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php +++ b/app/code/Magento/SalesSequence/Test/Unit/Model/Sequence/DeleteByStoreTest.php @@ -11,7 +11,8 @@ 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\ResourceModel\Meta\Ids as ResourceMetaIds; +use Magento\SalesSequence\Model\ResourceModel\Profile\Ids as ResourceProfileIds; use Magento\SalesSequence\Model\Sequence\DeleteByStore; use Magento\Store\Api\Data\StoreInterface; use PHPUnit\Framework\MockObject\MockObject; @@ -33,9 +34,14 @@ class DeleteByStoreTest extends TestCase private $resourceSequenceMeta; /** - * @var ResourceProfile | MockObject + * @var ResourceMetaIds | MockObject */ - private $resourceSequenceProfile; + private $resourceSequenceMetaIds; + + /** + * @var ResourceProfileIds | MockObject + */ + private $resourceSequenceProfileIds; /** * @var Meta | MockObject @@ -75,11 +81,15 @@ protected function setUp() ); $this->resourceSequenceMeta = $this->createPartialMock( ResourceMeta::class, - ['getIdsByStore', 'load', 'delete'] + ['load', 'delete'] + ); + $this->resourceSequenceMetaIds = $this->createPartialMock( + ResourceMetaIds::class, + ['getByStoreId'] ); - $this->resourceSequenceProfile = $this->createPartialMock( - ResourceProfile::class, - ['getProfileIdsByMetadataIds'] + $this->resourceSequenceProfileIds = $this->createPartialMock( + ResourceProfileIds::class, + ['getByMetadataIds'] ); $this->meta = $this->createPartialMock( Meta::class, @@ -102,8 +112,9 @@ protected function setUp() $this->deleteByStore = $helper->getObject( DeleteByStore::class, [ + 'resourceMetadataIds' => $this->resourceSequenceMetaIds, 'resourceMetadata' => $this->resourceSequenceMeta, - 'resourceProfile' => $this->resourceSequenceProfile, + 'resourceProfileIds' => $this->resourceSequenceProfileIds, 'metaFactory' => $this->metaFactory, 'appResource' => $this->resourceMock, ] @@ -119,12 +130,12 @@ public function testExecute() $this->store->expects($this->once()) ->method('getId') ->willReturn($storeId); - $this->resourceSequenceMeta->expects($this->once()) - ->method('getIdsByStore') + $this->resourceSequenceMetaIds->expects($this->once()) + ->method('getByStoreId') ->with($storeId) ->willReturn($metadataIds); - $this->resourceSequenceProfile->expects($this->once()) - ->method('getProfileIdsByMetadataIds') + $this->resourceSequenceProfileIds->expects($this->once()) + ->method('getByMetadataIds') ->with($metadataIds) ->willReturn($profileIds); $this->resourceMock->expects($this->once())