forked from Skillshare/formatphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdInterpolatorTest.php
More file actions
131 lines (113 loc) · 4.96 KB
/
IdInterpolatorTest.php
File metadata and controls
131 lines (113 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace FormatPHP\Test\Extractor;
use FormatPHP\Descriptor;
use FormatPHP\DescriptorInterface;
use FormatPHP\Exception\InvalidArgumentException;
use FormatPHP\Exception\UnableToGenerateMessageIdException;
use FormatPHP\Extractor\IdInterpolator;
use FormatPHP\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
class IdInterpolatorTest extends TestCase
{
#[DataProvider('generateIdProvider')]
public function testGenerateId(DescriptorInterface $descriptor, ?string $pattern, string $expectedId): void
{
$idInterpolator = new IdInterpolator();
if ($pattern !== null) {
$this->assertSame($expectedId, $idInterpolator->generateId($descriptor, $pattern));
} else {
$this->assertSame($expectedId, $idInterpolator->generateId($descriptor));
}
}
/**
* @return array<array{descriptor: DescriptorInterface, pattern: string | null, expectedId: string}>
*/
public static function generateIdProvider(): array
{
return [
'descriptor has ID' => [
'descriptor' => new Descriptor('aMessageId'),
'pattern' => null,
'expectedId' => 'aMessageId',
],
'default message with default pattern' => [
'descriptor' => new Descriptor(null, 'my default message'),
'pattern' => null,
'expectedId' => 'LbPcjH',
],
'default message and description with default pattern' => [
'descriptor' => new Descriptor(null, 'my default message', 'test description'),
'pattern' => null,
'expectedId' => 'R1aZay',
],
'custom pattern with crc32, base64, and 8' => [
'descriptor' => new Descriptor(null, '<<???>>'),
'pattern' => '[crc32:contenthash:base64:8]',
'expectedId' => 'ZSFCKQ==',
],
'custom pattern with crc32, base64url, and 8' => [
'descriptor' => new Descriptor(null, '<<???>>'),
'pattern' => '[crc32:contenthash:base64url:8]',
'expectedId' => 'ZSFCKQ',
],
'custom pattern with sha3-384, base64, and 20' => [
'descriptor' => new Descriptor(null, '<<???/>>>>'),
'pattern' => '[sha3-384:contenthash:base64:20]',
'expectedId' => 'WC+Ex4/ILCD7Lb6tRv7x',
],
'custom pattern with sha3-384, base64url, and 20' => [
'descriptor' => new Descriptor(null, '<<???/>>>>'),
'pattern' => '[sha3-384:contenthash:base64url:20]',
'expectedId' => 'WC-Ex4_ILCD7Lb6tRv7x',
],
'custom pattern with md5, hex, and 6' => [
'descriptor' => new Descriptor(null, 'some message'),
'pattern' => '[md5:contenthash:hex:6]',
'expectedId' => 'df49b6',
],
'custom pattern using hash instead of contenthash' => [
'descriptor' => new Descriptor(null, 'some message'),
'pattern' => '[md5:hash:hex:6]',
'expectedId' => 'df49b6',
],
];
}
public function testGenerateIdThrowsExceptionWhenUnableToGenerateMessageId(): void
{
$idInterpolator = new IdInterpolator();
$this->expectException(UnableToGenerateMessageIdException::class);
$this->expectExceptionMessage(
'To auto-generate a message ID, the message descriptor must '
. 'have a default message and, optionally, a description.',
);
$idInterpolator->generateId(new Descriptor(null, null, 'a description'));
}
public function testGenerateIdThrowsExceptionWhenInterpolationPatternIsInvalid(): void
{
$idInterpolator = new IdInterpolator();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Pattern is not a valid ID interpolation pattern: "[sha256:something:base64:6]".',
);
$idInterpolator->generateId(new Descriptor(null, 'foo'), '[sha256:something:base64:6]');
}
public function testGenerateIdThrowsExceptionWhenHashingAlgorithmIsInvalid(): void
{
$idInterpolator = new IdInterpolator();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Unknown or unsupported hashing algorithm: "foobar".',
);
$idInterpolator->generateId(new Descriptor(null, 'foo'), '[foobar:hash:base64:6]');
}
public function testGenerateIdThrowsExceptionWhenEncodingAlgorithmIsInvalid(): void
{
$idInterpolator = new IdInterpolator();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Unknown or unsupported encoding algorithm: "baz".',
);
$idInterpolator->generateId(new Descriptor(null, 'foo'), '[md5:hash:baz:6]');
}
}