Skip to content

Commit

Permalink
Merge pull request #670 from tienvx/phpstan-max-tests
Browse files Browse the repository at this point in the history
refactor: PHPStan max level > Fix errors on tests
  • Loading branch information
tienvx authored Oct 1, 2024
2 parents 5449976 + 66db3b3 commit 5e03397
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class InteractionDriverTest extends TestCase
private int $pactHandle = 234;
private string $description = 'Sending request receiving response';
/**
* @var array<string, array<string, mixed>>
* @var array<string, array<string, string>>
*/
private array $providerStates = [
'item exist' => [
'id' => 12,
'id' => '12',
'name' => 'abc',
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class MessageDriverTest extends TestCase
private int $pactHandle = 234;
private string $description = 'Receiving message';
/**
* @var array<string, array<string, mixed>>
* @var array<string, array<string, string>>
*/
private array $providerStates = [
'item exist' => [
'id' => 12,
'id' => '12',
'name' => 'abc',
]
];
Expand Down
4 changes: 3 additions & 1 deletion tests/PhpPact/Consumer/InteractionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 5 additions & 5 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 3 additions & 1 deletion tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions tests/PhpPact/Consumer/MessageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, callable>
* @return array<mixed, callable>
*/
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;
}
}
92 changes: 61 additions & 31 deletions tests/PhpPact/FFI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/PhpPact/FFI/Model/ArrayDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class SyncMessageDriverTest extends TestCase
private int $pactHandle = 234;
private string $description = 'Receiving message';
/**
* @var array<string, mixed>
* @var array<string, array<string, string>>
*/
private array $providerStates = [
'item exist' => [
'id' => 12,
'id' => '12',
'name' => 'abc',
]
];
Expand Down
4 changes: 3 additions & 1 deletion tests/PhpPact/SyncMessage/SyncMessageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/PhpPact/Xml/XmlTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 5e03397

Please sign in to comment.