diff --git a/composer.json b/composer.json index 322d464..840775d 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,12 @@ "email": "sascha.ternes@gmx.de" } ], - "version": "1.10.1", - "time": "2023-05-31", + "version": "1.11.0", + "time": "2024-02-03", "require": { - "php": "~8.1", - "phpunit/phpunit": "~10.0" + "php": "~8.2", + "phpunit/phpunit": "~11.0" }, "autoload": { diff --git a/src/Assertions.php b/src/Assertions.php index 6c2e798..fdfc584 100644 --- a/src/Assertions.php +++ b/src/Assertions.php @@ -8,17 +8,15 @@ trait Assertions { /** * Assert that the actual value is an array containing an exact number of elements that may have a defined type. - * - * @param mixed $actual */ - public static function assertArray($actual, int $count = 0, ?string $type = null, string $message = ''): void { + public static function assertArray(mixed $actual, int $count = 0, ?string $type = null, string $message = ''): void { Assert::assertIsArray($actual, $message); $message = $message ?? 'Expected array of ' . $count . ' elements.'; Assert::assertSame($count, count($actual), $message); if ($type) { if ($type === 'stdClass') { Assert::assertContainsOnlyInstancesOf($type, $actual); - } elseif (strpos($type, '\\') !== false) { + } elseif (str_contains($type, '\\')) { Assert::assertContainsOnlyInstancesOf($type, $actual); } else { Assert::assertContainsOnly($type, $actual, true); @@ -28,12 +26,8 @@ public static function assertArray($actual, int $count = 0, ?string $type = null /** * Assert that array has key and value. - * - * @param mixed $actual - * @param mixed $key - * @param mixed $value */ - public static function assertArrayKey($actual, $key, $value, string $message = ''): void { + public static function assertArrayKey(mixed $actual, mixed $key, mixed $value, string $message = ''): void { Assert::assertIsArray($actual, $message); Assert::assertArrayHasKey($key, $actual, $message); $actualValue = $actual[$key]; @@ -43,11 +37,8 @@ public static function assertArrayKey($actual, $key, $value, string $message = ' /** * Assert that a value is an integer greater than zero and a whole-number power of two. - * - * @param mixed $value */ - protected function assertIsPowerOfTwo($value): void - { + protected function assertIsPowerOfTwo(mixed $value): void { $this->assertIsInt($value); $this->assertGreaterThan(0, $value); if ($value === 1) { @@ -62,11 +53,8 @@ protected function assertIsPowerOfTwo($value): void /** * Assert that an object has a method. - * - * @param mixed $object */ - protected function assertObjectHasMethod(string $method, $object): void - { + protected function assertObjectHasMethod(string $method, mixed $object): void { Assert::assertMatchesRegularExpression('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $method, 'The method name "' . $method . '" is invalid.'); Assert::assertIsObject($object); $reflection = new \ReflectionClass($object); diff --git a/src/Functions.php b/src/Functions.php index 45aa2a8..a77ea84 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -4,10 +4,8 @@ /** * Return the class name of an object without its namespace. - * - * @param object|string $object */ -function getClass($object): string { +function getClass(object|string $object): string { $class = is_object($object) ? get_class($object) : $object; $i = strrpos($class, '\\'); return $i > 0 ? substr($class, $i + 1) : $class; diff --git a/tests/Assertions/AssertArrayKeyTest.php b/tests/Assertions/AssertArrayKeyTest.php index d4a74ec..9c07849 100644 --- a/tests/Assertions/AssertArrayKeyTest.php +++ b/tests/Assertions/AssertArrayKeyTest.php @@ -2,6 +2,7 @@ declare(strict_types = 1); namespace SATHub\PHPUnit\Tests\Assertions; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -17,45 +18,32 @@ class AssertArrayKeyTest extends TestCase private const STRING = 'value'; - /** - * @test - */ + #[Test] public function assertArrayKeySucceeds(): void { $this->assertArrayKey([self::KEY => self::INT], self::KEY, self::INT); } - /** - * @test - */ + #[Test] public function assertArrayKeyIgnoresAdditionalKeys(): void { $this->assertArrayKey([self::KEY => self::INT, 'key2' => self::STRING], self::KEY, self::INT); } - /** - * @test - */ - public function assertArrayKeyWithEmptyArray(): void - { + #[Test] + public function assertArrayKeyWithEmptyArray(): void { $this->expectException(ExpectationFailedException::class); $this->assertArrayKey([], 'key', null); } - /** - * @test - */ - public function assertArrayKeyFailsIfValueIsDifferent(): void - { + #[Test] + public function assertArrayKeyFailsIfValueIsDifferent(): void { $this->expectException(ExpectationFailedException::class); $this->assertArrayKey([self::KEY => self::INT], self::KEY, self::STRING); } - /** - * @test - */ - public function assertArrayKeyFailsIfKeyIsMissing(): void - { + #[Test] + public function assertArrayKeyFailsIfKeyIsMissing(): void { $this->expectException(ExpectationFailedException::class); $this->assertArrayKey([self::KEY => self::INT], 'key2', self::INT); diff --git a/tests/Assertions/AssertArrayTest.php b/tests/Assertions/AssertArrayTest.php index 5d4c05b..05279b0 100644 --- a/tests/Assertions/AssertArrayTest.php +++ b/tests/Assertions/AssertArrayTest.php @@ -2,6 +2,7 @@ declare(strict_types = 1); namespace SATHub\PHPUnit\Tests\Assertions; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -13,79 +14,52 @@ class AssertArrayTest extends TestCase { use Assertions; - /** - * @test - */ - public function assertArrayWithEmptyArray(): void - { + #[Test] + public function assertArrayWithEmptyArray(): void { $this->assertArray([]); } - /** - * @test - */ - public function assertArrayWithArrayOfIntegers(): void - { + #[Test] + public function assertArrayWithArrayOfIntegers(): void { $this->assertArray([735, -2673], 2, 'int'); } - /** - * @test - */ - public function assertCountParameterIsRequired(): void - { + #[Test] + public function assertCountParameterIsRequired(): void { $this->expectException(ExpectationFailedException::class); $this->assertArray([735, -2673]); } - /** - * @test - */ - public function assertArrayWithArrayOfStrings(): void - { + #[Test] + public function assertArrayWithArrayOfStrings(): void { $this->assertArray(['735', '', __CLASS__], 3, 'string'); } - /** - * @test - */ - public function assertArrayWithArrayOfObjects(): void - { + #[Test] + public function assertArrayWithArrayOfObjects(): void { $this->assertArray([new \stdClass(), $this, new Mock()], 3, 'object'); } - /** - * @test - */ - public function assertArrayWithArrayOfClassInstances(): void - { + #[Test] + public function assertArrayWithArrayOfClassInstances(): void { $this->assertArray([new Mock(), new Mock()], 2, Mock::class); } - /** - * @test - */ - public function assertArrayWithArrayOfMixedClasses(): void - { + #[Test] + public function assertArrayWithArrayOfMixedClasses(): void { $this->assertArray([new Mock(), new \stdClass()], 2); } - /** - * @test - */ - public function assertArrayWithArrayOfMixedClassesFails(): void - { + #[Test] + public function assertArrayWithArrayOfMixedClassesFails(): void { $this->expectException(ExpectationFailedException::class); $this->assertArray([new Mock(), new \stdClass()], 2, Mock::class); } - /** - * @test - */ - public function assertArrayWithArrayOfStdClass(): void - { + #[Test] + public function assertArrayWithArrayOfStdClass(): void { $this->assertArray([new \stdClass()], 1, \stdClass::class); $this->assertArray([new \stdClass()], 1, 'stdClass'); $this->assertArray([new \stdClass()], 1, '\\stdClass'); diff --git a/tests/Assertions/AssertIsPowerOfTwoTest.php b/tests/Assertions/AssertIsPowerOfTwoTest.php index 7ff7a24..a75613b 100644 --- a/tests/Assertions/AssertIsPowerOfTwoTest.php +++ b/tests/Assertions/AssertIsPowerOfTwoTest.php @@ -2,6 +2,7 @@ declare(strict_types = 1); namespace SATHub\PHPUnit\Tests\Assertions; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -11,11 +12,8 @@ class AssertIsPowerOfTwoTest extends TestCase { use Assertions; - /** - * @test - */ - public function assertIsPowerOfTwoSucceeds(): void - { + #[Test] + public function assertIsPowerOfTwoSucceeds(): void { $this->assertIsPowerOfTwo(1); $this->assertIsPowerOfTwo(2); $this->assertIsPowerOfTwo(8); @@ -23,41 +21,29 @@ public function assertIsPowerOfTwoSucceeds(): void $this->assertIsPowerOfTwo(65536 * 65536 * 65536); } - /** - * @test - */ - public function assertIsPowerOfTwoForZero(): void - { + #[Test] + public function assertIsPowerOfTwoForZero(): void { $this->expectException(ExpectationFailedException::class); $this->assertIsPowerOfTwo(0); } - /** - * @test - */ - public function assertIsPowerOfTwoForNegativeValue(): void - { + #[Test] + public function assertIsPowerOfTwoForNegativeValue(): void { $this->expectException(ExpectationFailedException::class); $this->assertIsPowerOfTwo(-4); } - /** - * @test - */ - public function assertIsPowerOfTwoFailsForFloat(): void - { + #[Test] + public function assertIsPowerOfTwoFailsForFloat(): void { $this->expectException(ExpectationFailedException::class); $this->assertIsPowerOfTwo(4.0); } - /** - * @test - */ - public function assertIsPowerOfTwoFailsForString(): void - { + #[Test] + public function assertIsPowerOfTwoFailsForString(): void { $this->expectException(ExpectationFailedException::class); $this->assertIsPowerOfTwo('4'); diff --git a/tests/Assertions/AssertObjectHasMethodTest.php b/tests/Assertions/AssertObjectHasMethodTest.php index 9ba2e19..2895322 100644 --- a/tests/Assertions/AssertObjectHasMethodTest.php +++ b/tests/Assertions/AssertObjectHasMethodTest.php @@ -2,6 +2,7 @@ declare(strict_types = 1); namespace SATHub\PHPUnit\Tests\Assertions; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -11,39 +12,27 @@ class AssertObjectHasMethodTest extends TestCase { use Assertions; - /** - * @test - */ - public function assertObjectHasMethodSucceeds(): void - { + #[Test] + public function assertObjectHasMethodSucceeds(): void { $this->assertObjectHasMethod(__FUNCTION__, $this); } - /** - * @test - */ - public function assertObjectHasMethodFailsIfMethodDoesNotExist(): void - { + #[Test] + public function assertObjectHasMethodFailsIfMethodDoesNotExist(): void { $this->expectException(ExpectationFailedException::class); $this->assertObjectHasMethod('nonExistingMethod', $this); } - /** - * @test - */ - public function assertObjectHasMethodFailsForNonObject(): void - { + #[Test] + public function assertObjectHasMethodFailsForNonObject(): void { $this->expectException(ExpectationFailedException::class); $this->assertObjectHasMethod('toString', 'string'); } - /** - * @test - */ - public function assertObjectHasMethodFailsForInvalidMethodName(): void - { + #[Test] + public function assertObjectHasMethodFailsForInvalidMethodName(): void { $this->expectException(ExpectationFailedException::class); $this->assertObjectHasMethod('I-am-!nvalid', $this); diff --git a/tests/Assertions/PassTest.php b/tests/Assertions/PassTest.php index e4c7e0f..0d438eb 100644 --- a/tests/Assertions/PassTest.php +++ b/tests/Assertions/PassTest.php @@ -12,8 +12,7 @@ class PassTest extends TestCase use Assertions; #[Test] - public function passSucceeds(): void - { + public function passSucceeds(): void { $this->pass(); } } diff --git a/tests/Functions/GetClassTest.php b/tests/Functions/GetClassTest.php index 290d216..ba5e497 100644 --- a/tests/Functions/GetClassTest.php +++ b/tests/Functions/GetClassTest.php @@ -2,20 +2,18 @@ declare(strict_types = 1); namespace SATHub\PHPUnit\Tests\Functions; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use function SATHub\PHPUnit\getClass; class GetClassTest extends TestCase { - protected function setUp(): void - { + protected function setUp(): void { require_once __DIR__ . '/../../src/Functions.php'; } - /** - * @test - */ + #[Test] public function getClassReturnsCorrectClassName(): void { $this->assertSame('GetClassTest', getClass($this)); }