Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ public function getType(): string {
/**
* Adds a value for this metric.
*
* @param mixed $value
* The value.
* @param int<0, max> $value
* The value. Non-int values are deprecated.
* @param array<string, string|int|float> $labels
* The list of key value label pairs.
*
* @throws \InvalidArgumentException
* If the value is not a non-negative integer.
*/
public function set(mixed $value, array $labels = []): void {
// @phpstan-ignore function.alreadyNarrowedType
if (!is_int($value)) {
@trigger_error(sprintf('Passing a non-int value to %s() is deprecated in php_prometheus:1.1.0 and will throw a \TypeError in php_prometheus:2.0.0. See https://github.com/previousnext/php-prometheus/issues/16', __METHOD__), E_USER_DEPRECATED);
}
if (!$this->isValidValue($value)) {
throw new \InvalidArgumentException("A count value must be a positive integer.");
}
Expand Down
39 changes: 37 additions & 2 deletions tests/Unit/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PNX\Prometheus\Tests\Unit;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\TestCase;
use PNX\Prometheus\Counter;

Expand All @@ -18,18 +19,52 @@ class CounterTest extends TestCase {
* Tests setting counter values.
*/
public function testCounter(): void {

$counter = new Counter('foo', 'bar', 'Example counter help');
$counter->set(89, ['baz' => 'wiz']);

$this->assertEquals("foo_bar", $counter->getName());
$this->assertEquals("counter", $counter->getType());

$values = $counter->getLabelledValues();
$this->assertCount(1, $values);
$this->assertEquals(89, $values[0]->getValue());
}

/**
* Tests that a negative value throws an exception.
*/
public function testNegativeValueThrows(): void {
$counter = new Counter('foo', 'bar', 'Example counter help');
$this->expectException(\InvalidArgumentException::class);
// @phpstan-ignore argument.type
$counter->set(-1, ['baz' => 'wiz']);
}

/**
* Tests that a non-int value triggers a deprecation notice.
*/
public function testNonIntValueIsDeprecated(): void {
$counter = new Counter('foo', 'bar', 'Example counter help');
$this->expectUserDeprecationMessage('Passing a non-int value to PNX\Prometheus\Counter::set() is deprecated in php_prometheus:1.1.0 and will throw a \TypeError in php_prometheus:2.0.0. See https://github.com/previousnext/php-prometheus/issues/16');

try {
// @phpstan-ignore argument.type
$counter->set('bing', ['baz' => 'wiz']);
}
catch (\InvalidArgumentException) {
// The deprecation is triggered before the exception is thrown.
}
}

/**
* Tests that a non-int value still throws an exception.
*/
#[IgnoreDeprecations]
public function testNonIntValueThrows(): void {
$counter = new Counter('foo', 'bar', 'Example counter help');
$this->expectException(\InvalidArgumentException::class);
$counter->set('bing', ['baz' => 'wiz']);
// @phpstan-ignore argument.type
@$counter->set('bing', ['baz' => 'wiz']);
}

/**
Expand Down