Skip to content

Commit

Permalink
Add strict types and attribute #[CoversClass] to tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
renfordt committed Jan 8, 2025
1 parent 6aa5c1b commit 31a4001
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 52 deletions.
18 changes: 11 additions & 7 deletions tests/AvatarTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Renfordt\Larvatar\Avatar;
Expand All @@ -8,6 +11,7 @@
/**
* Unit tests for the Avatar class.
*/
#[CoversClass(Avatar::class)]
class AvatarTest extends TestCase
{
public static function fontDataProvider(): array
Expand Down Expand Up @@ -89,7 +93,7 @@ public static function sizeProvider(): array
* Test setting and getting font family and path.
*/
#[DataProvider('fontDataProvider')]
public function testFontMethods($font, $path)
public function testFontMethods(string $font, string $path): void
{
$mock = $this->getMockForAbstractClass(Avatar::class);
$mock->setFont($font, $path);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/GravatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 31 additions & 28 deletions tests/IdenticonTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Renfordt\Larvatar\Identicon;
use Renfordt\Larvatar\Name;

#[CoversClass(Identicon::class)]
class IdenticonTest extends TestCase
{
/**
* Tests the getSymmetryMatrix method to ensure it returns the expected symmetry matrix.
*/
public function testGetSymmetryMatrixReturnedExpectedSymmetryMatrix()
public function testGetSymmetryMatrixReturnedExpectedSymmetryMatrix(): void
{
// Mocking the Name class
$name = Name::make('Test Name');
Expand Down Expand Up @@ -56,7 +59,7 @@ protected function invokeMethod(&$object, $methodName, array $parameters = []):
/**
* Tests the convertStrToBool method with the hexadecimal value '0'.
*/
public function testConvertStrToBoolWithZero()
public function testConvertStrToBoolWithZero(): void
{
// Mocking the Name class
$nameMock = $this->createMock(Name::class);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -299,7 +302,7 @@ public function testSetPixelsWithValidValue()
*
* @expectedException TypeError
*/
public function testSetPixelsWithInvalidArgument()
public function testSetPixelsWithInvalidArgument(): void
{
$this->expectException(TypeError::class);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions tests/InitialsAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +11,7 @@
use SVG\Nodes\Shapes\SVGPolygon;
use SVG\Nodes\Shapes\SVGRect;

#[CoversClass(InitialsAvatar::class)]
final class InitialsAvatarTest extends TestCase
{
/**
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -325,7 +327,6 @@ public function testGetTextLightnessAfterSettingTooLowValue(): void

/**
* Tests if the set offset returns correct value
* @return void
*/
public function testGetOffsetIsSetCorrectly(): void
{
Expand All @@ -337,7 +338,6 @@ public function testGetOffsetIsSetCorrectly(): void

/**
* Tests if the default value should be zero
* @return void
*/
public function testGetOffsetReturnsDefaultValue(): void
{
Expand Down
Loading

0 comments on commit 31a4001

Please sign in to comment.