|
| 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 | +} |
0 commit comments