Skip to content

Commit

Permalink
Merge pull request #480 from magento-dragons/MAGETWO-40195
Browse files Browse the repository at this point in the history
[Dragons] New interface and Obsolete connection test
  • Loading branch information
Miniailo, Igor(iminiailo) committed Jul 17, 2015
2 parents 03a71ea + a6025b5 commit d525084
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 12 deletions.
9 changes: 8 additions & 1 deletion app/code/Magento/Bundle/Test/Unit/Model/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ protected function setUp()
);
$this->resource = $this->getMock(
'Magento\Framework\Model\Resource\AbstractResource',
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getSearchableData'],
[
'_construct',
'_getReadAdapter',
'_getWriteAdapter',
'getIdFieldName',
'getSearchableData',
'getConnection'
],
[],
'',
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function setUp()

$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
->setMethods(['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'saveInSetIncluding'])
->getMock();
->getMockForAbstractClass();
$this->cacheManager = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
->getMock();
$this->eventDispatcher = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public function setUp()
);
$this->resource = $this->getMock(
'\Magento\Framework\Model\Resource\AbstractResource',
['updateAttributes', '_getWriteAdapter', '_getReadAdapter', '_construct', 'getIdFieldName'],
[
'updateAttributes',
'_getWriteAdapter',
'_getReadAdapter',
'_construct',
'getIdFieldName',
'getConnection'
],
[],
'',
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ protected function setUp()
'_getWriteAdapter',
'_getReadAdapter',
'_construct',
'getIdFieldName'
'getIdFieldName',
'getConnection'
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function setUp()

$this->resourceMock = $this->getMock(
'Magento\Framework\Model\Resource\AbstractResource',
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName',
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getConnection',
'save', 'saveInSetIncluding', 'isUsedBySuperProducts', 'delete'],
[], '', false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp()
{
$resourceMock = $this->getMock(
'Magento\Framework\Model\Resource\AbstractResource',
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName'],
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getConnection'],
[],
'',
false
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Cms/Test/Unit/Model/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function setUp()
'_getWriteAdapter',
]
)
->getMock(),
->getMockForAbstractClass(),
$this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
->disableOriginalConstructor()
->getMockForAbstractClass(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected function setUp()
'beginTransaction',
'save',
'commit',
'addCommitCallback'
'addCommitCallback',
'getConnection'
],
[],
'',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected function setUp()
'commit',
'addCommitCallback',
'rollBack',
'getConnection',
],
[],
'',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function setUp()

$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
->setMethods(['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'saveInSetIncluding'])
->getMock();
->getMockForAbstractClass();
$this->cacheManager = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
->getMock();
$this->eventDispatcher = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/Eav/Model/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\App\Config\Element;
use Magento\Framework\App\Resource\Config;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Resource\Db\ObjectRelationProcessor;
Expand Down Expand Up @@ -275,6 +276,16 @@ protected function _getWriteAdapter()
return $this->_write;
}

/**
* Get connection
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected function getConnection()
{
$this->_resource->getConnection(Config::DEFAULT_SETUP_CONNECTION);
}

/**
* Retrieve read DB connection
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function setUp()
]
)
->disableOriginalConstructor()
->getMock();
->getMockForAbstractClass();

$this->resourceMock->expects($this->any())
->method('getIdFieldName')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function setUp()
$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
->disableOriginalConstructor()
->setMethods(['getIdFieldName', '_construct', '_getReadAdapter', '_getWriteAdapter'])
->getMock();
->getMockForAbstractClass();
$this->dbMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
->disableOriginalConstructor()
->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setUp()
$this->resourceMock = $this->getMock(
'Magento\Framework\Model\Resource\AbstractResource',
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'beginTransaction',
'rollBack'],
'rollBack', 'getConnection'],
[],
'',
false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Test\Legacy;

/**
* Temporary test
* Test verifies obsolete usages in modules that were refactored to work with getConnection.
*/
class ObsoleteConnectionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var array
*/
protected $obsoleteMethods = [];

/**
* @var array
*/
protected $obsoleteRegexp = [];

/**
* @var array
*/
protected $filesBlackList = [];

/**
* @var string
*/
protected $appPath;

protected function setUp()
{
$this->appPath = \Magento\Framework\App\Utility\Files::init()->getPathToSource();
$this->obsoleteMethods = [
'_getReadConnection',
'_getWriteConnection',
'_getReadAdapter',
'_getWriteAdapter',
'getReadConnection',
'getWriteConnection',
'getReadAdapter',
'getWriteAdapter',
'getAdapter',
];

$this->obsoleteRegexp = [
'getConnection\\(\'\\w*_*(read|write)',
'\\$_?(read|write)(Connection|Adapter)',
// '\\$write([A-Z]\\w*|\\s)',
];

$this->filesBlackList = $this->getBlackList();
}

/**
* Test verify that obsolete regexps do not appear in refactored folders
*/
public function testObsoleteRegexp()
{
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
$invoker(
function ($file) {
$content = file_get_contents($file);
foreach ($this->obsoleteRegexp as $regexp) {
$this->assertSame(
0,
preg_match('/' . $regexp . '/iS', $content),
"File: $file\nContains obsolete regexp: $regexp. "
);
}
},
$this->modulesFilesDataProvider()
);
}

/**
* Test verify that obsolete methods do not appear in refactored folders
*/
public function testObsoleteResponseMethods()
{
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
$invoker(
function ($file) {
$content = file_get_contents($file);
foreach ($this->obsoleteMethods as $method) {
$quotedMethod = preg_quote($method, '/');
$this->assertSame(
0,
preg_match('/(?<=[a-z\\d_:]|->|function\\s)' . $quotedMethod . '\\s*\\(/iS', $content),
"File: $file\nContains obsolete method: $method . "
);
}
},
$this->modulesFilesDataProvider()
);
}

/**
* Return refactored files
*
* @return array
*/
public function modulesFilesDataProvider()
{
$filesList = [];

foreach ($this->getFilesData('whitelist/refactored_modules*') as $refactoredFolder) {
$files = \Magento\Framework\App\Utility\Files::init()->getFiles(
[$this->appPath . $refactoredFolder],
'*.php'
);
$filesList = array_merge($filesList, $files);
}

$result = array_map('realpath', $filesList);
$result = array_diff($result, $this->filesBlackList);
return \Magento\Framework\App\Utility\Files::composeDataSets($result);
}

/**
* @return array
*/
protected function getBlackList()
{
$blackListFiles = [];
foreach ($this->getFilesData('blacklist/files_list*') as $file) {
$blackListFiles[] = realpath($this->appPath . $file);
}
return $blackListFiles;
}

/**
* @param string $filePattern
* @return array
*/
protected function getFilesData($filePattern)
{
$result = [];
foreach (glob(__DIR__ . '/_files/connection/' . $filePattern) as $file) {
$fileData = include $file;
$result = array_merge($result, $fileData);
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Files that excluded from results
*/
return [
//example '/app/code/Magento/Backend/Model/View.php',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Modules that were refactored
*/
return [
// example '/app/code/Magento/Catalog',
'/app/code/Magento/Ups',
];
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ abstract protected function _getReadAdapter();
*/
abstract protected function _getWriteAdapter();

/**
* Get connection
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
abstract protected function getConnection();

/**
* Start resource transaction
*
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/Magento/Framework/Model/Resource/Db/AbstractDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ protected function _getConnection($resourceName)
return $connectionInstance;
}

/**
* Get connection
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface|false
*/
protected function getConnection()
{
$fullResourceName = ($this->_resourcePrefix ? $this->_resourcePrefix . '_' : '') . 'write';
return $this->_resources->getConnection($fullResourceName);
}

/**
* Retrieve connection for read data
*
Expand Down

0 comments on commit d525084

Please sign in to comment.