Skip to content

Commit 826bff0

Browse files
author
Joan He
committed
Merge branch 'MAGETWO-28383' of https://github.corp.ebay.com/magento-extensibility/magento2ce into MAGETWO-28383
2 parents ca2ff11 + 908f180 commit 826bff0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+145
-19
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/***
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\App\Utility;
8+
9+
use Magento\Framework\App\Utility\Files;
10+
11+
class FilesTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var \Magento\Framework\App\Utility\Files */
14+
protected $model;
15+
16+
/** @var string */
17+
protected $moduleTests = '#app/code/[\\w]+/[\\w]+/Test#';
18+
19+
/** @var string */
20+
protected $toolsTests = '#dev/tools/Magento/Tools/[\\w]+/Test#';
21+
22+
/** @var string */
23+
protected $frameworkTests = '#lib/internal/Magento/Framework/[\\w]+/Test#';
24+
25+
/** @var string */
26+
protected $libTests = '#lib/internal/[\\w]+/[\\w]+/Test#';
27+
28+
/** @var string */
29+
protected $rootTestsDir = '#dev/tests/#';
30+
31+
/** @var string */
32+
protected $setupTestsDir = '#setup/src/Magento/Setup/Test#';
33+
34+
public function setUp()
35+
{
36+
$this->model = new Files(BP);
37+
}
38+
39+
public function testGetPhpFilesExcludeTests()
40+
{
41+
$this->assertNoTestDirs(
42+
$this->model->getPhpFiles(true, true, true, false)
43+
);
44+
}
45+
46+
public function testGetComposerExcludeTests()
47+
{
48+
$this->assertNoTestDirs(
49+
$this->model->getComposerFiles('code', false)
50+
);
51+
}
52+
53+
public function testGetClassFilesExcludeTests()
54+
{
55+
$this->assertNoTestDirs(
56+
$this->model->getClassFiles(true, false, true, true, false)
57+
);
58+
}
59+
60+
public function testGetClassFilesOnlyTests()
61+
{
62+
$classFiles = $this->model->getClassFiles(false, true, false, false, false);
63+
64+
$classFiles = preg_grep($this->moduleTests, $classFiles, PREG_GREP_INVERT);
65+
$classFiles = preg_grep($this->libTests, $classFiles, PREG_GREP_INVERT);
66+
$classFiles = preg_grep($this->frameworkTests, $classFiles, PREG_GREP_INVERT);
67+
$classFiles = preg_grep($this->toolsTests, $classFiles, PREG_GREP_INVERT);
68+
$classFiles = preg_grep($this->rootTestsDir, $classFiles, PREG_GREP_INVERT);
69+
$classFiles = preg_grep($this->setupTestsDir, $classFiles, PREG_GREP_INVERT);
70+
71+
$this->assertEmpty($classFiles);
72+
}
73+
74+
/**
75+
* Verify that the given array of files does not contain anything in test directories
76+
*
77+
* @param array $files
78+
*/
79+
protected function assertNoTestDirs($files)
80+
{
81+
$this->assertEmpty(preg_grep($this->moduleTests, $files));
82+
$this->assertEmpty(preg_grep($this->frameworkTests, $files));
83+
$this->assertEmpty(preg_grep($this->libTests, $files));
84+
$this->assertEmpty(preg_grep($this->toolsTests, $files));
85+
}
86+
}

dev/tests/unit/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
bootstrap="./framework/bootstrap.php"
1212
>
1313
<testsuite name="Magento Unit Tests">
14-
<directory suffix="Test.php">../../../setup/Test/Unit</directory>
14+
<directory suffix="Test.php">../../../setup/src/Magento/Setup/Test/Unit</directory>
1515
<directory suffix="Test.php">../../../app/code/Magento/*/Test/Unit</directory>
1616
<directory suffix="Test.php">../../../lib/internal/Magento/Framework/Test/Unit</directory>
1717
<directory suffix="Test.php">../../../lib/internal/Magento/Framework/*/Test/Unit</directory>

lib/internal/Magento/Framework/App/Utility/Files.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class Files
3131
*/
3232
protected $_path = '';
3333

