|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Jose\Tests\Component\Core; |
| 6 | + |
| 7 | +use Jose\Component\Core\AlgorithmManager; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use const E_USER_DEPRECATED; |
| 11 | + |
| 12 | +/** |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +final class AlgorithmManagerTest extends TestCase |
| 16 | +{ |
| 17 | + #[Test] |
| 18 | + public function theManagerIsBuiltFromTheAlgorithmsPassedToTheConstructor(): void |
| 19 | + { |
| 20 | + $algorithm = new FooAlgorithm(); |
| 21 | + |
| 22 | + $sut = new AlgorithmManager([$algorithm]); |
| 23 | + |
| 24 | + static::assertTrue($sut->has('foo')); |
| 25 | + static::assertSame(['foo'], $sut->list()); |
| 26 | + static::assertSame([ |
| 27 | + 'foo' => $algorithm, |
| 28 | + ], $sut->all()); |
| 29 | + static::assertSame($algorithm, $sut->get('foo')); |
| 30 | + } |
| 31 | + |
| 32 | + #[Test] |
| 33 | + public function theConstructorDoesNotTriggerTheDeprecationOfTheAddMethod(): void |
| 34 | + { |
| 35 | + $deprecations = $this->collectDeprecations(static function (): void { |
| 36 | + new AlgorithmManager([new FooAlgorithm()]); |
| 37 | + }); |
| 38 | + |
| 39 | + static::assertSame([], $deprecations); |
| 40 | + } |
| 41 | + |
| 42 | + #[Test] |
| 43 | + public function withReturnsANewManagerAndLeavesTheCurrentOneUntouched(): void |
| 44 | + { |
| 45 | + $sut = new AlgorithmManager([new FooAlgorithm()]); |
| 46 | + |
| 47 | + $new = $sut->with(new BarAlgorithm()); |
| 48 | + |
| 49 | + static::assertNotSame($sut, $new); |
| 50 | + static::assertSame(['foo'], $sut->list()); |
| 51 | + static::assertSame(['foo', 'bar'], $new->list()); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function withAcceptsSeveralAlgorithmsAtOnce(): void |
| 56 | + { |
| 57 | + $sut = new AlgorithmManager([]); |
| 58 | + |
| 59 | + $new = $sut->with(new FooAlgorithm(), new BarAlgorithm()); |
| 60 | + |
| 61 | + static::assertSame(['foo', 'bar'], $new->list()); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function withReplacesAnAlgorithmThatHasTheSameName(): void |
| 66 | + { |
| 67 | + $replacement = new FooAlgorithm(); |
| 68 | + $sut = new AlgorithmManager([new FooAlgorithm()]); |
| 69 | + |
| 70 | + $new = $sut->with($replacement); |
| 71 | + |
| 72 | + static::assertSame(['foo'], $new->list()); |
| 73 | + static::assertSame($replacement, $new->get('foo')); |
| 74 | + } |
| 75 | + |
| 76 | + #[Test] |
| 77 | + public function withDoesNotTriggerAnyDeprecation(): void |
| 78 | + { |
| 79 | + $sut = new AlgorithmManager([new FooAlgorithm()]); |
| 80 | + |
| 81 | + $deprecations = $this->collectDeprecations(static function () use ($sut): void { |
| 82 | + $sut->with(new BarAlgorithm()); |
| 83 | + }); |
| 84 | + |
| 85 | + static::assertSame([], $deprecations); |
| 86 | + } |
| 87 | + |
| 88 | + #[Test] |
| 89 | + public function theAddMethodIsDeprecatedButStillMutatesTheManager(): void |
| 90 | + { |
| 91 | + $sut = new AlgorithmManager([new FooAlgorithm()]); |
| 92 | + $algorithm = new BarAlgorithm(); |
| 93 | + |
| 94 | + $deprecations = $this->collectDeprecations(static function () use ($sut, $algorithm): void { |
| 95 | + $sut->add($algorithm); |
| 96 | + }); |
| 97 | + |
| 98 | + static::assertCount(1, $deprecations); |
| 99 | + static::assertStringContainsString( |
| 100 | + 'The method "Jose\Component\Core\AlgorithmManager::add()" is deprecated and will be removed in 5.0.0.', |
| 101 | + $deprecations[0] |
| 102 | + ); |
| 103 | + static::assertSame($algorithm, $sut->get('bar')); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param callable(): void $callback |
| 108 | + * |
| 109 | + * @return list<string> |
| 110 | + */ |
| 111 | + private function collectDeprecations(callable $callback): array |
| 112 | + { |
| 113 | + $deprecations = []; |
| 114 | + set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool { |
| 115 | + $deprecations[] = $errstr; |
| 116 | + |
| 117 | + return true; |
| 118 | + }, E_USER_DEPRECATED); |
| 119 | + |
| 120 | + try { |
| 121 | + $callback(); |
| 122 | + } finally { |
| 123 | + restore_error_handler(); |
| 124 | + } |
| 125 | + |
| 126 | + return $deprecations; |
| 127 | + } |
| 128 | +} |
0 commit comments