Skip to content

Commit 0181ef7

Browse files
committed
Fixed Unit tests
1 parent 09ec6f9 commit 0181ef7

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

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,

0 commit comments

Comments
 (0)