Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Nov 25, 2021
1 parent 1f5b995 commit 5a1cc4a
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 30 deletions.
4 changes: 4 additions & 0 deletions tests/Core/Serialize/DOM/Normalizers/BomNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public function testNormalizeDependenciesOmitWhenEmpty(): void
);
}

// region normalize ExternalReferences

public function testNormalizeExternalReferences(): void
{
$spec = $this->createConfiguredMock(
Expand Down Expand Up @@ -360,4 +362,6 @@ public function testNormalizeExternalReferencesOmitIfEmpty(): void
$actual
);
}

// endregion normalize ExternalReferences
}
114 changes: 99 additions & 15 deletions tests/Core/Serialize/DOM/Normalizers/ComponentNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
use CycloneDX\Core\Models\License\DisjunctiveLicenseWithName;
use CycloneDX\Core\Models\License\LicenseExpression;
use CycloneDX\Core\Repositories\DisjunctiveLicenseRepository;
use CycloneDX\Core\Repositories\ExternalReferenceRepository;
use CycloneDX\Core\Repositories\HashRepository;
use CycloneDX\Core\Serialize\DOM\NormalizerFactory;
use CycloneDX\Core\Serialize\DOM\Normalizers\ComponentNormalizer;
use CycloneDX\Core\Serialize\DOM\Normalizers\DisjunctiveLicenseRepositoryNormalizer;
use CycloneDX\Core\Serialize\DOM\Normalizers\HashRepositoryNormalizer;
use CycloneDX\Core\Serialize\DOM\Normalizers\LicenseExpressionNormalizer;
use CycloneDX\Core\Serialize\DOM\Normalizers;
use CycloneDX\Core\Spec\SpecInterface;
use CycloneDX\Tests\_traits\DomNodeAssertionTrait;
use DomainException;
Expand Down Expand Up @@ -62,7 +60,7 @@ public function testNormalizeThrowsOnUnsupportedType(): void
);
$spec = $this->createMock(SpecInterface::class);
$factory = $this->createConfiguredMock(NormalizerFactory::class, ['getSpec' => $spec]);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
Expand Down Expand Up @@ -94,7 +92,7 @@ public function testNormalizeMinimal(): void
NormalizerFactory::class,
['getSpec' => $spec, 'getDocument' => new DOMDocument()]
);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
Expand Down Expand Up @@ -137,8 +135,8 @@ public function testNormalizeFull(): void
'supportsBomRef' => true,
]
);
$licenseExpressionNormalizer = $this->createMock(LicenseExpressionNormalizer::class);
$hashRepositoryNormalizer = $this->createMock(HashRepositoryNormalizer::class);
$licenseExpressionNormalizer = $this->createMock(Normalizers\LicenseExpressionNormalizer::class);
$hashRepositoryNormalizer = $this->createMock(Normalizers\HashRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
Expand All @@ -148,7 +146,7 @@ public function testNormalizeFull(): void
'makeForHashRepository' => $hashRepositoryNormalizer,
]
);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
Expand Down Expand Up @@ -198,7 +196,7 @@ public function testNormalizeUnsupportedLicenseExpression(): void
'supportsLicenseExpression' => false,
]
);
$licenseNormalizer = $this->createMock(DisjunctiveLicenseRepositoryNormalizer::class);
$licenseNormalizer = $this->createMock(Normalizers\DisjunctiveLicenseRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
Expand All @@ -207,7 +205,7 @@ public function testNormalizeUnsupportedLicenseExpression(): void
'makeForDisjunctiveLicenseRepository' => $licenseNormalizer,
]
);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$transformedLicenseTest = static function (DisjunctiveLicenseRepository $licenses): bool {
$licenses = $licenses->getLicenses();
Expand Down Expand Up @@ -250,7 +248,7 @@ public function testNormalizeDisjunctiveLicenses(): void
]
);
$spec = $this->createMock(SpecInterface::class);
$licenseNormalizer = $this->createMock(DisjunctiveLicenseRepositoryNormalizer::class);
$licenseNormalizer = $this->createMock(Normalizers\DisjunctiveLicenseRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
Expand All @@ -259,7 +257,7 @@ public function testNormalizeDisjunctiveLicenses(): void
'makeForDisjunctiveLicenseRepository' => $licenseNormalizer,
]
);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
Expand Down Expand Up @@ -292,7 +290,7 @@ public function testNormalizeDisjunctiveLicensesEmpty(): void
]
);
$spec = $this->createMock(SpecInterface::class);
$licenseNormalizer = $this->createMock(DisjunctiveLicenseRepositoryNormalizer::class);
$licenseNormalizer = $this->createMock(Normalizers\DisjunctiveLicenseRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
Expand All @@ -301,7 +299,7 @@ public function testNormalizeDisjunctiveLicensesEmpty(): void
'makeForDisjunctiveLicenseRepository' => $licenseNormalizer,
]
);
$normalizer = new ComponentNormalizer($factory);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
Expand All @@ -318,4 +316,90 @@ public function testNormalizeDisjunctiveLicensesEmpty(): void
$got
);
}

