-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringIdTest.php
More file actions
106 lines (90 loc) · 3.28 KB
/
StringIdTest.php
File metadata and controls
106 lines (90 loc) · 3.28 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
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Tests\Unit\Toolkit\Identifiers;
use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier;
use CloudCreativity\Modules\Toolkit\ContractException;
use CloudCreativity\Modules\Toolkit\Identifiers\Guid;
use CloudCreativity\Modules\Toolkit\Identifiers\IntegerId;
use CloudCreativity\Modules\Toolkit\Identifiers\StringId;
use CloudCreativity\Modules\Toolkit\Identifiers\Uuid;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class StringIdTest extends TestCase
{
public function test(): void
{
$id = new StringId('99');
$this->assertSame('99', $id->value);
$this->assertSame('99', $id->context());
$this->assertSame('99', $id->key());
$this->assertSame('99', $id->toString());
$this->assertSame('99', (string) $id);
$this->assertJsonStringEqualsJsonString(
json_encode(['id' => '99'], \JSON_THROW_ON_ERROR),
json_encode(compact('id'), \JSON_THROW_ON_ERROR),
);
}
public function testItCanBeZero(): void
{
$id = new StringId('0');
$this->assertSame('0', $id->value);
}
public function testItMustNotBeEmpty(): void
{
$this->expectException(ContractException::class);
$this->expectExceptionMessage('Identifier value must be a non-empty string.');
new StringId('');
}
public function testItIsEquals(): void
{
$this->assertObjectEquals($id = new StringId('99'), $other = StringId::from('99'));
$this->assertSame($id, StringId::from($id));
$this->assertTrue($id->is($other));
$this->assertTrue($id->any(new StringId('foo'), new StringId('bar'), null, $other));
}
public function testItIsNotEqual(): void
{
$id = new StringId('99');
$this->assertFalse($id->equals($other = new StringId('100')));
$this->assertFalse($id->is($other));
$this->assertFalse($id->any(new StringId('foo'), new StringId('bar'), null, $other));
$this->assertFalse($id->any());
}
/**
* @return array<int, array<Identifier>>
*/
public static function notStringIdProvider(): array
{
return [
[new IntegerId(1)],
[new Guid('SomeType', new StringId('1'))],
[new Uuid(\Ramsey\Uuid\Uuid::uuid4())],
];
}
#[DataProvider('notStringIdProvider')]
public function testIsWithOtherIdentifiers(Identifier $other): void
{
$id = new StringId('1');
$this->assertFalse($id->is($other));
$this->assertFalse($id->any($other, new StringId('foo'), null));
}
public function testIsWithNull(): void
{
$id = new StringId('1');
$this->assertFalse($id->is(null));
}
#[DataProvider('notStringIdProvider')]
public function testFromWithOtherIdentifiers(Identifier $other): void
{
$this->expectException(ContractException::class);
$this->expectExceptionMessage('Unexpected identifier type, received: ' . get_debug_type($other));
StringId::from($other);
}
}