Skip to content

Commit 960bad1

Browse files
authored
Merge pull request #22 from magento/EQPS-416
Eqps 416 - magento-semver doesn't work correctly as standalone application
2 parents cd0f3e4 + 0181ef7 commit 960bad1

File tree

12 files changed

+170
-13
lines changed

12 files changed

+170
-13
lines changed

.travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: php
2+
3+
php:
4+
- '7.1'
5+
- '7.2'
6+
- '7.3'
7+
8+
matrix:
9+
fast_finish: true
10+
11+
before_install:
12+
- composer install --prefer-source --no-interaction --dev
13+
14+
script: php vendor/phpunit/phpunit/phpunit --configuration tests/Unit/phpunit.xml.dist

src/Console/Command/CompareSourceCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private function validateAllowedLevel($input)
216216
*/
217217
private function getHtmlHeader()
218218
{
219-
$css = file_get_contents(__DIR__ . '/../../../../../../build/core_dev/bootstrap.min.css');
219+
$css = file_get_contents(__DIR__ . '/css/bootstrap.min.css');
220220

221221
return <<<HEADER
222222
<!DOCTYPE html>

src/Console/Command/css/bootstrap.min.css

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Filter/SourceWithJsonFilter.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SemanticVersionChecker\Filter;
9+
10+
/**
11+
* Filter Implementation to remove not changed files from the file list.
12+
*/
13+
class SourceWithJsonFilter
14+
{
15+
/**
16+
* Filters unchanged files
17+
*
18+
* @param array $filesBefore
19+
* @param array $filesAfter
20+
*
21+
* @return int
22+
*/
23+
public function filter(array &$filesBefore, array &$filesAfter): int
24+
{
25+
$hashedBefore = [];
26+
$jsonHashedBefore = [];
27+
foreach ($filesBefore as $fileBefore) {
28+
if ($this->isJson($fileBefore)) {
29+
$jsonHashedBefore[] = $fileBefore;
30+
} else {
31+
$hashedBefore[$this->getHash($fileBefore)] = $fileBefore;
32+
}
33+
}
34+
35+
$hashedAfter = [];
36+
$jsonHashedAfter = [];
37+
foreach ($filesAfter as $fileAfter) {
38+
if ($this->isJson($fileAfter)) {
39+
$jsonHashedAfter[] = $fileAfter;
40+
} else {
41+
$hashedAfter[$this->getHash($fileAfter)] = $fileAfter;
42+
}
43+
}
44+
45+
$intersection = array_intersect_key($hashedBefore, $hashedAfter);
46+
$filesBefore = array_merge(array_values(array_diff_key($hashedBefore, $intersection)), $jsonHashedBefore);
47+
$filesAfter = array_merge(array_values(array_diff_key($hashedAfter, $intersection)), $jsonHashedAfter);
48+
49+
return count($intersection);
50+
}
51+
52+
/**
53+
* Checks if file has JSON extension
54+
*
55+
* @param string $file
56+
*
57+
* @return bool
58+
*/
59+
private function isJson(string $file): bool
60+
{
61+
return (bool)preg_match('/^.*.json/', $file);
62+
}
63+
64+
/**
65+
* Returns the an sha1 file hash to filter not changed files.
66+
*
67+
* @param string $fileName
68+
* @return string
69+
*/
70+
private function getHash(string $fileName): string
71+
{
72+
// for xml we need to cache also the parts of dirname if move a xml than it should be also marked as change.
73+
if (preg_match('/^.*.xml$/', $fileName)) {
74+
$path = basename(pathinfo($fileName, PATHINFO_DIRNAME));
75+
return sha1(file_get_contents($fileName).$path);
76+
}
77+
78+
return sha1_file($fileName);
79+
}
80+
}

src/Helper/ClassParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function getFullyQualifiedName(string $alias): string
333333
private function retrieveFilePath($namespace)
334334
{
335335
// Concession to SVC unit testing; test classes do not exist in the normal source paths
336-
$testDirPath = 'SemanticVersionChecker' . DIRECTORY_SEPARATOR . 'Test' . DIRECTORY_SEPARATOR;
336+
$testDirPath = 'tests' . DIRECTORY_SEPARATOR . 'Unit' . DIRECTORY_SEPARATOR;
337337
if (strpos($this->filePath, $testDirPath) !== false) {
338338
$testSourceDir = substr($this->filePath, 0, strrpos($this->filePath, DIRECTORY_SEPARATOR));
339339
$fileName = substr($namespace, strrpos($namespace, '\\') ?: 0) . '.php';

src/ReportBuilder.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Magento\SemanticVersionChecker\ClassHierarchy\StaticAnalyzerFactory;
2121
use Magento\SemanticVersionChecker\Analyzer\Factory\LessAnalyzerFactory;
2222
use Magento\SemanticVersionChecker\Filter\FilePatternFilter;
23-
use Magento\SemanticVersionChecker\Filter\SourceFilter;
23+
use Magento\SemanticVersionChecker\Filter\SourceWithJsonFilter;
2424
use Magento\SemanticVersionChecker\Finder\FinderDecoratorFactory;
2525
use Magento\SemanticVersionChecker\Scanner\ScannerRegistryFactory;
2626
use PHPSemVerChecker\Configuration\LevelMapping;
@@ -131,11 +131,6 @@ protected function buildReport()
131131
$sourceBeforeFiles = $fileIterator->findFromString($this->sourceBeforeDir, '', '');
132132
$sourceAfterFiles = $fileIterator->findFromString($this->sourceAfterDir, '', '');
133133

134-
foreach ($this->getFilters($this->sourceBeforeDir, $this->sourceAfterDir) as $filter) {
135-
// filters modify arrays by reference
136-
$filter->filter($sourceBeforeFiles, $sourceAfterFiles);
137-
}
138-
139134
//let static analyzer build a complete dependency graph
140135
$staticAnalyzer = (new StaticAnalyzerFactory())->create();
141136
$staticAnalyzer->analyse($sourceBeforeFiles);
@@ -146,6 +141,11 @@ protected function buildReport()
146141
$scannerBefore = new ScannerRegistry($scannerRegistryFactory->create($dependencyMap));
147142
$scannerAfter = new ScannerRegistry($scannerRegistryFactory->create($dependencyMap));
148143

144+
foreach ($this->getFilters($this->sourceBeforeDir, $this->sourceAfterDir) as $filter) {
145+
// filters modify arrays by reference
146+
$filter->filter($sourceBeforeFiles, $sourceAfterFiles);
147+
}
148+
149149
foreach ($sourceBeforeFiles as $file) {
150150
$scannerBefore->scanFile($file);
151151
}
@@ -190,8 +190,8 @@ protected function buildReport()
190190
protected function getFilters($sourceBeforeDir, $sourceAfterDir): array
191191
{
192192
$filters = [
193-
// always filter out files that are identical before and after
194-
new SourceFilter(),
193+
// always filter out files that are identical before and after except for JSON :facepalm:
194+
new SourceWithJsonFilter(),
195195
// process the include and exclude patterns
196196
new FilePatternFilter(
197197
$this->includePatternsPath,

tests/Unit/Console/Command/CompareSourceCommandApiClassesTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Tools\SemanticVersionChecker\Test\Unit\Console\Command;
6+
namespace Magento\SemanticVersionChecker\Test\Unit\Console\Command;
77

8-
use Magento\Tools\SemanticVersionChecker\Test\Unit\Console\Command\CompareSourceCommandTest\AbstractTestCase;
8+
use Magento\SemanticVersionChecker\Test\Unit\Console\Command\CompareSourceCommandTest\AbstractTestCase;
99

1010
/**
1111
* Test semantic version checker CLI command dealing with API classes.
@@ -461,4 +461,4 @@ public function changesDataProvider()
461461
]
462462
];
463463
}
464-
}
464+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Test\Vcs;
8+
9+
/**
10+
* @api
11+
*/
12+
class ApiClass extends BaseApiClass
13+
{
14+
public function testFunction(): string
15+
{
16+
return 'ApiClass';
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Test\Vcs;
4+
5+
class BaseApiClass
6+
{
7+
public function testFunction(): string
8+
{
9+
return 'baseApiClass';
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Test\Vcs;
8+
9+
/**
10+
* @api
11+
*/
12+
class ApiClass extends BaseApiClass
13+
{
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Test\Vcs;
4+
5+
class BaseApiClass
6+
{
7+
public function testFunction(): string
8+
{
9+
return 'baseApiClass';
10+
}
11+
}

tests/Unit/tmp/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
!/.gitignore

0 commit comments

Comments
 (0)