34+
/** @var string regex for test directories in app/code */
35+
protected $moduleTestDirs = '#app/code/[\\w]+/[\\w]+/Test#';
36+
37+
/** @var string regex for test directories in tools */
38+
protected $toolsTestDirs = '#dev/tools/Magento/Tools/[\\w]+/Test#';
39+
40+
/** @var string regex for test directories in framework */
41+
protected $frameworkTestDirs = '#lib/internal/Magento/Framework/[\\w]+/Test#';
42+
43+
/** @var string regex for test directories in lib/internal */
44+
protected $libTestDirs = '#lib/internal/[\\w]+/[\\w]+/Test#';
45+
3446
/**
3547
* Setter for an instance of self
3648
*
@@ -112,15 +124,20 @@ public function getPhpFiles($appCode = true, $otherCode = true, $templates = tru
112124
if ($appCode) {
113125
$files = array_merge(
114126
glob($this->_path . '/app/*.php', GLOB_NOSORT),
115-
self::getFiles(["{$this->_path}/app/code/{$namespace}/{$module}"], '*.php')
127+
$this->getFilesSubset(
128+
["{$this->_path}/app/code/{$namespace}/{$module}"],
129+
'*.php',
130+
$this->moduleTestDirs
131+
)
116132
);
117133
}
118134
if ($otherCode) {
135+
$exclude = [$this->libTestDirs, $this->frameworkTestDirs];
119136
$files = array_merge(
120137
$files,
121138
glob($this->_path . '/*.php', GLOB_NOSORT),
122139
glob($this->_path . '/pub/*.php', GLOB_NOSORT),
123-
self::getFiles(["{$this->_path}/lib/internal/Magento"], '*.php'),
140+
$this->getFilesSubset(["{$this->_path}/lib/internal/Magento"], '*.php', $exclude),
124141
self::getFiles(["{$this->_path}/dev/tools/Magento/Tools/SampleData"], '*.php')
125142
);
126143
}
@@ -140,49 +157,52 @@ public function getPhpFiles($appCode = true, $otherCode = true, $templates = tru
140157
* Returns list of files, where expected to have class declarations
141158
*
142159
* @param bool $appCode application PHP-code
143-
* @param bool $devTests
160+
* @param bool $tests
144161
* @param bool $devTools
145162
* @param bool $lib
146163
* @param bool $asDataSet
147164
* @return array
148165
*/
149166
public function getClassFiles(
150167
$appCode = true,
151-
$devTests = true,
168+
$tests = true,
152169
$devTools = true,
153170
$lib = true,
154171
$asDataSet = true
155172
) {
156-
$key = __METHOD__ . "/{$this->_path}/{$appCode}/{$devTests}/{$devTools}/{$lib}";
173+
$key = __METHOD__ . "/{$this->_path}/{$appCode}/{$tests}/{$devTools}/{$lib}";
157174
if (!isset(self::$_cache[$key])) {
158175
$files = [];
159176
if ($appCode) {
160-
$appFiles = self::getFiles(["{$this->_path}/app/code/Magento"], '*.php');
161-
$appFiles = preg_grep('#app/code/[\\w]+/[\\w]+/Test#', $appFiles, PREG_GREP_INVERT);
162-
$files = array_merge($files, $appFiles);
177+
$files = array_merge(
178+
$files,
179+
$this->getFilesSubset(["{$this->_path}/app/code/Magento"], '*.php', $this->moduleTestDirs)
180+
);
163181
}
164-
if ($devTests) {
182+
if ($tests) {
165183
$testDirs = [
166184
"{$this->_path}/dev/tests",
167185
"{$this->_path}/app/code/*/*/Test",
168186
"{$this->_path}/lib/internal/*/*/Test",
169187
"{$this->_path}/lib/internal/Magento/Framework/*/Test",
170188
"{$this->_path}/dev/tools/Magento/Tools/*/Test",
171-
"{$this->_path}/setup/Test",
189+
"{$this->_path}/setup/src/Magento/Setup/Test",
172190

173191
];
174192
$files = array_merge($files, self::getFiles($testDirs, '*.php'));
175193
}
176194
if ($devTools) {
177-
$toolFiles = self::getFiles(["{$this->_path}/dev/tools/Magento"], '*.php');
178-
$toolFiles = preg_grep('#dev/tools/Magento/Tools/[\\w]+/Test#', $toolFiles, PREG_GREP_INVERT);
179-
$files = array_merge($files, $toolFiles);
195+
$files = array_merge(
196+
$files,
197+
$this->getFilesSubset(["{$this->_path}/dev/tools/Magento"], '*.php', $this->toolsTestDirs)
198+
);
180199
}
181200
if ($lib) {
182-
$libFiles = self::getFiles(["{$this->_path}/lib/internal/Magento"], '*.php');
183-
$libFiles = preg_grep('#lib/internal/Magento/Framework/[\\w]+/Test#', $libFiles, PREG_GREP_INVERT);
184-
$libFiles = preg_grep('#lib/internal/[\\w]+/[\\w]+/Test#', $libFiles, PREG_GREP_INVERT);
185-
$files = array_merge($files, $libFiles);
201+
$exclude = [$this->libTestDirs, $this->frameworkTestDirs];
202+
$files = array_merge(
203+
$files,
204+
$this->getFilesSubset(["{$this->_path}/lib/internal/Magento"], '*.php', $exclude)
205+
);
186206
}
187207
self::$_cache[$key] = $files;
188208
}
@@ -1049,7 +1069,7 @@ public function getComposerFiles($appDir, $asDataSet = true)
10491069
{
10501070
$key = __METHOD__ . '|' . $this->_path . '|' . serialize(func_get_args());
10511071
if (!isset(self::$_cache[$key])) {
1052-
$files = self::getFiles(["{$this->_path}/app/{$appDir}"], 'composer.json');
1072+
$files = $this->getFilesSubset(["{$this->_path}/app/{$appDir}"], 'composer.json', $this->moduleTestDirs);
10531073
self::$_cache[$key] = $files;
10541074
}
10551075

@@ -1111,4 +1131,24 @@ public function isModuleExists($moduleName)
11111131

11121132
return self::$_cache[$key];
11131133
}
1134+
1135+
/**
1136+
* Returns list of files in a given directory, minus files in specifically excluded directories.
1137+
*
1138+
* @param array $dirPatterns Directories to search in
1139+
* @param string $fileNamePattern Pattern for filename
1140+
* @param string|array $excludes Subdirectories to exlude, represented as regex
1141+
* @return array Files in $dirPatterns but not in $excludes
1142+
*/
1143+
protected function getFilesSubset(array $dirPatterns, $fileNamePattern, $excludes)
1144+
{
1145+
if (!is_array($excludes)) {
1146+
$excludes = [$excludes];
1147+
}
1148+
$fileSet = self::getFiles($dirPatterns, $fileNamePattern);
1149+
foreach ($excludes as $excludeRegex) {
1150+
$fileSet = preg_grep($excludeRegex, $fileSet, PREG_GREP_INVERT);
1151+
}
1152+
return $fileSet;
1153+
}
11141154
}

0 commit comments

Comments
 (0)