-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathAlgorithmManagerTest.php
More file actions
128 lines (101 loc) · 3.46 KB
/
Copy pathAlgorithmManagerTest.php
File metadata and controls
128 lines (101 loc) · 3.46 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
<?php
declare(strict_types=1);
namespace Jose\Tests\Component\Core;
use Jose\Component\Core\AlgorithmManager;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use const E_USER_DEPRECATED;
/**
* @internal
*/
final class AlgorithmManagerTest extends TestCase
{
#[Test]
public function theManagerIsBuiltFromTheAlgorithmsPassedToTheConstructor(): void
{
$algorithm = new FooAlgorithm();
$sut = new AlgorithmManager([$algorithm]);
static::assertTrue($sut->has('foo'));
static::assertSame(['foo'], $sut->list());
static::assertSame([
'foo' => $algorithm,
], $sut->all());
static::assertSame($algorithm, $sut->get('foo'));
}
#[Test]
public function theConstructorDoesNotTriggerTheDeprecationOfTheAddMethod(): void
{
$deprecations = $this->collectDeprecations(static function (): void {
new AlgorithmManager([new FooAlgorithm()]);
});
static::assertSame([], $deprecations);
}
#[Test]
public function withReturnsANewManagerAndLeavesTheCurrentOneUntouched(): void
{
$sut = new AlgorithmManager([new FooAlgorithm()]);
$new = $sut->with(new BarAlgorithm());
static::assertNotSame($sut, $new);
static::assertSame(['foo'], $sut->list());
static::assertSame(['foo', 'bar'], $new->list());
}
#[Test]
public function withAcceptsSeveralAlgorithmsAtOnce(): void
{
$sut = new AlgorithmManager([]);
$new = $sut->with(new FooAlgorithm(), new BarAlgorithm());
static::assertSame(['foo', 'bar'], $new->list());
}
#[Test]
public function withReplacesAnAlgorithmThatHasTheSameName(): void
{
$replacement = new FooAlgorithm();
$sut = new AlgorithmManager([new FooAlgorithm()]);
$new = $sut->with($replacement);
static::assertSame(['foo'], $new->list());
static::assertSame($replacement, $new->get('foo'));
}
#[Test]
public function withDoesNotTriggerAnyDeprecation(): void
{
$sut = new AlgorithmManager([new FooAlgorithm()]);
$deprecations = $this->collectDeprecations(static function () use ($sut): void {
$sut->with(new BarAlgorithm());
});
static::assertSame([], $deprecations);
}
#[Test]
public function theAddMethodIsDeprecatedButStillMutatesTheManager(): void
{
$sut = new AlgorithmManager([new FooAlgorithm()]);
$algorithm = new BarAlgorithm();
$deprecations = $this->collectDeprecations(static function () use ($sut, $algorithm): void {
$sut->add($algorithm);
});
static::assertCount(1, $deprecations);
static::assertStringContainsString(
'The method "Jose\Component\Core\AlgorithmManager::add()" is deprecated and will be removed in 5.0.0.',
$deprecations[0]
);
static::assertSame($algorithm, $sut->get('bar'));
}
/**
* @param callable(): void $callback
*
* @return list<string>
*/
private function collectDeprecations(callable $callback): array
{
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;
return true;
}, E_USER_DEPRECATED);
try {
$callback();
} finally {
restore_error_handler();
}
return $deprecations;
}
}