Skip to content

Commit d525084

Browse files
author
Miniailo, Igor(iminiailo)
committed
Merge pull request #480 from magento-dragons/MAGETWO-40195
[Dragons] New interface and Obsolete connection test
2 parents 03a71ea + a6025b5 commit d525084

File tree

19 files changed

+232
-12
lines changed

19 files changed

+232
-12
lines changed

app/code/Magento/Bundle/Test/Unit/Model/OptionTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ protected function setUp()
4747
);
4848
$this->resource = $this->getMock(
4949
'Magento\Framework\Model\Resource\AbstractResource',
50-
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getSearchableData'],
50+
[
51+
'_construct',
52+
'_getReadAdapter',
53+
'_getWriteAdapter',
54+
'getIdFieldName',
55+
'getSearchableData',
56+
'getConnection'
57+
],
5158
[],
5259
'',
5360
false

app/code/Magento/Catalog/Test/Unit/Model/Entity/AttributeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ protected function setUp()
175175

176176
$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
177177
->setMethods(['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'saveInSetIncluding'])
178-
->getMock();
178+
->getMockForAbstractClass();
179179
$this->cacheManager = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
180180
->getMock();
181181
$this->eventDispatcher = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')

app/code/Magento/Catalog/Test/Unit/Model/Product/ActionTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ public function setUp()
5959
);
6060
$this->resource = $this->getMock(
6161
'\Magento\Framework\Model\Resource\AbstractResource',
62-
['updateAttributes', '_getWriteAdapter', '_getReadAdapter', '_construct', 'getIdFieldName'],
62+
[
63+
'updateAttributes',
64+
'_getWriteAdapter',
65+
'_getReadAdapter',
66+
'_construct',
67+
'getIdFieldName',
68+
'getConnection'
69+
],
6370
[],
6471
'',
6572
false

app/code/Magento/Catalog/Test/Unit/Model/Product/LinkTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ protected function setUp()
6060
'_getWriteAdapter',
6161
'_getReadAdapter',
6262
'_construct',
63-
'getIdFieldName'
63+
'getIdFieldName',
64+
'getConnection'
6465
]
6566
);
6667

app/code/Magento/Catalog/Test/Unit/Model/Resource/Eav/AttributeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function setUp()
8989

9090
$this->resourceMock = $this->getMock(
9191
'Magento\Framework\Model\Resource\AbstractResource',
92-
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName',
92+
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getConnection',
9393
'save', 'saveInSetIncluding', 'isUsedBySuperProducts', 'delete'],
9494
[], '', false
9595
);

app/code/Magento/CatalogInventory/Test/Unit/Model/Adminhtml/Stock/ItemTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function setUp()
1919
{
2020
$resourceMock = $this->getMock(
2121
'Magento\Framework\Model\Resource\AbstractResource',
22-
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName'],
22+
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'getConnection'],
2323
[],
2424
'',
2525
false

app/code/Magento/Cms/Test/Unit/Model/PageTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function setUp()
6767
'_getWriteAdapter',
6868
]
6969
)
70-
->getMock(),
70+
->getMockForAbstractClass(),
7171
$this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
7272
->disableOriginalConstructor()
7373
->getMockForAbstractClass(),

app/code/Magento/Config/Test/Unit/Model/Config/Backend/EncryptedTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ protected function setUp()
4242
'beginTransaction',
4343
'save',
4444
'commit',
45-
'addCommitCallback'
45+
'addCommitCallback',
46+
'getConnection'
4647
],
4748
[],
4849
'',

app/code/Magento/Cookie/Test/Unit/Model/Config/Backend/DomainTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function setUp()
4848
'commit',
4949
'addCommitCallback',
5050
'rollBack',
51+
'getConnection',
5152
],
5253
[],
5354
'',

app/code/Magento/Customer/Test/Unit/Model/AttributeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected function setUp()
161161

