From 66db3b3b16d8b15a47d3a60159426d0cd2fc3c9e Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Tue, 1 Oct 2024 02:23:14 +0700 Subject: [PATCH] refactor: PHPStan max level > Fix errors on tests --- .../StubService/StubServerConfigInterface.php | 2 +- .../Interaction/InteractionDriverTest.php | 4 +- .../Driver/Interaction/MessageDriverTest.php | 4 +- .../Consumer/InteractionBuilderTest.php | 4 +- .../PhpPact/Consumer/Matcher/MatcherTest.php | 10 +- .../Matcher/Matchers/StatusCodeTest.php | 4 +- tests/PhpPact/Consumer/MessageBuilderTest.php | 10 +- tests/PhpPact/FFI/ClientTest.php | 92 ++++++++++++------- tests/PhpPact/FFI/Model/ArrayDataTest.php | 4 +- .../CsvInteractionDriverFactoryTest.php | 8 +- .../ProviderVerifier/VerifierTest.php | 4 +- .../Interaction/SyncMessageDriverTest.php | 4 +- .../SyncMessage/SyncMessageBuilderTest.php | 4 +- tests/PhpPact/Xml/XmlTextTest.php | 2 +- 14 files changed, 101 insertions(+), 55 deletions(-) diff --git a/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php b/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php index a1f8c641..ae4e673e 100644 --- a/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php +++ b/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php @@ -51,7 +51,7 @@ public function getLogLevel(): ?string; /** * @param string $logLevel Log level (defaults to info) [possible values: error, warn, info, debug, trace, none] */ - public function setLogLevel(string $logLevel): self; + public function setLogLevel(?string $logLevel): self; /** * @return int the port of the stub service diff --git a/tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php b/tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php index 6abba6b2..fefb7084 100644 --- a/tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php +++ b/tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php @@ -31,11 +31,11 @@ class InteractionDriverTest extends TestCase private int $pactHandle = 234; private string $description = 'Sending request receiving response'; /** - * @var array> + * @var array> */ private array $providerStates = [ 'item exist' => [ - 'id' => 12, + 'id' => '12', 'name' => 'abc', ] ]; diff --git a/tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php b/tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php index 1a6707ea..0af86172 100644 --- a/tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php +++ b/tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php @@ -29,11 +29,11 @@ class MessageDriverTest extends TestCase private int $pactHandle = 234; private string $description = 'Receiving message'; /** - * @var array> + * @var array> */ private array $providerStates = [ 'item exist' => [ - 'id' => 12, + 'id' => '12', 'name' => 'abc', ] ]; diff --git a/tests/PhpPact/Consumer/InteractionBuilderTest.php b/tests/PhpPact/Consumer/InteractionBuilderTest.php index a51d5434..59f6b861 100644 --- a/tests/PhpPact/Consumer/InteractionBuilderTest.php +++ b/tests/PhpPact/Consumer/InteractionBuilderTest.php @@ -142,7 +142,9 @@ public function testAddTextComment(): void private function getInteraction(): Interaction { $reflection = new ReflectionProperty($this->builder, 'interaction'); + $interaction = $reflection->getValue($this->builder); + $this->assertInstanceOf(Interaction::class, $interaction); - return $reflection->getValue($this->builder); + return $interaction; } } diff --git a/tests/PhpPact/Consumer/Matcher/MatcherTest.php b/tests/PhpPact/Consumer/Matcher/MatcherTest.php index 20fe1744..602c1ef1 100644 --- a/tests/PhpPact/Consumer/Matcher/MatcherTest.php +++ b/tests/PhpPact/Consumer/Matcher/MatcherTest.php @@ -194,7 +194,7 @@ public function testHexadecimal(?string $value, bool $hasGenerator): void $hexadecimal = $this->matcher->hexadecimal($value); $this->assertInstanceOf(Regex::class, $hexadecimal); if ($hasGenerator) { - $this->assertSame(RandomHexadecimal::class, get_class($hexadecimal->getGenerator())); + $this->assertInstanceOf(RandomHexadecimal::class, $hexadecimal->getGenerator()); } else { $this->assertNull($hexadecimal->getGenerator()); } @@ -207,7 +207,7 @@ public function testUuid(?string $value, bool $hasGenerator): void $uuid = $this->matcher->uuid($value); $this->assertInstanceOf(Regex::class, $uuid); if ($hasGenerator) { - $this->assertSame(Uuid::class, get_class($uuid->getGenerator())); + $this->assertInstanceOf(Uuid::class, $uuid->getGenerator()); } else { $this->assertNull($uuid->getGenerator()); } @@ -257,9 +257,9 @@ public function testFromProviderState(): void { $uuid = $this->matcher->uuid(); $this->assertInstanceOf(Regex::class, $uuid); - $this->assertSame(Uuid::class, get_class($uuid->getGenerator())); + $this->assertInstanceOf(Uuid::class, $uuid->getGenerator()); $this->assertSame($uuid, $this->matcher->fromProviderState($uuid, '${id}')); - $this->assertSame(ProviderState::class, get_class($uuid->getGenerator())); + $this->assertInstanceOf(ProviderState::class, $uuid->getGenerator()); } public function testEqual(): void @@ -345,7 +345,7 @@ public function testUrl(bool $useMockServerBasePath, bool $hasGenerator): void $url = $this->matcher->url('http://localhost:1234/path', '.*(/path)$', $useMockServerBasePath); $this->assertInstanceOf(Regex::class, $url); if ($hasGenerator) { - $this->assertSame(MockServerURL::class, get_class($url->getGenerator())); + $this->assertInstanceOf(MockServerURL::class, $url->getGenerator()); } else { $this->assertNull($url->getGenerator()); } diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php index 309e0837..32470866 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php @@ -39,7 +39,9 @@ public function testSerialize(string $status, ?int $value, ?string $json): void $matcher = new StatusCode($status, $value); $jsonEncoded = json_encode($matcher); $this->assertIsString($jsonEncoded); - $this->assertJsonStringEqualsJsonString($json, $jsonEncoded); + if ($json) { + $this->assertJsonStringEqualsJsonString($json, $jsonEncoded); + } } public function testCreateJsonFormatter(): void diff --git a/tests/PhpPact/Consumer/MessageBuilderTest.php b/tests/PhpPact/Consumer/MessageBuilderTest.php index 5d12795f..01dd497a 100644 --- a/tests/PhpPact/Consumer/MessageBuilderTest.php +++ b/tests/PhpPact/Consumer/MessageBuilderTest.php @@ -235,17 +235,21 @@ public function testVerify(bool $callbackThrowException): void private function getMessage(): Message { $reflection = new ReflectionProperty($this->builder, 'message'); + $message = $reflection->getValue($this->builder); + $this->assertInstanceOf(Message::class, $message); - return $reflection->getValue($this->builder); + return $message; } /** - * @return array + * @return array */ private function getCallbacks(): array { $reflection = new ReflectionProperty($this->builder, 'callback'); + $callback = $reflection->getValue($this->builder); + $this->assertIsArray($callback); - return $reflection->getValue($this->builder); + return $callback; } } diff --git a/tests/PhpPact/FFI/ClientTest.php b/tests/PhpPact/FFI/ClientTest.php index 62beaf72..2b664602 100644 --- a/tests/PhpPact/FFI/ClientTest.php +++ b/tests/PhpPact/FFI/ClientTest.php @@ -205,117 +205,147 @@ public function testVerifierNewForApplication(): void public function testVerifierSetProviderInfo(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierSetProviderInfo($handle, null, null, null, null, null); + if ($handle) { + $this->client->verifierSetProviderInfo($handle, null, null, null, null, null); + } $this->expectNotToPerformAssertions(); } public function testVerifierAddProviderTransport(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierAddProviderTransport($handle, null, null, null, null); + if ($handle) { + $this->client->verifierAddProviderTransport($handle, null, null, null, null); + } $this->expectNotToPerformAssertions(); } public function testVerifierSetFilterInfo(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierSetFilterInfo($handle, null, null, true); + if ($handle) { + $this->client->verifierSetFilterInfo($handle, null, null, true); + } $this->expectNotToPerformAssertions(); } public function testVerifierSetProviderState(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierSetProviderState($handle, null, true, true); + if ($handle) { + $this->client->verifierSetProviderState($handle, null, true, true); + } $this->expectNotToPerformAssertions(); } public function testVerifierSetVerificationOptions(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $result = $this->client->verifierSetVerificationOptions($handle, true, 1); - $this->assertSame(0, $result); + if ($handle) { + $result = $this->client->verifierSetVerificationOptions($handle, true, 1); + $this->assertSame(0, $result); + } } public function testVerifierSetPublishOptions(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $result = $this->client->verifierSetPublishOptions($handle, '1.0.0', null, null, 'some-branch'); - $this->assertSame(0, $result); + if ($handle) { + $result = $this->client->verifierSetPublishOptions($handle, '1.0.0', null, null, 'some-branch'); + $this->assertSame(0, $result); + } } public function testVerifierSetConsumerFilters(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierSetConsumerFilters($handle, null); + if ($handle) { + $this->client->verifierSetConsumerFilters($handle, null); + } $this->expectNotToPerformAssertions(); } public function testVerifierAddCustomHeader(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierAddCustomHeader($handle, 'name', 'value'); + if ($handle) { + $this->client->verifierAddCustomHeader($handle, 'name', 'value'); + } $this->expectNotToPerformAssertions(); } public function testVerifierAddFileSource(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierAddFileSource($handle, '/path/to/file'); + if ($handle) { + $this->client->verifierAddFileSource($handle, '/path/to/file'); + } $this->expectNotToPerformAssertions(); } public function testVerifierAddDirectorySource(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierAddDirectorySource($handle, '/path/to/directory'); + if ($handle) { + $this->client->verifierAddDirectorySource($handle, '/path/to/directory'); + } $this->expectNotToPerformAssertions(); } public function testVerifierAddUrlSource(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierAddUrlSource($handle, 'http://example.domain/file.ext', null, null, null); + if ($handle) { + $this->client->verifierAddUrlSource($handle, 'http://example.domain/file.ext', null, null, null); + } $this->expectNotToPerformAssertions(); } public function testVerifierBrokerSourceWithSelectors(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierBrokerSourceWithSelectors( - $handle, - 'http://example.domain/file.ext', - null, - null, - null, - true, - null, - null, - null, - null, - null - ); + if ($handle) { + $this->client->verifierBrokerSourceWithSelectors( + $handle, + 'http://example.domain/file.ext', + null, + null, + null, + true, + null, + null, + null, + null, + null + ); + } $this->expectNotToPerformAssertions(); } public function testVerifierExecute(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $result = $this->client->verifierExecute($handle); - $this->assertSame(0, $result); + if ($handle) { + $result = $this->client->verifierExecute($handle); + $this->assertSame(0, $result); + } } public function testVerifierJson(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $result = $this->client->verifierJson($handle); - $this->assertSame('{"errors":[],"notices":[],"output":[],"pendingErrors":[],"result":true}', $result); + if ($handle) { + $result = $this->client->verifierJson($handle); + $this->assertSame('{"errors":[],"notices":[],"output":[],"pendingErrors":[],"result":true}', $result); + } } public function testVerifierShutdown(): void { $handle = $this->client->verifierNewForApplication('name', '1.1'); - $this->client->verifierShutdown($handle); + if ($handle) { + $this->client->verifierShutdown($handle); + } $this->expectNotToPerformAssertions(); } diff --git a/tests/PhpPact/FFI/Model/ArrayDataTest.php b/tests/PhpPact/FFI/Model/ArrayDataTest.php index 8b198826..3d4e2ec9 100644 --- a/tests/PhpPact/FFI/Model/ArrayDataTest.php +++ b/tests/PhpPact/FFI/Model/ArrayDataTest.php @@ -17,11 +17,11 @@ public function testCreateFromArray(): void { $branches = ['feature-x', 'master', 'test', 'prod']; $arrayData = ArrayData::createFrom($branches); - + $this->assertInstanceOf(ArrayData::class, $arrayData); $this->assertSame(count($branches), $arrayData->getSize()); foreach ($branches as $index => $branch) { // @phpstan-ignore offsetAccess.nonOffsetAccessible - $this->assertSame($branch, FFI::string($arrayData->getItems()[$index])); // @phpstan-ignore-line + $this->assertSame($branch, FFI::string($arrayData->getItems()[$index])); } } } diff --git a/tests/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactoryTest.php b/tests/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactoryTest.php index c7fbd35d..08174b64 100644 --- a/tests/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactoryTest.php +++ b/tests/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactoryTest.php @@ -76,14 +76,18 @@ public function testMissingPluginPartsException(): void private function getRequestDriver(InteractionDriverInterface $driver): RequestDriverInterface { $reflection = new ReflectionProperty($driver, 'requestDriver'); + $requestDriver = $reflection->getValue($driver); + $this->assertInstanceOf(RequestDriverInterface::class, $requestDriver); - return $reflection->getValue($driver); + return $requestDriver; } private function getResponseDriver(InteractionDriverInterface $driver): ResponseDriverInterface { $reflection = new ReflectionProperty($driver, 'responseDriver'); + $responseDriver = $reflection->getValue($driver); + $this->assertInstanceOf(ResponseDriverInterface::class, $responseDriver); - return $reflection->getValue($driver); + return $responseDriver; } } diff --git a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php index 3ff2d152..66a36d90 100644 --- a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php +++ b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php @@ -35,7 +35,9 @@ protected function setUp(): void $this->config = new VerifierConfig(); $this->logger = $this->createMock(LoggerInterface::class); $this->client = $this->createMock(ClientInterface::class); - $this->handle = FFI::new('int'); + $handle = FFI::new('int'); + $this->assertInstanceOf(CData::class, $handle); + $this->handle = $handle; } private function setUpCalls(bool $hasProviderTags = true, bool $hasFilterConsumerNames = true): void diff --git a/tests/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriverTest.php b/tests/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriverTest.php index b054758f..49fa0f3c 100644 --- a/tests/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriverTest.php +++ b/tests/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriverTest.php @@ -29,11 +29,11 @@ class SyncMessageDriverTest extends TestCase private int $pactHandle = 234; private string $description = 'Receiving message'; /** - * @var array + * @var array> */ private array $providerStates = [ 'item exist' => [ - 'id' => 12, + 'id' => '12', 'name' => 'abc', ] ]; diff --git a/tests/PhpPact/SyncMessage/SyncMessageBuilderTest.php b/tests/PhpPact/SyncMessage/SyncMessageBuilderTest.php index bb006c94..5031585c 100644 --- a/tests/PhpPact/SyncMessage/SyncMessageBuilderTest.php +++ b/tests/PhpPact/SyncMessage/SyncMessageBuilderTest.php @@ -146,7 +146,9 @@ public function testVerify(bool $matched): void private function getMessage(): Message { $reflection = new ReflectionProperty($this->builder, 'message'); + $message = $reflection->getValue($this->builder); + $this->assertInstanceOf(Message::class, $message); - return $reflection->getValue($this->builder); + return $message; } } diff --git a/tests/PhpPact/Xml/XmlTextTest.php b/tests/PhpPact/Xml/XmlTextTest.php index ee0e3ee3..cfe9a9cf 100644 --- a/tests/PhpPact/Xml/XmlTextTest.php +++ b/tests/PhpPact/Xml/XmlTextTest.php @@ -17,7 +17,7 @@ class XmlTextTest extends TestCase #[TestWith([false])] #[TestWith([true])] #[TestWith([null])] - public function testJsonSerializePredefinedTypes(mixed $content): void + public function testJsonSerializePredefinedTypes(string|float|int|bool|null $content): void { $text = new XmlText($content); $this->assertSame(json_encode(['content' => $content]), json_encode($text));