-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLegacyCryptographyDecryptMiddlewareTest.php
More file actions
99 lines (79 loc) · 3.33 KB
/
Copy pathLegacyCryptographyDecryptMiddlewareTest.php
File metadata and controls
99 lines (79 loc) · 3.33 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Tests\Unit\Extension\Cryptography;
use Patchlevel\Hydrator\Cryptography\PayloadCryptographer;
use Patchlevel\Hydrator\Extension\Cryptography\LegacyCryptographyDecryptMiddleware;
use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Middleware\Middleware;
use Patchlevel\Hydrator\Middleware\Stack;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(LegacyCryptographyDecryptMiddleware::class)]
final class LegacyCryptographyDecryptMiddlewareTest extends TestCase
{
public function testHydrateSetsLegacyContextIfDataWasDecrypted(): void
{
$data = ['profileId' => 'foo', 'email' => 'encrypted'];
$processedData = ['profileId' => 'foo', 'email' => 'info@patchlevel.de'];
$metadata = $this->metadata(ProfileCreated::class);
$cryptographer = $this->createMock(PayloadCryptographer::class);
$cryptographer
->expects($this->once())
->method('decrypt')
->with($metadata, $data)
->willReturn($processedData);
$expected = new ProfileCreated(
ProfileId::fromString('foo'),
Email::fromString('info@patchlevel.de'),
);
$otherMiddleware = $this->createMock(Middleware::class);
$stack = new Stack([$otherMiddleware]);
$otherMiddleware
->expects($this->once())
->method('hydrate')
->with(
$metadata,
$processedData,
[LegacyCryptographyDecryptMiddleware::class => true],
$stack,
)
->willReturn($expected);
$middleware = new LegacyCryptographyDecryptMiddleware($cryptographer);
$result = $middleware->hydrate($metadata, $data, [], $stack);
self::assertSame($expected, $result);
}
public function testHydrateDoesNotSetLegacyContextIfDataWasNotDecrypted(): void
{
$data = ['profileId' => 'foo', 'email' => 'info@patchlevel.de'];
$metadata = $this->metadata(ProfileCreated::class);
$cryptographer = $this->createMock(PayloadCryptographer::class);
$cryptographer
->expects($this->once())
->method('decrypt')
->with($metadata, $data)
->willReturn($data);
$expected = new ProfileCreated(
ProfileId::fromString('foo'),
Email::fromString('info@patchlevel.de'),
);
$otherMiddleware = $this->createMock(Middleware::class);
$stack = new Stack([$otherMiddleware]);
$otherMiddleware
->expects($this->once())
->method('hydrate')
->with($metadata, $data, [], $stack)
->willReturn($expected);
$middleware = new LegacyCryptographyDecryptMiddleware($cryptographer);
$result = $middleware->hydrate($metadata, $data, [], $stack);
self::assertSame($expected, $result);
}
/** @param class-string $class */
private function metadata(string $class): ClassMetadata
{
return (new AttributeMetadataFactory())->metadata($class);
}
}