Files
php-uuid/tests/Generator/DefaultNameGeneratorTest.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

86 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Exception\NameException;
use Ramsey\Uuid\Generator\DefaultNameGenerator;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Uuid;
use function hash;
class DefaultNameGeneratorTest extends TestCase
{
/**
* @param non-empty-string $ns
* @param non-empty-string $name
* @param non-empty-string $algorithm
*
* @dataProvider provideNamesForHashingTest
*/
public function testDefaultNameGeneratorHashesName(string $ns, string $name, string $algorithm): void
{
$namespace = Uuid::fromString($ns);
$expectedBytes = hash($algorithm, $namespace->getBytes() . $name, true);
$generator = new DefaultNameGenerator();
$this->assertSame($expectedBytes, $generator->generate($namespace, $name, $algorithm));
}
/**
* @return array<array{ns: non-empty-string, name: non-empty-string, algorithm: non-empty-string}>
*/
public function provideNamesForHashingTest(): array
{
return [
[
'ns' => Uuid::NAMESPACE_URL,
'name' => 'https://example.com/foobar',
'algorithm' => 'md5',
],
[
'ns' => Uuid::NAMESPACE_URL,
'name' => 'https://example.com/foobar',
'algorithm' => 'sha1',
],
[
'ns' => Uuid::NAMESPACE_URL,
'name' => 'https://example.com/foobar',
'algorithm' => 'sha256',
],
[
'ns' => Uuid::NAMESPACE_OID,
'name' => '1.3.6.1.4.1.343',
'algorithm' => 'sha1',
],
[
'ns' => Uuid::NAMESPACE_OID,
'name' => '1.3.6.1.4.1.52627',
'algorithm' => 'md5',
],
[
'ns' => 'd988ae29-674e-48e7-b93c-2825e2a96fbe',
'name' => 'foobar',
'algorithm' => 'sha1',
],
];
}
public function testGenerateThrowsException(): void
{
$namespace = Uuid::fromString('cd998804-c661-4264-822c-00cada75a87b');
$generator = new DefaultNameGenerator();
$this->expectException(NameException::class);
$this->expectExceptionMessage(
'Unable to hash namespace and name with algorithm \'aBadAlgorithm\''
);
/** @phpstan-ignore method.resultUnused */
$generator->generate($namespace, 'a test name', 'aBadAlgorithm');
}
}