-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d546194
commit 348ec7d
Showing
9 changed files
with
149 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
// @codingStandardsIgnoreFile | ||
namespace Magento\SemanticVersionChecker; | ||
|
||
use PHPSemVerChecker\Report\Report; | ||
|
||
class MergedReport extends Report | ||
{ | ||
/** | ||
* Merges with the given report including any non-standard contexts | ||
* | ||
* @param Report $report | ||
* @return $this | ||
*/ | ||
public function merge(Report $report) | ||
{ | ||
foreach ($report->differences as $context => $levels) { | ||
if (!key_exists($context, $this->differences)) { | ||
$this->differences[$context] = $levels; | ||
} else { | ||
foreach ($levels as $level => $differences) { | ||
$this->differences[$context][$level] = array_merge($this->differences[$context][$level], $differences); | ||
} | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\SemanticVersionChecker\Test\Unit; | ||
|
||
use Magento\SemanticVersionChecker\InjectableReport; | ||
use Magento\SemanticVersionChecker\MergedReport; | ||
use Magento\SemanticVersionChecker\Operation\ClassConstantRemoved; | ||
use Magento\SemanticVersionChecker\Operation\ClassConstructorOptionalParameterAdded; | ||
use Magento\SemanticVersionChecker\Operation\ClassExtendsAdded; | ||
use Magento\SemanticVersionChecker\Operation\ClassImplementsAdded; | ||
use Magento\SemanticVersionChecker\Operation\ClassMethodOptionalParameterAdded; | ||
use Magento\SemanticVersionChecker\Operation\ClassTraitAdded; | ||
use Magento\SemanticVersionChecker\Operation\DropForeignKey; | ||
use Magento\SemanticVersionChecker\Operation\SystemXml\FieldAdded; | ||
use PHPSemVerChecker\Report\Report; | ||
use PHPSemVerChecker\SemanticVersioning\Level; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MergedReportTest extends TestCase | ||
{ | ||
/** | ||
* Verify that reports are properly merged including non-standard contexts | ||
* | ||
* @return void | ||
*/ | ||
public function testMergedChanges() | ||
{ | ||
$diffsWithDatabase = []; | ||
$diffsWithDatabase['class'][Level::PATCH] = [$this->createMock(ClassConstructorOptionalParameterAdded::class)]; | ||
$diffsWithDatabase['class'][Level::MINOR] = [ | ||
$this->createMock(ClassExtendsAdded::class), | ||
$this->createMock(ClassMethodOptionalParameterAdded::class) | ||
]; | ||
$diffsWithDatabase['database'][Level::MAJOR] = [$this->createMock(DropForeignKey::class)]; | ||
$databaseReport = new InjectableReport($diffsWithDatabase); | ||
|
||
$diffsWithXml = []; | ||
$diffsWithXml['class'][Level::MAJOR] = [$this->createMock(ClassConstantRemoved::class)]; | ||
$diffsWithXml['class'][Level::MINOR] = [$this->createMock(ClassImplementsAdded::class)]; | ||
$diffsWithXml['xml'][Level::MINOR] = [$this->createMock(FieldAdded::class)]; | ||
$xmlReport = new InjectableReport($diffsWithXml); | ||
|
||
/** @var MockObject|ClassTraitAdded $preMergeOperation */ | ||
$preMergeOperation = $this->createMock(ClassTraitAdded::class); | ||
$preMergeOperation->expects($this->any())->method('getLevel')->willReturn(Level::MINOR); | ||
|
||
$mergedReport = new MergedReport(); | ||
$mergedReport->addClass($preMergeOperation); | ||
$mergedReport->merge($databaseReport); | ||
$mergedReport->merge($xmlReport); | ||
|
||
$mergedDiffs = $mergedReport->getDifferences(); | ||
$this->assertTrue(key_exists('database', $mergedDiffs)); | ||
$this->assertTrue(key_exists('xml', $mergedDiffs)); | ||
$this->assertTrue($this->hasAllDifferences($mergedReport, $databaseReport)); | ||
$this->assertTrue($this->hasAllDifferences($mergedReport, $xmlReport)); | ||
$this->assertTrue(array_search($preMergeOperation, $mergedDiffs['class'][Level::MINOR]) !== false); | ||
} | ||
|
||
/** | ||
* Checks if a merged report contains all differences in another report | ||
* @param Report $mergedReport | ||
* @param Report $inputReport | ||
* @return bool | ||
*/ | ||
private function hasAllDifferences($mergedReport, $inputReport) | ||
{ | ||
$mergedDiffs = $mergedReport->getDifferences(); | ||
$inputDiffs = $inputReport->getDifferences(); | ||
foreach ($inputDiffs as $context => $levels) { | ||
if (!key_exists($context, $mergedDiffs)) { | ||
return false; | ||
} | ||
$mergedLevels = $mergedDiffs[$context]; | ||
foreach ($levels as $level => $operations) { | ||
if (!key_exists($level, $mergedLevels)) { | ||
return false; | ||
} | ||
$mergedOperations = $mergedLevels[$level]; | ||
foreach ($operations as $operation) { | ||
if (array_search($operation, $mergedOperations) === false) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |