Skip to content
18 changes: 13 additions & 5 deletions app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(

/**
* Return whole scopes config data from db.
*
* Ignore $path argument due to config source must return all config data
*
* @param string $path
Expand All @@ -64,6 +65,8 @@ public function get($path = '')
}

/**
* Retrieve default connection
*
* @return AdapterInterface
*/
private function getConnection()
Expand All @@ -83,12 +86,17 @@ private function getConnection()
*/
private function getEntities($table, $keyField)
{
$entities = $this->getConnection()->fetchAll(
$this->getConnection()->select()->from($this->resourceConnection->getTableName($table))
);
$data = [];
foreach ($entities as $entity) {
$data[$entity[$keyField]] = $entity;
$tableName = $this->resourceConnection->getTableName($table);
// Check if db table exists before fetch data
if ($this->resourceConnection->getConnection()->isTableExists($tableName)) {
$entities = $this->getConnection()->fetchAll(
$this->getConnection()->select()->from($tableName)
);

foreach ($entities as $entity) {
$data[$entity[$keyField]] = $entity;
}
}

return $data;
Expand Down
13 changes: 9 additions & 4 deletions app/code/Magento/Store/Model/ResourceModel/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model)
*/
public function readAllStores()
{
$select = $this->getConnection()
->select()
->from($this->getTable('store'));
$stores = [];
if ($this->getConnection()->isTableExists($this->getMainTable())) {
$select = $this->getConnection()
->select()
->from($this->getTable($this->getMainTable()));

return $this->getConnection()->fetchAll($select);
$stores = $this->getConnection()->fetchAll($select);
}

return $stores;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions app/code/Magento/Store/Model/ResourceModel/Website.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ protected function _initUniqueFields()
public function readAllWebsites()
{
$websites = [];
$select = $this->getConnection()
->select()
->from($this->getTable('store_website'));
$tableName = $this->getMainTable();
if ($this->getConnection()->isTableExists($tableName)) {
$select = $this->getConnection()
->select()
->from($tableName);

foreach ($this->getConnection()->fetchAll($select) as $websiteData) {
$websites[$websiteData['code']] = $websiteData;
foreach ($this->getConnection()->fetchAll($select) as $websiteData) {
$websites[$websiteData['code']] = $websiteData;
}
}

return $websites;
Expand Down Expand Up @@ -115,6 +118,7 @@ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)

/**
* Retrieve default stores select object
*
* Select fields website_id, store_id
*
* @param bool $includeDefault include/exclude default admin website
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function setUp()

public function testGet()
{
$this->deploymentConfig->expects($this->once())
$this->deploymentConfig->expects($this->any())
->method('get')
->with('db')
->willReturn(true);
$this->resourceConnection->expects($this->once())->method('getConnection')->willReturn($this->connection);
$this->resourceConnection->expects($this->any())->method('getConnection')->willReturn($this->connection);

$selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
$selectMock->expects($this->any())->method('from')->willReturnSelf();
Expand Down
190 changes: 190 additions & 0 deletions app/code/Magento/Store/Test/Unit/Model/ResourceModel/StoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Store\Test\Unit\Model\ResourceModel;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Framework\DB\Adapter\AdapterInterface;

class StoreTest extends \PHPUnit\Framework\TestCase
{
/** @var Store */
protected $model;

/**
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
*/
protected $resourceMock;

/** @var Select | \PHPUnit_Framework_MockObject_MockObject */
protected $select;

/**
* @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $connectionMock;

public function setUp()
{
$objectManagerHelper = new ObjectManager($this);
$this->select = $this->createMock(Select::class);
$this->resourceMock = $this->createPartialMock(
ResourceConnection::class,
[
'getConnection',
'getTableName'
]
);
$this->connectionMock = $this->createPartialMock(
\Magento\Framework\DB\Adapter\Pdo\Mysql::class,
[
'isTableExists',
'select',
'fetchAll',
'fetchOne',
'from',
'getCheckSql',
'where',
'quoteIdentifier',
'quote'
]
);

$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
$contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
$configCacheTypeMock = $this->createMock('\Magento\Framework\App\Cache\Type\Config');
$this->model = $objectManagerHelper->getObject(
Store::class,
[
'context' => $contextMock,
'configCacheType' => $configCacheTypeMock
]
);
}

public function testCountAll($countAdmin = false)
{
$mainTable = 'store';
$tableIdentifier = 'code';
$tableIdentifierValue = 'admin';
$count = 1;

$this->resourceMock->expects($this->once())
->method('getConnection')
->willReturn($this->connectionMock);

$this->connectionMock->expects($this->once())
->method('select')
->willReturn($this->select);

$this->resourceMock->expects($this->once())
->method('getTableName')
->willReturn($mainTable);

$this->select->expects($this->once())
->method('from')
->with($mainTable, 'COUNT(*)')
->willReturnSelf();

$this->connectionMock->expects($this->any())
->method('quoteIdentifier')
->with($tableIdentifier)
->willReturn($tableIdentifier);

$this->connectionMock->expects($this->once())
->method('quote')
->with($tableIdentifierValue)
->willReturn($tableIdentifierValue);

$this->select->expects($this->any())
->method('where')
->with(sprintf('%s <> %s', $tableIdentifier, $tableIdentifierValue))
->willReturnSelf();

$this->connectionMock->expects($this->once())
->method('fetchOne')
->with($this->select)
->willReturn($count);

$this->assertEquals($count, $this->model->countAll($countAdmin));
}

public function testReadAllStores()
{
$mainTable = 'store';
$data = [
["store_id" => "0", "code" => "admin", "website_id" => 0, "name" => "Admin"],
["store_id" => "1", "code" => "default", "website_id" => 1, "name" => "Default Store View"]
];

$this->resourceMock->expects($this->atLeastOnce())
->method('getConnection')
->willReturn($this->connectionMock);

$this->resourceMock->expects($this->atLeastOnce())
->method('getTableName')
->willReturn($mainTable);

$this->connectionMock->expects($this->once())
->method('isTableExists')
->with($mainTable)
->willReturn(true);

$this->connectionMock->expects($this->once())
->method('select')
->willReturn($this->select);

$this->select->expects($this->once())
->method('from')
->with($mainTable)
->willReturnSelf();

$this->connectionMock->expects($this->once())
->method('fetchAll')
->with($this->select)
->willReturn($data);

$this->assertEquals($data, $this->model->readAllStores());
}

public function testReadAllStoresNoDbTable()
{
$mainTable = 'no_store_table';
$data = [];

$this->resourceMock->expects($this->once())
->method('getConnection')
->willReturn($this->connectionMock);

$this->resourceMock->expects($this->once())
->method('getTableName')
->willReturn($mainTable);

$this->connectionMock->expects($this->once())
->method('isTableExists')
->with($mainTable)
->willReturn(false);

$this->connectionMock->expects($this->never())
->method('select')
->willReturn($this->select);

$this->select->expects($this->never())
->method('from')
->with($mainTable)
->willReturnSelf();

$this->connectionMock->expects($this->never())
->method('fetchAll')
->with($this->select)
->willReturn($data);

$this->assertEquals($data, $this->model->readAllStores());
}
}
Loading