Skip to content

Commit

Permalink
Merge pull request #56 from bram123/add-result-data-values
Browse files Browse the repository at this point in the history
Set extra result data fields
  • Loading branch information
Ocramius authored Nov 9, 2022
2 parents 5f15c02 + e4e74ed commit 2e94f1a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
7 changes: 6 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.27.0@faf106e717c37b8c81721845dba9de3d8deed8ff">
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Check/AbstractCheck.php">
<MissingConstructor occurrences="4">
<code>$label</code>
Expand Down Expand Up @@ -1006,6 +1006,11 @@
<code>Generator</code>
</MixedInferredReturnType>
</file>
<file src="test/ExtensionLoadedTest.php">
<MixedArgument occurrences="1">
<code>$extensionName</code>
</MixedArgument>
</file>
<file src="test/GuzzleHttpServiceTest.php">
<ArgumentTypeCoercion occurrences="1">
<code>$resultClass</code>
Expand Down
6 changes: 3 additions & 3 deletions src/Check/AbstractMemoryCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function check()
);

if ($percentUsed > $this->criticalThreshold) {
return new Failure($message);
return new Failure($message, $percentUsed);
}

if ($percentUsed > $this->warningThreshold) {
return new Warning($message);
return new Warning($message, $percentUsed);
}

return new Success($message);
return new Success($message, $percentUsed);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Check/ApcFragmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public function check()
$message = sprintf('%.0f%% memory fragmentation.', $fragPercent);

if ($fragPercent > $this->criticalThreshold) {
return new Failure($message);
return new Failure($message, $fragPercent);
}

if ($fragPercent > $this->warningThreshold) {
return new Warning($message);
return new Warning($message, $fragPercent);
}

return new Success($message);
return new Success($message, $fragPercent);
}
}
6 changes: 3 additions & 3 deletions src/Check/DiskUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public function check()
$dp = ($du / $dt) * 100;

if ($dp >= $this->criticalThreshold) {
return new Failure(sprintf('Disk usage too high: %2d percent.', $dp));
return new Failure(sprintf('Disk usage too high: %2d percent.', $dp), $dp);
}

if ($dp >= $this->warningThreshold) {
return new Warning(sprintf('Disk usage high: %2d percent.', $dp));
return new Warning(sprintf('Disk usage high: %2d percent.', $dp), $dp);
}

return new Success(sprintf('Disk usage is %2d percent.', $dp));
return new Success(sprintf('Disk usage is %2d percent.', $dp), $dp);
}
}
4 changes: 2 additions & 2 deletions src/Check/ExtensionLoaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function check()
}
if (count($missing)) {
if (count($missing) > 1) {
return new Failure('Extensions ' . implode(', ', $missing) . ' are not available.');
return new Failure('Extensions ' . implode(', ', $missing) . ' are not available.', $missing);
} else {
return new Failure('Extension ' . implode('', $missing) . ' is not available.');
return new Failure('Extension ' . implode('', $missing) . ' is not available.', $missing);
}
} else {
if (count($this->extensions) > 1) {
Expand Down
3 changes: 3 additions & 0 deletions test/DiskUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ public function testCheck(): void
$result = $check->check();

self::assertInstanceof(SuccessInterface::class, $result);
self::assertSame($dp, $result->getData());

$check = new DiskUsage($dp - 1, 100, $this->getTempDir());
$result = $check->check();

self::assertInstanceof(WarningInterface::class, $result);
self::assertSame($dp, $result->getData());

$check = new DiskUsage(0, $dp - 1, $this->getTempDir());
$result = $check->check();

self::assertInstanceof(FailureInterface::class, $result);
self::assertSame($dp, $result->getData());
}

public function invalidArgumentProvider(): array
Expand Down
68 changes: 68 additions & 0 deletions test/ExtensionLoadedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace LaminasTest\Diagnostics;

use InvalidArgumentException;
use Laminas\Diagnostics\Check\ExtensionLoaded;
use Laminas\Diagnostics\Result\FailureInterface;
use Laminas\Diagnostics\Result\SuccessInterface;
use PHPUnit\Framework\TestCase;
use stdClass;

use function phpversion;

/** @covers \Laminas\Diagnostics\Check\ExtensionLoaded */
final class ExtensionLoadedTest extends TestCase
{
/**
* @dataProvider invalidArgumentProvider
* @param mixed $extensionName
*/
public function testInvalidArguments($extensionName): void
{
$this->expectException(InvalidArgumentException::class);

new ExtensionLoaded($extensionName);
}

public function testCheck(): void
{
$check = new ExtensionLoaded('json');
$result = $check->check();

self::assertInstanceof(SuccessInterface::class, $result);
self::assertSame('json ' . phpversion('json'), $result->getData());

$check = new ExtensionLoaded(['json', 'xml']);
$result = $check->check();

self::assertInstanceof(SuccessInterface::class, $result);
self::assertSame(['json' => phpversion('json'), 'xml' => phpversion('xml')], $result->getData());
}

public function testCheckMissingExtension(): void
{
$check = new ExtensionLoaded('unknown-foo');
$result = $check->check();

self::assertInstanceof(FailureInterface::class, $result);
self::assertSame(['unknown-foo'], $result->getData());

$check = new ExtensionLoaded(['unknown-foo', 'unknown-bar']);
$result = $check->check();

self::assertInstanceof(FailureInterface::class, $result);
self::assertSame(['unknown-foo', 'unknown-bar'], $result->getData());
}

/**
* @return array<int, array<int, mixed>>
*/
public function invalidArgumentProvider(): array
{
return [
[new stdClass()],
[100],
];
}
}

0 comments on commit 2e94f1a

Please sign in to comment.