init
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Framework\Model\ResourceModel\Db;
|
||||
|
||||
class AbstractTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\App\ResourceConnection::class
|
||||
);
|
||||
$context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Model\ResourceModel\Db\Context::class,
|
||||
['resource' => $resource]
|
||||
);
|
||||
$this->_model = $this->getMockForAbstractClass(
|
||||
\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
|
||||
['context' => $context]
|
||||
);
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$resourceProperty = new \ReflectionProperty(get_class($this->_model), '_resources');
|
||||
$resourceProperty->setAccessible(true);
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Framework\App\ResourceConnection::class,
|
||||
$resourceProperty->getValue($this->_model)
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetMainTable()
|
||||
{
|
||||
$setMainTableMethod = new \ReflectionMethod($this->_model, '_setMainTable');
|
||||
$setMainTableMethod->setAccessible(true);
|
||||
|
||||
$tableName = $this->_model->getTable('store_website');
|
||||
$idFieldName = 'website_id';
|
||||
|
||||
$setMainTableMethod->invoke($this->_model, $tableName);
|
||||
$this->assertEquals($tableName, $this->_model->getMainTable());
|
||||
|
||||
$setMainTableMethod->invoke($this->_model, $tableName, $idFieldName);
|
||||
$this->assertEquals($tableName, $this->_model->getMainTable());
|
||||
$this->assertEquals($idFieldName, $this->_model->getIdFieldName());
|
||||
}
|
||||
|
||||
public function testGetTableName()
|
||||
{
|
||||
$tableNameOrig = 'store_website';
|
||||
$tableSuffix = 'suffix';
|
||||
$resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\App\ResourceConnection::class,
|
||||
['tablePrefix' => 'prefix_']
|
||||
);
|
||||
$context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Model\ResourceModel\Db\Context::class,
|
||||
['resource' => $resource]
|
||||
);
|
||||
|
||||
$model = $this->getMockForAbstractClass(
|
||||
\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
|
||||
['context' => $context]
|
||||
);
|
||||
|
||||
$tableName = $model->getTable([$tableNameOrig, $tableSuffix]);
|
||||
$this->assertEquals('prefix_store_website_suffix', $tableName);
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Model\ResourceModel\Db\Collection;
|
||||
|
||||
class AbstractTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
|
||||
*/
|
||||
protected $_model = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$resourceModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\ResourceConnection::class);
|
||||
$context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Model\ResourceModel\Db\Context::class,
|
||||
['resource' => $resourceModel]
|
||||
);
|
||||
|
||||
$resource = $this->getMockForAbstractClass(
|
||||
\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
|
||||
[$context],
|
||||
'',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
['getMainTable', 'getIdFieldName']
|
||||
);
|
||||
|
||||
$resource->expects(
|
||||
$this->any()
|
||||
)->method(
|
||||
'getMainTable'
|
||||
)->willReturn(
|
||||
$resource->getTable('store_website')
|
||||
);
|
||||
$resource->expects($this->any())->method('getIdFieldName')->willReturn('website_id');
|
||||
|
||||
$fetchStrategy = $this->getMockForAbstractClass(
|
||||
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface::class
|
||||
);
|
||||
|
||||
$eventManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\Event\ManagerInterface::class
|
||||
);
|
||||
|
||||
$entityFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\Data\Collection\EntityFactory::class
|
||||
);
|
||||
$logger = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Psr\Log\LoggerInterface::class);
|
||||
|
||||
$this->_model = $this->getMockForAbstractClass(
|
||||
\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class,
|
||||
[$entityFactory, $logger, $fetchStrategy, $eventManager, null, $resource]
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAllIds()
|
||||
{
|
||||
$allIds = $this->_model->getAllIds();
|
||||
sort($allIds);
|
||||
$this->assertEquals(['0', '1'], $allIds);
|
||||
}
|
||||
|
||||
public function testGetAllIdsWithBind()
|
||||
{
|
||||
$this->_model->getSelect()->where('code = :code');
|
||||
$this->_model->addBindParam('code', 'admin');
|
||||
$this->assertEquals(['0'], $this->_model->getAllIds());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check add field to select doesn't remove expression field from select.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddExpressionFieldToSelectWithAdditionalFields()
|
||||
{
|
||||
$expectedColumns = ['code', 'test_field'];
|
||||
$actualColumns = [];
|
||||
|
||||
$testExpression = new \Zend_Db_Expr('(sort_order + group_id)');
|
||||
$this->_model->addExpressionFieldToSelect('test_field', $testExpression, ['sort_order', 'group_id']);
|
||||
$this->_model->addFieldToSelect('code', 'code');
|
||||
$columns = $this->_model->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
|
||||
foreach ($columns as $columnEntry) {
|
||||
$actualColumns[] = $columnEntry[2];
|
||||
}
|
||||
|
||||
$this->assertEquals($expectedColumns, $actualColumns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check add expression field doesn't remove all fields from select.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddExpressionFieldToSelectWithoutAdditionalFields()
|
||||
{
|
||||
$expectedColumns = ['*', 'test_field'];
|
||||
|
||||
$testExpression = new \Zend_Db_Expr('(sort_order + group_id)');
|
||||
$this->_model->addExpressionFieldToSelect('test_field', $testExpression, ['sort_order', 'group_id']);
|
||||
$columns = $this->_model->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
|
||||
$actualColumns = [$columns[0][1], $columns[1][2]];
|
||||
|
||||
$this->assertEquals($expectedColumns, $actualColumns);
|
||||
}
|
||||
}
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/**
|
||||
* Test for \Magento\Framework\Model\ResourceModel\Db\Profiler
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Model\ResourceModel\Db;
|
||||
|
||||
use Magento\Framework\Config\ConfigOptionsListConstants;
|
||||
|
||||
/**
|
||||
* Test profiler on database queries
|
||||
*/
|
||||
class ProfilerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\App\ResourceConnection
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected static $_testResourceName = 'testtest_0000_setup';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$_testResourceName = 'testtest_' . random_int(1000, 9999) . '_setup';
|
||||
|
||||
\Magento\Framework\Profiler::enable();
|
||||
} // phpcs:enable
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
\Magento\Framework\Profiler::disable();
|
||||
} // phpcs:enable
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Framework\App\ResourceConnection::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\TestFramework\Db\Adapter\Mysql
|
||||
*/
|
||||
protected function _getConnection()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$reader = $objectManager->get(\Magento\Framework\App\DeploymentConfig::class);
|
||||
$dbConfig = $reader->getConfigData(ConfigOptionsListConstants::KEY_DB);
|
||||
$connectionConfig = $dbConfig['connection']['default'];
|
||||
$connectionConfig['profiler'] = [
|
||||
'class' => \Magento\Framework\Model\ResourceModel\Db\Profiler::class,
|
||||
'enabled' => 'true',
|
||||
];
|
||||
|
||||
return $objectManager->create(\Magento\TestFramework\Db\Adapter\Mysql::class, ['config' => $connectionConfig]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init profiler during creation of DB connect
|
||||
*
|
||||
* @param string $selectQuery
|
||||
* @param int $queryType
|
||||
* @dataProvider profileQueryDataProvider
|
||||
*/
|
||||
public function testProfilerInit($selectQuery, $queryType)
|
||||
{
|
||||
$connection = $this->_getConnection();
|
||||
|
||||
/** @var \Magento\Framework\App\ResourceConnection $resource */
|
||||
$resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\ResourceConnection::class);
|
||||
$testTableName = $resource->getTableName('setup_module');
|
||||
$selectQuery = sprintf($selectQuery, $testTableName);
|
||||
|
||||
$result = $connection->query($selectQuery);
|
||||
if ($queryType == \Zend_Db_Profiler::SELECT) {
|
||||
$result->fetchAll();
|
||||
}
|
||||
|
||||
/** @var \Magento\Framework\Model\ResourceModel\Db\Profiler $profiler */
|
||||
$profiler = $connection->getProfiler();
|
||||
$this->assertInstanceOf(\Magento\Framework\Model\ResourceModel\Db\Profiler::class, $profiler);
|
||||
|
||||
$queryProfiles = $profiler->getQueryProfiles($queryType);
|
||||
$this->assertCount(1, $queryProfiles);
|
||||
|
||||
/** @var \Zend_Db_Profiler_Query $queryProfile */
|
||||
$queryProfile = end($queryProfiles);
|
||||
$this->assertInstanceOf('Zend_Db_Profiler_Query', $queryProfile);
|
||||
|
||||
$this->assertEquals($selectQuery, $queryProfile->getQuery());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function profileQueryDataProvider()
|
||||
{
|
||||
return [
|
||||
["SELECT * FROM %s", \Magento\Framework\DB\Profiler::SELECT],
|
||||
[
|
||||
"INSERT INTO %s (module, schema_version, data_version) " .
|
||||
"VALUES ('" .
|
||||
self::$_testResourceName .
|
||||
"', '1.1', '1.1')",
|
||||
\Magento\Framework\DB\Profiler::INSERT
|
||||
],
|
||||
[
|
||||
"UPDATE %s SET schema_version = '1.2' WHERE module = '" . self::$_testResourceName . "'",
|
||||
\Magento\Framework\DB\Profiler::UPDATE
|
||||
],
|
||||
[
|
||||
"DELETE FROM %s WHERE module = '" . self::$_testResourceName . "'",
|
||||
\Magento\Framework\DB\Profiler::DELETE
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test correct event starting and stopping in magento profile during SQL query fail
|
||||
*/
|
||||
public function testProfilerDuringSqlException()
|
||||
{
|
||||
/** @var \Zend_Db_Adapter_Pdo_Abstract $connection */
|
||||
$connection = $this->_getConnection();
|
||||
|
||||
try {
|
||||
$connection->select()->from('unknown_table')->query()->fetch();
|
||||
} catch (\Magento\Framework\DB\Adapter\TableNotFoundException $exception) {
|
||||
$this->assertNotEmpty($exception);
|
||||
}
|
||||
|
||||
if (!isset($exception)) {
|
||||
$this->fail("Expected exception wasn't thrown!");
|
||||
}
|
||||
|
||||
/** @var \Magento\Framework\App\ResourceConnection $resource */
|
||||
$resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\ResourceConnection::class);
|
||||
$testTableName = $resource->getTableName('setup_module');
|
||||
$connection->select()->from($testTableName)->query()->fetch();
|
||||
|
||||
/** @var \Magento\Framework\Model\ResourceModel\Db\Profiler $profiler */
|
||||
$profiler = $connection->getProfiler();
|
||||
$this->assertInstanceOf(\Magento\Framework\Model\ResourceModel\Db\Profiler::class, $profiler);
|
||||
|
||||
$queryProfiles = $profiler->getQueryProfiles(\Magento\Framework\DB\Profiler::SELECT);
|
||||
$this->assertCount(2, $queryProfiles);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Model\ResourceModel\Entity;
|
||||
|
||||
class TableTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Model\ResourceModel\Entity\Table
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
// @codingStandardsIgnoreStart
|
||||
$config = new \Magento\Framework\Simplexml\Config();
|
||||
$config->table = 'test_table';
|
||||
$config->test_key = 'test';
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Framework\Model\ResourceModel\Entity\Table::class, ['config' => $config]);
|
||||
}
|
||||
|
||||
public function testGetTable()
|
||||
{
|
||||
$this->assertEquals('test_table', $this->_model->getTable());
|
||||
}
|
||||
|
||||
public function testGetConfig()
|
||||
{
|
||||
$this->assertInstanceOf(\Magento\Framework\Simplexml\Config::class, $this->_model->getConfig());
|
||||
$this->assertEquals('test', $this->_model->getConfig('test_key'));
|
||||
$this->assertFalse($this->_model->getConfig('some_key'));
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Model\ResourceModel;
|
||||
|
||||
class IteratorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Model\ResourceModel\Iterator
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* Counter for testing walk() callback
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_callbackCounter = 0;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Model\ResourceModel\Iterator::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testWalk()
|
||||
{
|
||||
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Store\Model\ResourceModel\Store\Collection::class
|
||||
);
|
||||
$this->_model->walk($collection->getSelect(), [[$this, 'walkCallback']]);
|
||||
$this->assertGreaterThan(0, $this->_callbackCounter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper callback for testWalk()
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function walkCallback($data)
|
||||
{
|
||||
$this->_callbackCounter = $data['idx'];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testWalkException()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
|
||||
|
||||
$this->_model->walk('test', [[$this, 'walkCallback']]);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Model\ResourceModel\Type\Db;
|
||||
|
||||
class ConnectionFactoryTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ConnectionFactory
|
||||
*/
|
||||
private $model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new ConnectionFactory(
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$dbInstance = \Magento\TestFramework\Helper\Bootstrap::getInstance()
|
||||
->getBootstrap()
|
||||
->getApplication()
|
||||
->getDbInstance();
|
||||
$dbConfig = [
|
||||
'host' => $dbInstance->getHost(),
|
||||
'username' => $dbInstance->getUser(),
|
||||
'password' => $dbInstance->getPassword(),
|
||||
'dbname' => $dbInstance->getSchema(),
|
||||
'active' => true,
|
||||
];
|
||||
$connection = $this->model->create($dbConfig);
|
||||
$this->assertInstanceOf(\Magento\Framework\DB\Adapter\AdapterInterface::class, $connection);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Framework\Model\ResourceModel\Type\Db\Pdo;
|
||||
|
||||
class MysqlTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testGetConnection()
|
||||
{
|
||||
$db = \Magento\TestFramework\Helper\Bootstrap::getInstance()->getBootstrap()->getApplication()->getDbInstance();
|
||||
$config = [
|
||||
'profiler' => [
|
||||
'class' => \Magento\Framework\DB\Profiler::class,
|
||||
'enabled' => true,
|
||||
],
|
||||
'type' => 'pdo_mysql',
|
||||
'host' => $db->getHost(),
|
||||
'username' => $db->getUser(),
|
||||
'password' => $db->getPassword(),
|
||||
'dbname' => $db->getSchema(),
|
||||
'active' => true,
|
||||
];
|
||||
/** @var \Magento\Framework\Model\ResourceModel\Type\Db\Pdo\Mysql $object */
|
||||
$object = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Framework\Model\ResourceModel\Type\Db\Pdo\Mysql::class,
|
||||
['config' => $config]
|
||||
);
|
||||
|
||||
$connection = $object->getConnection(
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\DB\LoggerInterface::class
|
||||
)
|
||||
);
|
||||
$this->assertInstanceOf(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, $connection);
|
||||
$profiler = $connection->getProfiler();
|
||||
$this->assertInstanceOf(\Magento\Framework\DB\Profiler::class, $profiler);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user