-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCryptographyMiddlewareTest.php
More file actions
63 lines (42 loc) · 2.25 KB
/
Copy pathCryptographyMiddlewareTest.php
File metadata and controls
63 lines (42 loc) · 2.25 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Tests\Unit\Cryptography;
use Patchlevel\Hydrator\Cryptography\CryptographyMiddleware;
use Patchlevel\Hydrator\Cryptography\PayloadCryptographer;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Middleware\Middleware;
use Patchlevel\Hydrator\Middleware\Stack;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
#[CoversClass(CryptographyMiddleware::class)]
final class CryptographyMiddlewareTest extends TestCase
{
public function testHydrate(): void
{
$metadata = new ClassMetadata(new ReflectionClass(stdClass::class));
$payloadCryptographer = $this->createMock(PayloadCryptographer::class);
$payloadCryptographer->expects($this->once())->method('decrypt')->with($metadata, ['name' => 'foo'])->willReturn(['name' => 'bar']);
$object = new stdClass();
$cryptographyMiddleware = new CryptographyMiddleware($payloadCryptographer);
$otherMiddleware = $this->createMock(Middleware::class);
$stack = new Stack([$otherMiddleware]);
$otherMiddleware->expects($this->once())->method('hydrate')->with($metadata, ['name' => 'bar'], [], $stack)->willReturn($object);
$result = $cryptographyMiddleware->hydrate($metadata, ['name' => 'foo'], [], $stack);
self::assertSame($object, $result);
}
public function testExtract(): void
{
$metadata = new ClassMetadata(new ReflectionClass(stdClass::class));
$payloadCryptographer = $this->createMock(PayloadCryptographer::class);
$payloadCryptographer->expects($this->once())->method('encrypt')->with($metadata, ['name' => 'foo'])->willReturn(['name' => 'bar']);
$object = new stdClass();
$cryptographyMiddleware = new CryptographyMiddleware($payloadCryptographer);
$otherMiddleware = $this->createMock(Middleware::class);
$stack = new Stack([$otherMiddleware]);
$otherMiddleware->expects($this->once())->method('extract')->with($metadata, $object, [], $stack)->willReturn(['name' => 'foo']);
$result = $cryptographyMiddleware->extract($metadata, $object, [], $stack);
self::assertSame(['name' => 'bar'], $result);
}
}