From 31a40016c0d7e5dac61f16ead5dae528fc26da3c Mon Sep 17 00:00:00 2001 From: renfordt Date: Wed, 8 Jan 2025 20:02:11 +0100 Subject: [PATCH] Add strict types and attribute #[CoversClass] to tests This update enforces strict typing across all test files and introduces the #[CoversClass] attribute to improve test coverage annotations. Additionally, minor improvements such as explicit return type hints were added for better code clarity. --- tests/AvatarTest.php | 18 +++++---- tests/GravatarTest.php | 2 + tests/IdenticonTest.php | 59 ++++++++++++++++-------------- tests/InitialsAvatarTest.php | 6 +-- tests/LarvatarTest.php | 6 ++- tests/NameTest.php | 20 ++++++---- tests/Traits/LarvatarTraitTest.php | 14 ++++--- 7 files changed, 73 insertions(+), 52 deletions(-) diff --git a/tests/AvatarTest.php b/tests/AvatarTest.php index 1bc52e8..3424faa 100644 --- a/tests/AvatarTest.php +++ b/tests/AvatarTest.php @@ -1,5 +1,8 @@ getMockForAbstractClass(Avatar::class); $mock->setFont($font, $path); @@ -102,7 +106,7 @@ public function testFontMethods($font, $path) * */ #[DataProvider('backgroundLightnessProvider')] - public function testBackgroundLightnessMethods($input, $expected) + public function testBackgroundLightnessMethods(float|int $input, float|int $expected): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setBackgroundLightness($input); @@ -114,7 +118,7 @@ public function testBackgroundLightnessMethods($input, $expected) * */ #[DataProvider('textLightnessProvider')] - public function testTextLightnessMethods($input, $expected) + public function testTextLightnessMethods(int|float $input, int|float $expected): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setTextLightness($input); @@ -126,7 +130,7 @@ public function testTextLightnessMethods($input, $expected) * */ #[DataProvider('nameProvider')] - public function testNameMethods($name, $expected) + public function testNameMethods(\Renfordt\Larvatar\Name|string $name, string $expected): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setName($name); @@ -138,7 +142,7 @@ public function testNameMethods($name, $expected) * */ #[DataProvider('fontSizeProvider')] - public function testFontSizeMethods($input) + public function testFontSizeMethods(int $input): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setFontSize($input); @@ -150,7 +154,7 @@ public function testFontSizeMethods($input) * */ #[DataProvider('fontWeightProvider')] - public function testFontWeightMethods($input) + public function testFontWeightMethods(string $input): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setFontWeight($input); @@ -162,7 +166,7 @@ public function testFontWeightMethods($input) * */ #[DataProvider('sizeProvider')] - public function testSizeMethods($input) + public function testSizeMethods(int $input): void { $mock = $this->getMockForAbstractClass(Avatar::class); $mock->setSize($input); diff --git a/tests/GravatarTest.php b/tests/GravatarTest.php index fdd69b6..43a7a57 100644 --- a/tests/GravatarTest.php +++ b/tests/GravatarTest.php @@ -2,10 +2,12 @@ declare(strict_types=1); +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Renfordt\Larvatar\Enum\LarvatarTypes; use Renfordt\Larvatar\Gravatar; +#[CoversClass(Gravatar::class)] final class GravatarTest extends TestCase { public function testGravatarLink(): void diff --git a/tests/IdenticonTest.php b/tests/IdenticonTest.php index 3176a02..fe412ef 100644 --- a/tests/IdenticonTest.php +++ b/tests/IdenticonTest.php @@ -1,16 +1,19 @@ createMock(Name::class); @@ -72,7 +75,7 @@ public function testConvertStrToBoolWithZero() /** * Tests the convertStrToBool method with the hexadecimal value 'F'. */ - public function testConvertStrToBoolWithF() + public function testConvertStrToBoolWithF(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -88,7 +91,7 @@ public function testConvertStrToBoolWithF() /** * Tests the convertStrToBool method with a mid-range hexadecimal value. */ - public function testConvertStrToBoolWithEight() + public function testConvertStrToBoolWithEight(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -104,7 +107,7 @@ public function testConvertStrToBoolWithEight() /** * Tests the convertStrToBool method with a low-range hexadecimal value. */ - public function testConvertStrToBoolWithFour() + public function testConvertStrToBoolWithFour(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -120,7 +123,7 @@ public function testConvertStrToBoolWithFour() /** * Tests the getSymmetryMatrix method to handle single pixel cases. */ - public function testGetSymmetryMatrixHandlesSinglePixel() + public function testGetSymmetryMatrixHandlesSinglePixel(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -146,7 +149,7 @@ public function testGetSymmetryMatrixHandlesSinglePixel() /** * Tests the getSymmetryMatrix method to handle even pixel counts. */ - public function testGetSymmetryMatrixHandlesEvenPixelCount() + public function testGetSymmetryMatrixHandlesEvenPixelCount(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -174,7 +177,7 @@ public function testGetSymmetryMatrixHandlesEvenPixelCount() /** * Tests the getSVG method to ensure it returns a valid SVG representation with symmetry. */ - public function testGetSVGWithSymmetry() + public function testGetSVGWithSymmetry(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -191,7 +194,7 @@ public function testGetSVGWithSymmetry() /** * Tests the getBase64 method to ensure it returns a base64 encoded SVG. */ - public function testGetBase64ReturnsBase64EncodedSVG() + public function testGetBase64ReturnsBase64EncodedSVG(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -210,7 +213,7 @@ public function testGetBase64ReturnsBase64EncodedSVG() /** * Tests the getBase64 method to ensure it contains a valid SVG representation. */ - public function testGetBase64ContainsValidSVG() + public function testGetBase64ContainsValidSVG(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -231,7 +234,7 @@ public function testGetBase64ContainsValidSVG() /** * Tests the getSVG method to ensure it returns a valid SVG representation without symmetry. */ - public function testGetSVGWithoutSymmetry() + public function testGetSVGWithoutSymmetry(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -248,7 +251,7 @@ public function testGetSVGWithoutSymmetry() /** * Tests the getSVG method to ensure the SVG output contains proper header. */ - public function testGetSVGHeader() + public function testGetSVGHeader(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -265,7 +268,7 @@ public function testGetSVGHeader() * Tests if the Identicon object is created successfully with a valid Name object. * Also tests the static make method of Identicon class. */ - public function testConstructWithValidName() + public function testConstructWithValidName(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -281,7 +284,7 @@ public function testConstructWithValidName() /** * Tests the setPixels method if it correctly sets the number of pixels. */ - public function testSetPixelsWithValidValue() + public function testSetPixelsWithValidValue(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -299,7 +302,7 @@ public function testSetPixelsWithValidValue() * * @expectedException TypeError */ - public function testSetPixelsWithInvalidArgument() + public function testSetPixelsWithInvalidArgument(): void { $this->expectException(TypeError::class); @@ -314,7 +317,7 @@ public function testSetPixelsWithInvalidArgument() /** * Tests the make method with a valid Name object. */ - public function testMakeWithValidName() + public function testMakeWithValidName(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -331,30 +334,30 @@ public function testMakeWithValidName() * Tests the make method with an invalid argument. * */ - public function testMakeWithInvalidArgument() + public function testMakeWithInvalidArgument(): void { $this->expectException(TypeError::class); // Creating Identicon with invalid argument using the make method - $identicon = Identicon::make('invalid_argument'); + Identicon::make('invalid_argument'); } /** * Tests if the Identicon object throws an error when Name object is not provided. * */ - public function testConstructWithInvalidArgument() + public function testConstructWithInvalidArgument(): void { $this->expectException(TypeError::class); // Creating Identicon with invalid argument - $identicon = new Identicon('invalid_argument'); + new Identicon('invalid_argument'); } /** * Tests the setSymmetry method if it correctly sets the symmetry to true. */ - public function testSetSymmetryWithTrueValue() + public function testSetSymmetryWithTrueValue(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -375,7 +378,7 @@ public function testSetSymmetryWithTrueValue() /** * Tests the setSymmetry method if it correctly sets the symmetry to false. */ - public function testSetSymmetryWithFalseValue() + public function testSetSymmetryWithFalseValue(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -397,7 +400,7 @@ public function testSetSymmetryWithFalseValue() * Tests the setSymmetry method with an invalid argument. * */ - public function testSetSymmetryWithInvalidArgument() + public function testSetSymmetryWithInvalidArgument(): void { $this->expectException(TypeError::class); @@ -412,7 +415,7 @@ public function testSetSymmetryWithInvalidArgument() /** * Tests the getHTML method without base64 encoding. */ - public function testGetHTMLWithoutBase64() + public function testGetHTMLWithoutBase64(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -427,7 +430,7 @@ public function testGetHTMLWithoutBase64() /** * Tests the getHTML method with base64 encoding. */ - public function testGetHTMLWithBase64() + public function testGetHTMLWithBase64(): void { // Mocking the Name class $nameMock = $this->createMock(Name::class); @@ -442,7 +445,7 @@ public function testGetHTMLWithBase64() /** * Tests the generateSymmetricMatrix method to ensure it returns a symmetric matrix. */ - public function testGenerateSymmetricMatrix() + public function testGenerateSymmetricMatrix(): void { // Mocking the Name class $name = Name::make('Test Name'); @@ -465,7 +468,7 @@ public function testGenerateSymmetricMatrix() /** * Tests the generateSymmetricMatrix method with different pixel values. */ - public function testGenerateSymmetricMatrixWithDifferentPixelValues() + public function testGenerateSymmetricMatrixWithDifferentPixelValues(): void { // Mocking the Name class $name = Name::make('Test Name'); @@ -489,7 +492,7 @@ public function testGenerateSymmetricMatrixWithDifferentPixelValues() /** * Tests the generateSymmetricMatrix method with variation in Name hashes. */ - public function testGenerateSymmetricMatrixWithNameHashVariation() + public function testGenerateSymmetricMatrixWithNameHashVariation(): void { $hashes = [ 'aabbccddeeff001122334455', diff --git a/tests/InitialsAvatarTest.php b/tests/InitialsAvatarTest.php index b6b75e5..b388e65 100644 --- a/tests/InitialsAvatarTest.php +++ b/tests/InitialsAvatarTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Renfordt\Colors\HexColor; use Renfordt\Larvatar\Enum\FormTypes; @@ -10,6 +11,7 @@ use SVG\Nodes\Shapes\SVGPolygon; use SVG\Nodes\Shapes\SVGRect; +#[CoversClass(InitialsAvatar::class)] final class InitialsAvatarTest extends TestCase { /** @@ -125,7 +127,7 @@ public function testGenerateWithBase64(): void { $name = Name::make('Test Name'); $initialsAvatar = InitialsAvatar::make($name); - $svg = $initialsAvatar->generate(); + $svg = $initialsAvatar->generate()->__toString(); $base64 = $initialsAvatar->getBase64(); $this->assertEquals( @@ -325,7 +327,6 @@ public function testGetTextLightnessAfterSettingTooLowValue(): void /** * Tests if the set offset returns correct value - * @return void */ public function testGetOffsetIsSetCorrectly(): void { @@ -337,7 +338,6 @@ public function testGetOffsetIsSetCorrectly(): void /** * Tests if the default value should be zero - * @return void */ public function testGetOffsetReturnsDefaultValue(): void { diff --git a/tests/LarvatarTest.php b/tests/LarvatarTest.php index a84f56d..1718d64 100644 --- a/tests/LarvatarTest.php +++ b/tests/LarvatarTest.php @@ -1,9 +1,13 @@ expectExceptionMessage('must be of type Renfordt\Larvatar\Enum\LarvatarTypes'); - $larvatar = new Larvatar(700, 'Test Name', 'test@example.com'); + new Larvatar(700, 'Test Name', 'test@example.com'); } public function testSetFont(): void diff --git a/tests/NameTest.php b/tests/NameTest.php index b84b0f4..3d13b97 100644 --- a/tests/NameTest.php +++ b/tests/NameTest.php @@ -1,9 +1,13 @@ assertEquals($expectedHash, $name->getHash()); } #[DataProvider('splitNamesProvider')] - public function testGetSplitNames($nameInput, $expectedSplitNames) + public function testGetSplitNames(string $nameInput, array $expectedSplitNames): void { $name = new Name($nameInput); $this->assertEquals($expectedSplitNames, $name->getSplitNames()); } #[DataProvider('hexColorProvider')] - public function testGetHexColor($nameInput, $offset, $expectedHexColor) + public function testGetHexColor(string $nameInput, int $offset, string $expectedHexColor): void { $name = new Name($nameInput); $this->assertEquals($expectedHexColor, $name->getHexColor($offset)); } #[DataProvider('nameProvider')] - public function testGetName($nameInput, $expectedName) + public function testGetName(string $nameInput, string $expectedName): void { $name = new Name($nameInput); $this->assertEquals($expectedName, $name->getName()); } #[DataProvider('initialsProvider')] - public function testGetInitials($nameInput, $expectedInitials) + public function testGetInitials(string $nameInput, string $expectedInitials): void { $name = new Name($nameInput); $this->assertEquals($expectedInitials, $name->getInitials()); } #[DataProvider('initialsProvider')] - public function testMakeMethodInitials($nameInput, $expectedInitials) + public function testMakeMethodInitials(string $nameInput, string $expectedInitials): void { $name = Name::make($nameInput); $this->assertEquals($expectedInitials, $name->getInitials()); } #[DataProvider('initialsProvider')] - public function testCreateMethodInitials($nameInput, $expectedInitials) + public function testCreateMethodInitials(string $nameInput, string $expectedInitials): void { $name = Name::create($nameInput); $this->assertEquals($expectedInitials, $name->getInitials()); } -} \ No newline at end of file +} diff --git a/tests/Traits/LarvatarTraitTest.php b/tests/Traits/LarvatarTraitTest.php index 6790540..39b6525 100644 --- a/tests/Traits/LarvatarTraitTest.php +++ b/tests/Traits/LarvatarTraitTest.php @@ -1,12 +1,16 @@ name = $name; $this->email = $email; $this->type = $type; @@ -66,7 +70,7 @@ public function testGetAvatar( $this->assertSame($expectedData, $result); } - public function getAvatar(int $size = 100, bool $encoding = true) + public function getAvatar(int $size = 100, bool $encoding = true): string { $larvatar = $this->getLarvatar($this->name, $this->email, $this->type); $larvatar->setSize($size); @@ -74,14 +78,14 @@ public function getAvatar(int $size = 100, bool $encoding = true) return $larvatar->getImageHTML($encoding); } - public function testGetAvatarWithDefaultParameters() + public function testGetAvatarWithDefaultParameters(): void { $result = $this->getAvatar(); $this->assertNotEmpty($result); } #[DataProvider('dataProviderForDifferentAvatarTypes')] - public function testGetAvatarWithDifferentAvatarTypes(LarvatarTypes $type) + public function testGetAvatarWithDifferentAvatarTypes(LarvatarTypes $type): void { $this->type = $type; $result = $this->getAvatar(100, false); @@ -89,7 +93,7 @@ public function testGetAvatarWithDifferentAvatarTypes(LarvatarTypes $type) } #[DataProvider('dataProviderForEncodingVariations')] - public function testGetAvatarWithEncodingVariations(bool $encoding) + public function testGetAvatarWithEncodingVariations(bool $encoding): void { $result = $this->getAvatar(100, $encoding); $this->assertNotEmpty($result);