Skip to content

Commit

Permalink
Fix magento#14958 - apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartlomiejsz committed Jun 4, 2019
1 parent c83a283 commit 328bc51
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 116 deletions.
21 changes: 0 additions & 21 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
48 changes: 48 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Meta/Ids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SalesSequence\Model\ResourceModel\Meta;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
* Class Ids is used to retrieve metadata ids for sequence
*/
class Ids extends AbstractDb
{
/**
* Model initialization
*
* @return void
*/
protected function _construct()
{
$this->_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);
}
}
17 changes: 0 additions & 17 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
42 changes: 42 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Profile/Ids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SalesSequence\Model\ResourceModel\Profile;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
* Class Ids is used to retrieve profile ids for sequence profile
*/
class Ids extends AbstractDb
{
/**
* Model initialization
*
* @return void
*/
protected function _construct()
{
$this->_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);
}
}
28 changes: 18 additions & 10 deletions app/code/Magento/SalesSequence/Model/Sequence/DeleteByStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesSequence\Test\Unit\Model\ResourceModel\Meta;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\SalesSequence\Model\ResourceModel\Meta\Ids;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class IdsTest
*/
class IdsTest extends TestCase
{
/**
* @var AdapterInterface | MockObject
*/
private $connectionMock;

/**
* @var Context | MockObject
*/
private $dbContext;

/**
* @var Ids
*/
private $resource;

/**
* @var Resource | MockObject
*/
protected $resourceMock;

/**
* @var Select | MockObject
*/
private $select;

/**
* Initialization
*/
protected function setUp()
{
$this->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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading

0 comments on commit 328bc51

Please sign in to comment.