162162
$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
163163
->setMethods(['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'saveInSetIncluding'])
164-
->getMock();
164+
->getMockForAbstractClass();
165165
$this->cacheManager = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
166166
->getMock();
167167
$this->eventDispatcher = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
1414
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
1515
use Magento\Framework\App\Config\Element;
16+
use Magento\Framework\App\Resource\Config;
1617
use Magento\Framework\Exception\LocalizedException;
1718
use Magento\Framework\Model\AbstractModel;
1819
use Magento\Framework\Model\Resource\Db\ObjectRelationProcessor;
@@ -275,6 +276,16 @@ protected function _getWriteAdapter()
275276
return $this->_write;
276277
}
277278

279+
/**
280+
* Get connection
281+
*
282+
* @return \Magento\Framework\DB\Adapter\AdapterInterface
283+
*/
284+
protected function getConnection()
285+
{
286+
$this->_resource->getConnection(Config::DEFAULT_SETUP_CONNECTION);
287+
}
288+
278289
/**
279290
* Retrieve read DB connection
280291
*

app/code/Magento/Integration/Test/Unit/Model/Oauth/TokenTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected function setUp()
136136
]
137137
)
138138
->disableOriginalConstructor()
139-
->getMock();
139+
->getMockForAbstractClass();
140140

141141
$this->resourceMock->expects($this->any())
142142
->method('getIdFieldName')

app/code/Magento/Reports/Test/Unit/Model/Product/Index/ComparedTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function setUp()
102102
$this->resourceMock = $this->getMockBuilder('Magento\Framework\Model\Resource\AbstractResource')
103103
->disableOriginalConstructor()
104104
->setMethods(['getIdFieldName', '_construct', '_getReadAdapter', '_getWriteAdapter'])
105-
->getMock();
105+
->getMockForAbstractClass();
106106
$this->dbMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
107107
->disableOriginalConstructor()
108108
->getMock();

app/code/Magento/Tax/Test/Unit/Model/Calculation/RateTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setUp()
2626
$this->resourceMock = $this->getMock(
2727
'Magento\Framework\Model\Resource\AbstractResource',
2828
['_construct', '_getReadAdapter', '_getWriteAdapter', 'getIdFieldName', 'beginTransaction',
29-
'rollBack'],
29+
'rollBack', 'getConnection'],
3030
[],
3131
'',
3232
false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Test\Legacy;
8+
9+
/**
10+
* Temporary test
11+
* Test verifies obsolete usages in modules that were refactored to work with getConnection.
12+
*/
13+
class ObsoleteConnectionTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected $obsoleteMethods = [];
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $obsoleteRegexp = [];
24+
25+
/**
26+
* @var array
27+
*/
28+
protected $filesBlackList = [];
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $appPath;
34+
35+
protected function setUp()
36+
{
37+
$this->appPath = \Magento\Framework\App\Utility\Files::init()->getPathToSource();
38+
$this->obsoleteMethods = [
39+
'_getReadConnection',
40+
'_getWriteConnection',
41+
'_getReadAdapter',
42+
'_getWriteAdapter',
43+
'getReadConnection',
44+
'getWriteConnection',
45+
'getReadAdapter',
46+
'getWriteAdapter',
47+
'getAdapter',
48+
];
49+
50+
$this->obsoleteRegexp = [
51+
'getConnection\\(\'\\w*_*(read|write)',
52+
'\\$_?(read|write)(Connection|Adapter)',
53+
// '\\$write([A-Z]\\w*|\\s)',
54+
];
55+
56+
$this->filesBlackList = $this->getBlackList();
57+
}
58+
59+
/**
60+
* Test verify that obsolete regexps do not appear in refactored folders
61+
*/
62+
public function testObsoleteRegexp()
63+
{
64+
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
65+
$invoker(
66+
function ($file) {
67+
$content = file_get_contents($file);
68+
foreach ($this->obsoleteRegexp as $regexp) {
69+
$this->assertSame(
70+
0,
71+
preg_match('/' . $regexp . '/iS', $content),
72+
"File: $file\nContains obsolete regexp: $regexp. "
73+
);
74+
}
75+
},
76+
$this->modulesFilesDataProvider()
77+
);
78+
}
79+
80+
/**
81+
* Test verify that obsolete methods do not appear in refactored folders
82+
*/
83+
public function testObsoleteResponseMethods()
84+
{
85+
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
86+
$invoker(
87+
function ($file) {
88+
$content = file_get_contents($file);
89+
foreach ($this->obsoleteMethods as $method) {
90+
$quotedMethod = preg_quote($method, '/');
91+
$this->assertSame(
92+
0,
93+
preg_match('/(?<=[a-z\\d_:]|->|function\\s)' . $quotedMethod . '\\s*\\(/iS', $content),
94+
"File: $file\nContains obsolete method: $method . "
95+
);
96+
}
97+
},
98+
$this->modulesFilesDataProvider()
99+
);
100+
}
101+
102+
/**
103+
* Return refactored files
104+
*
105+
* @return array
106+
*/
107+
public function modulesFilesDataProvider()
108+
{
109+
$filesList = [];
110+
111+
foreach ($this->getFilesData('whitelist/refactored_modules*') as $refactoredFolder) {
112+
$files = \Magento\Framework\App\Utility\Files::init()->getFiles(
113+
[$this->appPath . $refactoredFolder],
114+
'*.php'
115+
);
116+
$filesList = array_merge($filesList, $files);
117+
}
118+
119+
$result = array_map('realpath', $filesList);
120+
$result = array_diff($result, $this->filesBlackList);
121+
return \Magento\Framework\App\Utility\Files::composeDataSets($result);
122+
}
123+
124+
/**
125+
* @return array
126+
*/
127+
protected function getBlackList()
128+
{
129+
$blackListFiles = [];
130+
foreach ($this->getFilesData('blacklist/files_list*') as $file) {
131+
$blackListFiles[] = realpath($this->appPath . $file);
132+
}
133+
return $blackListFiles;
134+
}
135+
136+
/**
137+
* @param string $filePattern
138+
* @return array
139+
*/
140+
protected function getFilesData($filePattern)
141+
{
142+
$result = [];
143+
foreach (glob(__DIR__ . '/_files/connection/' . $filePattern) as $file) {
144+
$fileData = include $file;
145+
$result = array_merge($result, $fileData);
146+
}
147+
return $result;
148+
}
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Files that excluded from results
9+
*/
10+
return [
11+
//example '/app/code/Magento/Backend/Model/View.php',
12+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* Modules that were refactored
9+
*/
10+
return [
11+
// example '/app/code/Magento/Catalog',
12+
'/app/code/Magento/Ups',
13+
];

lib/internal/Magento/Framework/Model/Resource/AbstractResource.php

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ abstract protected function _getReadAdapter();
5757
*/
5858
abstract protected function _getWriteAdapter();
5959

60+
/**
61+
* Get connection
62+
*
63+
* @return \Magento\Framework\DB\Adapter\AdapterInterface
64+
*/
65+
abstract protected function getConnection();
66+
6067
/**
6168
* Start resource transaction
6269
*

lib/internal/Magento/Framework/Model/Resource/Db/AbstractDb.php

+11
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,17 @@ protected function _getConnection($resourceName)
310310
return $connectionInstance;
311311
}
312312

313+
/**
314+
* Get connection
315+
*
316+
* @return \Magento\Framework\DB\Adapter\AdapterInterface|false
317+
*/
318+
protected function getConnection()
319+
{
320+
$fullResourceName = ($this->_resourcePrefix ? $this->_resourcePrefix . '_' : '') . 'write';
321+
return $this->_resources->getConnection($fullResourceName);
322+
}
323+
313324
/**
314325
* Retrieve connection for read data
315326
*

0 commit comments

Comments
 (0)