From a24aa5943c4d0f0a5c074b0a5ef9a386709aaf4a Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Tue, 14 May 2019 20:09:39 +0200 Subject: [PATCH] Add FingerprintDiffer --- src/Diff/FingerprintDiffer.php | 62 ++++++++++++ tests/unit/Diff/FingerprintDifferTest.php | 118 ++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 src/Diff/FingerprintDiffer.php create mode 100644 tests/unit/Diff/FingerprintDifferTest.php diff --git a/src/Diff/FingerprintDiffer.php b/src/Diff/FingerprintDiffer.php new file mode 100644 index 00000000..f817cf3c --- /dev/null +++ b/src/Diff/FingerprintDiffer.php @@ -0,0 +1,62 @@ + + */ +class FingerprintDiffer { + + /** + * @param Fingerprint $first + * @param Fingerprint $second + * + * @return Fingerprint Contains the terms present in $first that are not present in $second + */ + public function diff( Fingerprint $first, Fingerprint $second ): Fingerprint { + $difference = new Fingerprint(); + + foreach ( $first->getLabels() as $term ) { + if ( !$second->getLabels()->hasTerm( $term ) ) { + $difference->getLabels()->setTerm( $term ); + } + } + + foreach ( $first->getDescriptions() as $term ) { + if ( !$second->getDescriptions()->hasTerm( $term ) ) { + $difference->getDescriptions()->setTerm( $term ); + } + } + + foreach ( $first->getAliasGroups() as $aliasGroup ) { + $difference->setAliasGroup( + $aliasGroup->getLanguageCode(), + $this->diffAliasGroup( $aliasGroup, $second ) + ); + } + + return $difference; + } + + private function diffAliasGroup( AliasGroup $aliasGroup, Fingerprint $second ) { + if ( $second->hasAliasGroup( $aliasGroup->getLanguageCode() ) ) { + $secondAliases = $second->getAliasGroup( $aliasGroup->getLanguageCode() )->getAliases(); + $differenceAliases = []; + + foreach ( $aliasGroup->getAliases() as $aliasText ) { + if ( !in_array( $aliasText, $secondAliases ) ) { + $differenceAliases[] = $aliasText; + } + } + + return $differenceAliases; + } + + return $aliasGroup->getAliases(); + } + +} diff --git a/tests/unit/Diff/FingerprintDifferTest.php b/tests/unit/Diff/FingerprintDifferTest.php new file mode 100644 index 00000000..c8c1ecc3 --- /dev/null +++ b/tests/unit/Diff/FingerprintDifferTest.php @@ -0,0 +1,118 @@ +assertEquals( + new Fingerprint(), + $this->diff( new Fingerprint(), new Fingerprint() ) + ); + } + + private function diff( Fingerprint $first, Fingerprint $second ): Fingerprint { + return ( new FingerprintDiffer() )->diff( $first, $second ); + } + + public function testTermsNotInSecondAreReturned() { + $first = new Fingerprint( + new TermList( [ new Term( 'en', 'FirstLabel' ) ] ), + new TermList( [ new Term( 'en', 'FirstDesc' ) ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'FirstAliasOne', 'FirstAliasTwo' ] ), + new AliasGroup( 'de', [ 'DeAliasOne', 'DeAliasTwo' ] ), + ] ) + ); + + $this->assertEquals( + $first, + $this->diff( + $first, + new Fingerprint( + new TermList( [ new Term( 'en', 'SecondLabel' ) ] ), + new TermList( [ new Term( 'en', 'SecondDesc' ) ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'SecondAliasOne', 'SecondAliasTwo' ] ), + new AliasGroup( 'different', [ 'DeAliasOne', 'DeAliasTwo' ] ), + ] ) + ) + ) + ); + } + + public function testTermsInSecondAreNotReturned() { + $fingerprint = new Fingerprint( + new TermList( [ new Term( 'en', 'FirstLabel' ) ] ), + new TermList( [ new Term( 'en', 'FirstDesc' ) ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'FirstAliasOne', 'FirstAliasTwo' ] ), + ] ) + ); + + $this->assertEquals( + new Fingerprint(), + $this->diff( + $fingerprint, + $fingerprint + ) + ); + } + + public function testAllTheThings() { + $this->assertEquals( + new Fingerprint( + new TermList( [ + new Term( 'fr', 'OnlyInFirst' ) + ] ), + new TermList( [ + new Term( 'de', 'MismatchLanguage' ) + ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'AliasOnlyInFirst' ] ), + new AliasGroup( 'de', [ 'AliasesOnlyInFirst' ] ) + ] ) + ), + $this->diff( + new Fingerprint( + new TermList( [ + new Term( 'en', 'MatchingTerm' ), + new Term( 'fr', 'OnlyInFirst' ) + ] ), + new TermList( [ + new Term( 'de', 'MismatchLanguage' ) + ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'MatchingAlias', 'AliasOnlyInFirst' ] ), + new AliasGroup( 'de', [ 'AliasesOnlyInFirst' ] ), + ] ) + ), + new Fingerprint( + new TermList( [ + new Term( 'en', 'MatchingTerm' ), + new Term( 'ru', 'OnlyInSecond' ) + ] ), + new TermList( [ + new Term( 'different', 'MismatchLanguage' ) + ] ), + new AliasGroupList( [ + new AliasGroup( 'en', [ 'MatchingAlias', 'AliasOnlyInSecond' ] ), + new AliasGroup( 'ru', [ 'AliasesOnlyInSecond' ] ), + ] ) + ) + ) + ); + } + +}