// region normalize ExternalReferences

public function testNormalizeExternalReferences(): void
{
$component = $this->createConfiguredMock(
Component::class,
[
'getName' => 'myName',
'getVersion' => 'some-version',
'getType' => 'FakeType',
'getExternalReferenceRepository' => $this->createConfiguredMock(ExternalReferenceRepository::class, ['count' => 1]),
]
);
$spec = $this->createMock(SpecInterface::class);
$externalReferenceRepositoryNormalizer = $this->createMock(Normalizers\ExternalReferenceRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
'getSpec' => $spec,
'getDocument' => new DOMDocument(),
'makeForExternalReferenceRepository' => $externalReferenceRepositoryNormalizer,
]
);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
->willReturn(true);
$externalReferenceRepositoryNormalizer->expects(self::once())
->method('normalize')
->with($component->getExternalReferenceRepository())
->willReturn([$factory->getDocument()->createElement('FakeExternalReference', 'dummy')]);

$actual = $normalizer->normalize($component);

self::assertStringEqualsDomNode(
'<component type="FakeType">'.
'<name>myName</name>'.
'<version>some-version</version>'.
'<externalReferences><FakeExternalReference>dummy</FakeExternalReference></externalReferences>'.
'</component>',
$actual
);
}

public function testNormalizeExternalReferencesOmitIfEmpty(): void
{
$component = $this->createConfiguredMock(
Component::class,
[
'getName' => 'myName',
'getVersion' => 'some-version',
'getType' => 'FakeType',
'getExternalReferenceRepository' => $this->createConfiguredMock(ExternalReferenceRepository::class, ['count' => 0]),
]
);
$spec = $this->createMock(SpecInterface::class);
$externalReferenceRepositoryNormalizer = $this->createMock(Normalizers\ExternalReferenceRepositoryNormalizer::class);
$factory = $this->createConfiguredMock(
NormalizerFactory::class,
[
'getSpec' => $spec,
'getDocument' => new DOMDocument(),
'makeForExternalReferenceRepository' => $externalReferenceRepositoryNormalizer,
]
);
$normalizer = new Normalizers\ComponentNormalizer($factory);

$spec->expects(self::once())->method('isSupportedComponentType')
->with('FakeType')
->willReturn(true);
$externalReferenceRepositoryNormalizer->expects(self::never())->method('normalize');

$actual = $normalizer->normalize($component);

self::assertStringEqualsDomNode(
'<component type="FakeType">'.
'<name>myName</name>'.
'<version>some-version</version>'.
'</component>',
$actual
);
}

// endregion normalize ExternalReferences
}
4 changes: 4 additions & 0 deletions tests/Core/Serialize/JSON/Normalizers/BomNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ public function testNormalizeDependenciesOmitWhenEmpty(): void
);
}

// region normalize ExternalReferences

public function testNormalizeExternalReferences(): void
{
$spec = $this->createConfiguredMock(
Expand Down Expand Up @@ -419,4 +421,6 @@ public function testNormalizeExternalReferencesOmitIfEmpty(): void
$actual
);
}

// endregion normalize ExternalReferences
}
Loading

0 comments on commit 5a1cc4a

Please sign in to comment.