From 77c6419c805404a473a1ad4911da81f57f8f43e2 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 19 May 2023 10:56:23 -0400 Subject: [PATCH] Rector updates for tests --- .../AnimeClient/API/APIRequestBuilderTest.php | 12 ++++---- .../Transformer/AnimeListTransformerTest.php | 8 ++--- .../Transformer/AnimeTransformerTest.php | 2 +- .../Transformer/CharacterTransformerTest.php | 2 +- .../Transformer/HistoryTransformerTest.php | 2 +- .../Transformer/PersonTransformerTest.php | 2 +- tests/AnimeClient/AnimeClientTestCase.php | 12 ++++---- tests/AnimeClient/DispatcherTest.php | 5 +--- tests/AnimeClient/FormGeneratorTest.php | 2 +- tests/AnimeClient/Helper/MenuHelperTest.php | 1 + tests/AnimeClient/MenuGeneratorTest.php | 1 + tests/AnimeClient/RequirementsTest.php | 2 +- tests/AnimeClient/TestSessionHandler.php | 4 +-- tests/AnimeClient/UtilTest.php | 11 ++----- tests/AnimeClient/mocks.php | 6 ++-- tests/Ion/Di/ContainerTest.php | 30 +++++-------------- tests/Ion/EnumTest.php | 6 +--- .../Exception/DoubleRenderExceptionTest.php | 2 +- tests/Ion/IonTestCase.php | 12 ++++---- tests/Ion/JsonTest.php | 2 +- tests/Ion/TestSessionHandler.php | 4 +-- .../Transformer/AbstractTransformerTest.php | 16 ++-------- tests/Ion/di.php | 6 ++-- tests/Ion/mocks.php | 10 +++---- 24 files changed, 60 insertions(+), 100 deletions(-) diff --git a/tests/AnimeClient/API/APIRequestBuilderTest.php b/tests/AnimeClient/API/APIRequestBuilderTest.php index e1e7ab3c..5760765e 100644 --- a/tests/AnimeClient/API/APIRequestBuilderTest.php +++ b/tests/AnimeClient/API/APIRequestBuilderTest.php @@ -40,7 +40,7 @@ final class APIRequestBuilderTest extends TestCase $this->builder->setLogger(new NullLogger()); } - public function testGzipRequest(): void + public function testGzipRequest(): never { $this->markTestSkipped('Need new test API'); @@ -51,7 +51,7 @@ final class APIRequestBuilderTest extends TestCase $this->assertTrue($body['gzipped']); } - public function testInvalidRequestMethod(): void + public function testInvalidRequestMethod(): never { $this->markTestSkipped('Need new test API'); @@ -60,7 +60,7 @@ final class APIRequestBuilderTest extends TestCase ->getFullRequest(); } - public function testRequestWithBasicAuth(): void + public function testRequestWithBasicAuth(): never { $this->markTestSkipped('Need new test API'); @@ -74,7 +74,7 @@ final class APIRequestBuilderTest extends TestCase $this->assertSame('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']); } - public function testRequestWithQueryString(): void + public function testRequestWithQueryString(): never { $this->markTestSkipped('Need new test API'); @@ -104,7 +104,7 @@ final class APIRequestBuilderTest extends TestCase $this->assertSame($expected, $body['args']); } - public function testFormValueRequest(): void + public function testFormValueRequest(): never { $this->markTestSkipped('Need new test API'); @@ -123,7 +123,7 @@ final class APIRequestBuilderTest extends TestCase $this->assertSame($formValues, $body['form']); } - public function testFullUrlRequest(): void + public function testFullUrlRequest(): never { $this->markTestSkipped('Need new test API'); diff --git a/tests/AnimeClient/API/Kitsu/Transformer/AnimeListTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/AnimeListTransformerTest.php index 4b5e45e1..6dc2b403 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/AnimeListTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/AnimeListTransformerTest.php @@ -38,7 +38,7 @@ final class AnimeListTransformerTest extends AnimeClientTestCase $this->transformer = new AnimeListTransformer(); } - public function testTransform(): void + public function testTransform(): never { $this->markTestSkipped('Old test data'); @@ -50,7 +50,7 @@ final class AnimeListTransformerTest extends AnimeClientTestCase { return [[ 'input' => [ - 'id' => 14047981, + 'id' => 14_047_981, 'watching_status' => 'current', 'user_rating' => 8, 'episodes_watched' => 38, @@ -60,7 +60,7 @@ final class AnimeListTransformerTest extends AnimeClientTestCase ], ], [ 'input' => [ - 'id' => 14047981, + 'id' => 14_047_981, 'mal_id' => '12345', 'watching_status' => 'current', 'user_rating' => 8, @@ -73,7 +73,7 @@ final class AnimeListTransformerTest extends AnimeClientTestCase ], ], [ 'input' => [ - 'id' => 14047983, + 'id' => 14_047_983, 'mal_id' => '12347', 'watching_status' => 'current', 'user_rating' => 0, diff --git a/tests/AnimeClient/API/Kitsu/Transformer/AnimeTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/AnimeTransformerTest.php index ace7152f..559ce323 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/AnimeTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/AnimeTransformerTest.php @@ -37,7 +37,7 @@ final class AnimeTransformerTest extends AnimeClientTestCase $this->transformer = new AnimeTransformer(); } - public function testTransform() + public function testTransform(): never { $this->markTestSkipped('May fail on CI'); $actual = $this->transformer->transform($this->beforeTransform); diff --git a/tests/AnimeClient/API/Kitsu/Transformer/CharacterTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/CharacterTransformerTest.php index 80205bf3..592f07af 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/CharacterTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/CharacterTransformerTest.php @@ -35,7 +35,7 @@ final class CharacterTransformerTest extends AnimeClientTestCase $this->beforeTransform = $raw; } - public function testTransform(): void + public function testTransform(): never { $this->markTestSkipped('Fails on CI'); $actual = (new CharacterTransformer())->transform($this->beforeTransform); diff --git a/tests/AnimeClient/API/Kitsu/Transformer/HistoryTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/HistoryTransformerTest.php index 8240ec50..99aa4118 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/HistoryTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/HistoryTransformerTest.php @@ -35,7 +35,7 @@ final class HistoryTransformerTest extends AnimeClientTestCase $this->beforeTransform = $raw; } - public function testAnimeTransform(): void + public function testAnimeTransform(): never { $this->markTestSkipped('Old test data'); diff --git a/tests/AnimeClient/API/Kitsu/Transformer/PersonTransformerTest.php b/tests/AnimeClient/API/Kitsu/Transformer/PersonTransformerTest.php index b1d88ea0..77811752 100644 --- a/tests/AnimeClient/API/Kitsu/Transformer/PersonTransformerTest.php +++ b/tests/AnimeClient/API/Kitsu/Transformer/PersonTransformerTest.php @@ -35,7 +35,7 @@ final class PersonTransformerTest extends AnimeClientTestCase $this->beforeTransform = $raw; } - public function testTransform(): void + public function testTransform(): never { $this->markTestSkipped('Fails on CI'); $actual = (new PersonTransformer())->transform($this->beforeTransform); diff --git a/tests/AnimeClient/AnimeClientTestCase.php b/tests/AnimeClient/AnimeClientTestCase.php index 6a835520..4e5224c0 100644 --- a/tests/AnimeClient/AnimeClientTestCase.php +++ b/tests/AnimeClient/AnimeClientTestCase.php @@ -40,10 +40,10 @@ class AnimeClientTestCase extends TestCase use MatchesSnapshots; // Test directory constants - public const ROOT_DIR = AC_TEST_ROOT_DIR; - public const SRC_DIR = SRC_DIR; - public const TEST_DATA_DIR = __DIR__ . '/test_data'; - public const TEST_VIEW_DIR = __DIR__ . '/test_views'; + final public const ROOT_DIR = AC_TEST_ROOT_DIR; + final public const SRC_DIR = SRC_DIR; + final public const TEST_DATA_DIR = __DIR__ . '/test_data'; + final public const TEST_VIEW_DIR = __DIR__ . '/test_views'; protected ContainerInterface $container; @@ -97,7 +97,7 @@ class AnimeClientTestCase extends TestCase $container = $di($config_array); // Use mock session handler - $container->set('session-handler', static function () { + $container->set('session-handler', static function (): \Aviat\AnimeClient\Tests\TestSessionHandler { $session_handler = new TestSessionHandler(); session_set_save_handler($session_handler, TRUE); @@ -123,7 +123,7 @@ class AnimeClientTestCase extends TestCase ]; $request = call_user_func_array( - [ServerRequestFactory::class, 'fromGlobals'], + ServerRequestFactory::fromGlobals(...), array_values(array_merge($default, $supers)), ); $this->container->setInstance('request', $request); diff --git a/tests/AnimeClient/DispatcherTest.php b/tests/AnimeClient/DispatcherTest.php index d3a02428..13e06b73 100644 --- a/tests/AnimeClient/DispatcherTest.php +++ b/tests/AnimeClient/DispatcherTest.php @@ -141,11 +141,8 @@ final class DispatcherTest extends AnimeClientTestCase return $data; } - /** - * @param mixed $config - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataRoute')] - public function testRoute($config, mixed $controller, mixed $host, mixed $uri): void + public function testRoute(mixed $config, mixed $controller, mixed $host, mixed $uri): void { $this->doSetUp($config, $uri, $host); diff --git a/tests/AnimeClient/FormGeneratorTest.php b/tests/AnimeClient/FormGeneratorTest.php index d8cac34f..4a89b4a8 100644 --- a/tests/AnimeClient/FormGeneratorTest.php +++ b/tests/AnimeClient/FormGeneratorTest.php @@ -251,7 +251,7 @@ final class FormGeneratorTest extends AnimeClientTestCase { $generator = FormGenerator::new($this->container); - foreach (SETTINGS_MAP as $section => $fields) + foreach (SETTINGS_MAP as $fields) { foreach ($fields as $name => $config) { diff --git a/tests/AnimeClient/Helper/MenuHelperTest.php b/tests/AnimeClient/Helper/MenuHelperTest.php index 41b4ed4f..8c14d01a 100644 --- a/tests/AnimeClient/Helper/MenuHelperTest.php +++ b/tests/AnimeClient/Helper/MenuHelperTest.php @@ -64,6 +64,7 @@ final class MenuHelperTest extends AnimeClientTestCase // Set config for tests $config = $this->container->get('config'); $config->set('menus', $menus); + $this->container->setInstance('config', $config); foreach ($menus as $case => $config) diff --git a/tests/AnimeClient/MenuGeneratorTest.php b/tests/AnimeClient/MenuGeneratorTest.php index 34c45454..84ae825a 100644 --- a/tests/AnimeClient/MenuGeneratorTest.php +++ b/tests/AnimeClient/MenuGeneratorTest.php @@ -83,6 +83,7 @@ final class MenuGeneratorTest extends AnimeClientTestCase ]; $config = $this->container->get('config'); $config->set('menus', $menus); + $this->container->setInstance('config', $config); $expected = ''; diff --git a/tests/AnimeClient/RequirementsTest.php b/tests/AnimeClient/RequirementsTest.php index 9908bd5a..e813439b 100644 --- a/tests/AnimeClient/RequirementsTest.php +++ b/tests/AnimeClient/RequirementsTest.php @@ -23,7 +23,7 @@ final class RequirementsTest extends AnimeClientTestCase { public function testPHPVersion(): void { - $this->assertTrue(version_compare(PHP_VERSION, '8', 'ge')); + $this->assertTrue(PHP_VERSION_ID >= 8); } public function testHasPDO(): void diff --git a/tests/AnimeClient/TestSessionHandler.php b/tests/AnimeClient/TestSessionHandler.php index 8c834ff6..2a7d2acd 100644 --- a/tests/AnimeClient/TestSessionHandler.php +++ b/tests/AnimeClient/TestSessionHandler.php @@ -55,13 +55,13 @@ class TestSessionHandler implements SessionHandlerInterface public function read($id) { - return json_decode(@file_get_contents("{$this->savePath}/{$id}"), TRUE); + return json_decode(@file_get_contents("{$this->savePath}/{$id}"), TRUE, 512, JSON_THROW_ON_ERROR); } public function write($id, $data) { $file = "{$this->savePath}/{$id}"; - file_put_contents($file, json_encode($data)); + file_put_contents($file, json_encode($data, JSON_THROW_ON_ERROR)); return TRUE; } diff --git a/tests/AnimeClient/UtilTest.php b/tests/AnimeClient/UtilTest.php index 8b4c1658..26010fdb 100644 --- a/tests/AnimeClient/UtilTest.php +++ b/tests/AnimeClient/UtilTest.php @@ -69,11 +69,8 @@ final class UtilTest extends AnimeClientTestCase ]; } - /** - * @param mixed $uri - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataIsViewPage')] - public function testIsViewPage($uri, mixed $expected) + public function testIsViewPage(mixed $uri, mixed $expected) { $this->setSuperGlobals([ '_SERVER' => [ @@ -83,12 +80,8 @@ final class UtilTest extends AnimeClientTestCase $this->assertSame($expected, $this->util->isViewPage()); } - /** - * @param mixed $uri - * @param mixed $expected - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataIsViewPage')] - public function testIsFormPage($uri, $expected) + public function testIsFormPage(mixed $uri, mixed $expected) { $this->setSuperGlobals([ '_SERVER' => [ diff --git a/tests/AnimeClient/mocks.php b/tests/AnimeClient/mocks.php index 0c3bfba9..9719b981 100644 --- a/tests/AnimeClient/mocks.php +++ b/tests/AnimeClient/mocks.php @@ -29,9 +29,9 @@ class MockErrorHandler class TestEnum extends Enum { - public const FOO = 'bar'; - public const BAR = 'foo'; - public const FOOBAR = 'baz'; + final public const FOO = 'bar'; + final public const BAR = 'foo'; + final public const FOOBAR = 'baz'; } class FriendGrandParentTestClass diff --git a/tests/Ion/Di/ContainerTest.php b/tests/Ion/Di/ContainerTest.php index 2ae8130e..510d52a9 100644 --- a/tests/Ion/Di/ContainerTest.php +++ b/tests/Ion/Di/ContainerTest.php @@ -29,12 +29,9 @@ use TypeError; */ final class FooTest { - public $item; - - public function __construct($item) - { - $this->item = $item; - } + public function __construct(public $item) + { + } } class FooTest2 @@ -71,11 +68,8 @@ final class ContainerTest extends IonTestCase ]; } - /** - * @param mixed $exception - */ #[DataProvider('dataGetWithException')] - public function testGetWithException(mixed $id, $exception, ?string $message = NULL): void + public function testGetWithException(mixed $id, mixed $exception, ?string $message = NULL): void { try { @@ -92,11 +86,8 @@ final class ContainerTest extends IonTestCase } } - /** - * @param mixed $exception - */ #[DataProvider('dataGetWithException')] - public function testGetNewWithException(mixed $id, $exception, ?string $message = NULL): void + public function testGetNewWithException(mixed $id, mixed $exception, ?string $message = NULL): void { $this->expectException($exception); if ($message !== NULL) @@ -123,13 +114,8 @@ final class ContainerTest extends IonTestCase ]; } - /** - * @param mixed $id - * @param mixed $exception - * @param mixed $message - */ #[DataProvider('dataSetInstanceWithException')] - public function testSetInstanceWithException($id, $exception, $message): void + public function testSetInstanceWithException(mixed $id, mixed $exception, mixed $message): void { try { @@ -176,9 +162,7 @@ final class ContainerTest extends IonTestCase public function testGetSet(): void { - $container = $this->container->set('foo', static function () { - return static function () {}; - }); + $container = $this->container->set('foo', static fn() => static function (): void {}); $this->assertInstanceOf(Container::class, $container); $this->assertInstanceOf(ContainerInterface::class, $container); diff --git a/tests/Ion/EnumTest.php b/tests/Ion/EnumTest.php index f5d9a5bf..4a398a88 100644 --- a/tests/Ion/EnumTest.php +++ b/tests/Ion/EnumTest.php @@ -70,12 +70,8 @@ final class EnumTest extends IonTestCase ]; } - /** - * @param mixed $value - * @param mixed $static - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataIsValid')] - public function testIsValid($value, mixed $expected, $static) + public function testIsValid(mixed $value, mixed $expected, mixed $static) { $actual = ($static) ? TestEnum::isValid($value) diff --git a/tests/Ion/Exception/DoubleRenderExceptionTest.php b/tests/Ion/Exception/DoubleRenderExceptionTest.php index 700098a2..7394deef 100644 --- a/tests/Ion/Exception/DoubleRenderExceptionTest.php +++ b/tests/Ion/Exception/DoubleRenderExceptionTest.php @@ -22,7 +22,7 @@ use Aviat\Ion\Tests\IonTestCase; */ final class DoubleRenderExceptionTest extends IonTestCase { - public function testDefaultMessage() + public function testDefaultMessage(): never { $this->expectException(DoubleRenderException::class); $this->expectExceptionMessage('A view can only be rendered once, because headers can only be sent once.'); diff --git a/tests/Ion/IonTestCase.php b/tests/Ion/IonTestCase.php index 6d69a4da..bca5b68c 100644 --- a/tests/Ion/IonTestCase.php +++ b/tests/Ion/IonTestCase.php @@ -26,10 +26,10 @@ use function Aviat\Ion\_dir; class IonTestCase extends TestCase { // Test directory constants - public const ROOT_DIR = AC_TEST_ROOT_DIR; - public const SRC_DIR = SRC_DIR; - public const TEST_DATA_DIR = __DIR__ . '/test_data'; - public const TEST_VIEW_DIR = __DIR__ . '/test_views'; + final public const ROOT_DIR = AC_TEST_ROOT_DIR; + final public const SRC_DIR = SRC_DIR; + final public const TEST_DATA_DIR = __DIR__ . '/test_data'; + final public const TEST_VIEW_DIR = __DIR__ . '/test_views'; protected ContainerInterface $container; protected static $staticContainer; @@ -92,7 +92,7 @@ class IonTestCase extends TestCase // Set up DI container $di = require 'di.php'; $container = $di($config_array); - $container->set('session-handler', static function () { + $container->set('session-handler', static function (): \Aviat\Ion\Tests\TestSessionHandler { // Use mock session handler $session_handler = new TestSessionHandler(); session_set_save_handler($session_handler, TRUE); @@ -117,7 +117,7 @@ class IonTestCase extends TestCase ]; $request = call_user_func_array( - [ServerRequestFactory::class, 'fromGlobals'], + ServerRequestFactory::fromGlobals(...), array_merge($default, $supers) ); $this->container->setInstance('request', $request); diff --git a/tests/Ion/JsonTest.php b/tests/Ion/JsonTest.php index d58e24a8..4801baf4 100644 --- a/tests/Ion/JsonTest.php +++ b/tests/Ion/JsonTest.php @@ -74,7 +74,7 @@ final class JsonTest extends IonTestCase $this->assertEquals((object) $expected, Json::decode($json, FALSE)); $badJson = '{foo:{1|2}}'; - $this->expectException('Aviat\Ion\JsonException'); + $this->expectException(\Aviat\Ion\JsonException::class); $this->expectExceptionMessage('JSON_ERROR_SYNTAX - Syntax error'); $this->expectExceptionCode(JSON_ERROR_SYNTAX); diff --git a/tests/Ion/TestSessionHandler.php b/tests/Ion/TestSessionHandler.php index 4e222274..bf6348da 100644 --- a/tests/Ion/TestSessionHandler.php +++ b/tests/Ion/TestSessionHandler.php @@ -55,13 +55,13 @@ class TestSessionHandler implements SessionHandlerInterface public function read($id) { - return json_decode(@file_get_contents("{$this->save_path}/{$id}"), TRUE); + return json_decode(@file_get_contents("{$this->save_path}/{$id}"), TRUE, 512, JSON_THROW_ON_ERROR); } public function write($id, $data) { $file = "{$this->save_path}/{$id}"; - file_put_contents($file, json_encode($data)); + file_put_contents($file, json_encode($data, JSON_THROW_ON_ERROR)); return TRUE; } diff --git a/tests/Ion/Transformer/AbstractTransformerTest.php b/tests/Ion/Transformer/AbstractTransformerTest.php index bca18325..49eef04a 100644 --- a/tests/Ion/Transformer/AbstractTransformerTest.php +++ b/tests/Ion/Transformer/AbstractTransformerTest.php @@ -130,32 +130,22 @@ final class AbstractTransformerTest extends IonTestCase $this->assertSame($expected, $actual); } - /** - * @param mixed $original - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataTransformCollection')] - public function testTransformCollection($original, mixed $expected) + public function testTransformCollection(mixed $original, mixed $expected) { $actual = $this->transformer->transformCollection($original); $this->assertSame($expected, $actual); } - /** - * @param mixed $original - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')] - public function testUntransformCollection($original, mixed $expected) + public function testUntransformCollection(mixed $original, mixed $expected) { $actual = $this->untransformer->untransformCollection($original); $this->assertSame($expected, $actual); } - /** - * @param mixed $original - * @param mixed $expected - */ #[\PHPUnit\Framework\Attributes\DataProvider('dataUnTransformCollection')] - public function testUntransformCollectionWithException($original, $expected) + public function testUntransformCollectionWithException(mixed $original, mixed $expected) { $this->expectException(BadMethodCallException::class); $this->transformer->untransformCollection($original); diff --git a/tests/Ion/di.php b/tests/Ion/di.php index a1aa8608..52e0594f 100644 --- a/tests/Ion/di.php +++ b/tests/Ion/di.php @@ -16,15 +16,13 @@ return static function (array $config_array = []) { $container->setInstance('config', new Config($config_array)); - $container->set('request', static function () { - return ServerRequestFactory::fromGlobals( + $container->set('request', static fn() => ServerRequestFactory::fromGlobals( $GLOBALS['_SERVER'], $_GET, $_POST, $_COOKIE, $_FILES - ); - }); + )); $container->set('response', static fn () => new Response()); diff --git a/tests/Ion/mocks.php b/tests/Ion/mocks.php index cc3a9ca5..d9d734ec 100644 --- a/tests/Ion/mocks.php +++ b/tests/Ion/mocks.php @@ -36,9 +36,9 @@ class MockErrorHandler class TestEnum extends Enum { - public const FOO = 'bar'; - public const BAR = 'foo'; - public const FOOBAR = 'baz'; + final public const FOO = 'bar'; + final public const BAR = 'foo'; + final public const FOOBAR = 'baz'; } class FriendGrandParentTestClass @@ -49,13 +49,13 @@ class FriendGrandParentTestClass class FriendParentTestClass extends FriendGrandParentTestClass { protected $parentProtected = 47; - private $parentPrivate = 654; + private int $parentPrivate = 654; } class FriendTestClass extends FriendParentTestClass { protected $protected = 356; - private $private = 486; + private int $private = 486; protected function getProtected() {