Skip to content

Commit

Permalink
Merge branch 'master' into phpstan-max-level
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Oct 1, 2024
2 parents 0bb7e54 + 5e03397 commit 51346a6
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 113 deletions.
19 changes: 6 additions & 13 deletions src/PhpPact/Consumer/Driver/Pact/PactDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function __construct(

public function cleanUp(): void
{
$this->validatePact();
$success = $this->client->freePactHandle($this->pact->handle) === 0;
$success = $this->client->freePactHandle($this->getPact()->handle) === 0;
if (!$success) {
trigger_error('Can not free pact handle. The handle is not valid or does not refer to a valid Pact. Could be that it was previously deleted.', E_USER_WARNING);
}
Expand All @@ -32,9 +31,8 @@ public function cleanUp(): void

public function writePact(): void
{
$this->validatePact();
$error = $this->client->pactHandleWriteFile(
$this->pact->handle,
$this->getPact()->handle,
$this->config->getPactDir(),
$this->config->getPactFileWriteMode() === PactConfigInterface::MODE_OVERWRITE
);
Expand All @@ -45,7 +43,9 @@ public function writePact(): void

public function getPact(): Pact
{
$this->validatePact();
if (!$this->pact) {
throw new MissingPactException();
}

return $this->pact;
}
Expand Down Expand Up @@ -76,13 +76,6 @@ protected function getSpecification(): int
};
}

protected function validatePact(): void
{
if (!$this->pact) {
throw new MissingPactException();
}
}

private function versionEqualTo(string $version): bool
{
return Comparator::equalTo($this->config->getPactSpecificationVersion(), $version);
Expand All @@ -103,7 +96,7 @@ private function newPact(): void

private function withSpecification(): void
{
$success = $this->client->withSpecification($this->pact->handle, $this->getSpecification());
$success = $this->client->withSpecification($this->getPact()->handle, $this->getSpecification());
if (!$success) {
throw new PactNotModifiedException("The pact can't be modified (i.e. the mock server for it has already started, or the version is invalid)");
}
Expand Down
3 changes: 2 additions & 1 deletion src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,10 @@ public function matchingField(string $fieldName): MatcherInterface
}

/**
* @param array<mixed>|object $value
* @param MatcherInterface[] $matchers
*/
public function matchAll(mixed $value, array $matchers): MatcherInterface
public function matchAll(object|array $value, array $matchers): MatcherInterface
{
return $this->withFormatter(new MatchAll($value, $matchers));
}
Expand Down
10 changes: 2 additions & 8 deletions src/PhpPact/Consumer/Matcher/Matchers/Equality.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
class Equality extends AbstractMatcher
{
/**
* @param object|array<mixed>|string|float|int|bool|null $value
*/
public function __construct(private object|array|string|float|int|bool|null $value)
public function __construct(private mixed $value)
{
parent::__construct();
}
Expand All @@ -25,10 +22,7 @@ protected function getAttributesData(): array
return [];
}

/**
* @return object|array<mixed>|string|float|int|bool|null
*/
public function getValue(): object|array|string|float|int|bool|null
public function getValue(): mixed
{
return $this->value;
}
Expand Down
10 changes: 2 additions & 8 deletions src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
class NotEmpty extends AbstractMatcher
{
/**
* @param object|array<mixed>|string|float|int|bool $value
*/
public function __construct(private object|array|string|float|int|bool $value)
public function __construct(private mixed $value)
{
parent::__construct();
}
Expand All @@ -25,10 +22,7 @@ protected function getAttributesData(): array
return [];
}

/**
* @return object|array<mixed>|string|float|int|bool
*/
public function getValue(): object|array|string|float|int|bool
public function getValue(): mixed
{
return $this->value;
}
Expand Down
10 changes: 2 additions & 8 deletions src/PhpPact/Consumer/Matcher/Matchers/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
class Type extends AbstractMatcher
{
/**
* @param object|array<mixed>|string|float|int|bool|null $value
*/
public function __construct(private object|array|string|float|int|bool|null $value)
public function __construct(private mixed $value)
{
parent::__construct();
}
Expand All @@ -25,10 +22,7 @@ protected function getAttributesData(): array
return [];
}

/**
* @return object|array<mixed>|string|float|int|bool|null
*/
public function getValue(): object|array|string|float|int|bool|null
public function getValue(): mixed
{
return $this->value;
}
Expand Down
5 changes: 2 additions & 3 deletions src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ abstract class AbstractPluginPactDriver extends PactDriver
{
public function cleanUp(): void
{
$this->validatePact();
$this->client->cleanupPlugins($this->pact->handle);
$this->client->cleanupPlugins($this->getPact()->handle);
parent::cleanUp();
}

Expand All @@ -34,7 +33,7 @@ private function usingPlugin(): self
throw new PluginNotSupportedBySpecificationException($this->config->getPactSpecificationVersion());
}

$error = $this->client->usingPlugin($this->pact->handle, $this->getPluginName(), $this->getPluginVersion());
$error = $this->client->usingPlugin($this->getPact()->handle, $this->getPluginName(), $this->getPluginVersion());
if ($error) {
throw new PluginNotLoadedException($error);
}
Expand Down
27 changes: 17 additions & 10 deletions src/PhpPact/Standalone/MockService/MockServerEnvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ class MockServerEnvConfig extends MockServerConfig
*/
public function __construct()
{
if ($host = $this->parseEnv('PACT_MOCK_SERVER_HOST', false)) {
if ($host = $this->parseEnv('PACT_MOCK_SERVER_HOST')) {
$this->setHost($host);
}

if ($port = $this->parseEnv('PACT_MOCK_SERVER_PORT', false)) {
if ($port = $this->parseEnv('PACT_MOCK_SERVER_PORT')) {
$this->setPort((int) $port);
}

$this->setConsumer($this->parseEnv('PACT_CONSUMER_NAME'));
$this->setProvider($this->parseEnv('PACT_PROVIDER_NAME'));
$this->setPactDir($this->parseEnv('PACT_OUTPUT_DIR', false));
$this->setConsumer($this->parseRequiredEnv('PACT_CONSUMER_NAME'));
$this->setProvider($this->parseRequiredEnv('PACT_PROVIDER_NAME'));
$this->setPactDir($this->parseEnv('PACT_OUTPUT_DIR'));

if ($logDir = $this->parseEnv('PACT_LOG', false)) {
if ($logDir = $this->parseEnv('PACT_LOG')) {
$this->setLog($logDir);
}

if ($logLevel = $this->parseEnv('PACT_LOGLEVEL', false)) {
if ($logLevel = $this->parseEnv('PACT_LOGLEVEL')) {
$this->setLogLevel($logLevel);
}

$version = $this->parseEnv('PACT_SPECIFICATION_VERSION', false);
$version = $this->parseEnv('PACT_SPECIFICATION_VERSION');
if (!$version) {
$version = static::DEFAULT_SPECIFICATION_VERSION;
}
Expand All @@ -47,15 +47,22 @@ public function __construct()
*
* @throws MissingEnvVariableException
*/
private function parseEnv(string $variableName, bool $required = true): ?string
private function parseEnv(string $variableName): ?string
{
$result = \getenv($variableName);

if (is_bool($result)) {
$result = null;
}

if ($required === true && $result === null) {
return $result;
}

private function parseRequiredEnv(string $variableName): string
{
$result = $this->parseEnv($variableName);

if ($result === null) {
throw new MissingEnvVariableException($variableName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function __construct(
*/
public function jsonSerialize(): array
{
return array_filter(get_object_vars($this), fn (null|string|bool $value) => null !== $value);
return array_filter(get_object_vars($this), fn (mixed $value) => is_bool($value) || is_string($value));
}
}
2 changes: 1 addition & 1 deletion src/PhpPact/Standalone/ProviderVerifier/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function setVerificationOptions(VerifierConfigInterface $config): void

private function setPublishOptions(VerifierConfigInterface $config): void
{
if ($config->isPublishResults()) {
if ($config->isPublishResults() && $config->getPublishOptions()) {
$providerTags = ArrayData::createFrom($config->getPublishOptions()->getProviderTags());
$this->client->verifierSetPublishOptions(
$this->handle,
Expand Down
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
5 changes: 4 additions & 1 deletion src/PhpPact/Xml/XmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public function __construct(callable ...$options)

public function setName(string $name): self
{
$this->name = preg_replace('/(^[0-9]+|[^a-zA-Z0-9\-\_\:]+)/', '', $name);
if (preg_match('/^[0-9]+|[^a-zA-Z0-9\-\_\:]+/', $name) !== 0) {
throw new InvalidXmlElementException("Xml element's name is invalid");
}
$this->name = $name;

return $this;
}
Expand Down
5 changes: 1 addition & 4 deletions src/PhpPact/Xml/XmlText.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public function __construct(private string|float|int|bool|null|MatcherInterface
{
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
public function jsonSerialize(): mixed
{
if ($this->content instanceof MatcherInterface) {
return $this->content->jsonSerialize();
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;
}
}
Loading

0 comments on commit 51346a6

Please sign in to comment.