Files
php-uuid/tests/Converter/Time/BigNumberTimeConverterTest.php
T
Brandon Morrison aa18ce15d5 Fix pure annotations (#605)
Coming from https://github.com/ramsey/uuid/pull/603, this is an attempt
to fix the errors raised by the current phpstan settings.

I went through each of the errors raised by phpstan with the following
approach.

- If a method is part of an `@immutable` class, we can consider it pure,
  assuming it only affects internal variables.
- If a potentially pure method is calling a class's method that is only
  swapped during testing (and not during normal usage), then we can
  consider the calling method pure.
- If a class is marked deprecated, don't bother with attempting to mark
  it pure or immutable.
2025-06-25 08:24:36 -05:00

77 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Converter\Time;
use Brick\Math\BigInteger;
use Ramsey\Uuid\Converter\Time\BigNumberTimeConverter;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Type\Hexadecimal;
use function sprintf;
class BigNumberTimeConverterTest extends TestCase
{
public function testCalculateTimeReturnsArrayOfTimeSegments(): void
{
$seconds = BigInteger::of(5);
$microseconds = BigInteger::of(3);
$calculatedTime = BigInteger::zero()
->plus($seconds->multipliedBy(10000000))
->plus($microseconds->multipliedBy(10))
->plus(BigInteger::fromBase('01b21dd213814000', 16));
$maskLow = BigInteger::fromBase('ffffffff', 16);
$maskMid = BigInteger::fromBase('ffff', 16);
$maskHi = BigInteger::fromBase('0fff', 16);
$expected = sprintf('%04s', $calculatedTime->shiftedRight(48)->and($maskHi)->toBase(16));
$expected .= sprintf('%04s', $calculatedTime->shiftedRight(32)->and($maskMid)->toBase(16));
$expected .= sprintf('%08s', $calculatedTime->and($maskLow)->toBase(16));
$converter = new BigNumberTimeConverter();
$returned = $converter->calculateTime((string) $seconds, (string) $microseconds);
$this->assertSame($expected, $returned->toString());
}
public function testConvertTime(): void
{
$converter = new BigNumberTimeConverter();
$returned = $converter->convertTime(new Hexadecimal('1e1c57dff6f8cb0'));
$this->assertSame('1341368074', $returned->getSeconds()->toString());
}
public function testCalculateTimeThrowsExceptionWhenSecondsIsNotOnlyDigits(): void
{
$converter = new BigNumberTimeConverter();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Value must be a signed integer or a string containing only digits '
. '0-9 and, optionally, a sign (+ or -)'
);
/** @phpstan-ignore method.resultUnused */
$converter->calculateTime('12.34', '5678');
}
public function testCalculateTimeThrowsExceptionWhenMicrosecondsIsNotOnlyDigits(): void
{
$converter = new BigNumberTimeConverter();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Value must be a signed integer or a string containing only digits '
. '0-9 and, optionally, a sign (+ or -)'
);
/** @phpstan-ignore method.resultUnused */
$converter->calculateTime('1234', '56.78');
}
}