Skip to content

Commit

Permalink
Migrate to PHPUnit 11, clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
sat-hub committed Feb 3, 2024
1 parent a411809 commit f337e8a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 142 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"email": "[email protected]"
}
],
"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": {
Expand Down
22 changes: 5 additions & 17 deletions src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 9 additions & 21 deletions tests/Assertions/AssertArrayKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
64 changes: 19 additions & 45 deletions tests/Assertions/AssertArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
Expand Down
36 changes: 11 additions & 25 deletions tests/Assertions/AssertIsPowerOfTwoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,53 +12,38 @@ 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);
$this->assertIsPowerOfTwo(8192);
$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');
Expand Down
29 changes: 9 additions & 20 deletions tests/Assertions/AssertObjectHasMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions tests/Assertions/PassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class PassTest extends TestCase
use Assertions;

#[Test]
public function passSucceeds(): void
{
public function passSucceeds(): void {
$this->pass();
}
}
Loading

0 comments on commit f337e8a

Please sign in to comment.