Skip to content

Commit bffe5df

Browse files
committed
Code Review changes
1 parent 140a501 commit bffe5df

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Tests;
4+
5+
use Tempest\Cryptography\Encryption\EncryptionAlgorithm;
6+
use Tempest\Cryptography\Encryption\EncryptionConfig;
7+
use Tempest\Cryptography\Encryption\EncryptionKey;
8+
use Tempest\Cryptography\Encryption\GenericEncrypter;
9+
use Tempest\Cryptography\Signing\SigningAlgorithm;
10+
use Tempest\Cryptography\Signing\SigningConfig;
11+
use Tempest\DateTime\Duration;
12+
13+
trait CreatesEncrypter
14+
{
15+
use CreatesSigner;
16+
17+
private function createEncrypter(?string $key = null, false|Duration $minimumExecutionDuration = false): GenericEncrypter
18+
{
19+
$key ??= EncryptionKey::generate(EncryptionAlgorithm::AES_256_GCM)->toString();
20+
21+
return new GenericEncrypter(
22+
signer: $this->createSigner(new SigningConfig(
23+
algorithm: SigningAlgorithm::SHA256,
24+
key: $key,
25+
minimumExecutionDuration: $minimumExecutionDuration,
26+
)),
27+
config: new EncryptionConfig(
28+
algorithm: EncryptionAlgorithm::AES_256_GCM,
29+
key: $key,
30+
),
31+
);
32+
}
33+
}

packages/cryptography/tests/Encryption/EncryptionTest.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,16 @@
1212
use Tempest\Cryptography\Encryption\GenericEncrypter;
1313
use Tempest\Cryptography\Signing\SigningAlgorithm;
1414
use Tempest\Cryptography\Signing\SigningConfig;
15+
use Tempest\Cryptography\Tests\CreatesEncrypter;
1516
use Tempest\Cryptography\Tests\CreatesSigner;
1617
use Tempest\Cryptography\Tests\HasMoreIntegerAssertions;
1718
use Tempest\DateTime\Duration;
1819

1920
final class EncryptionTest extends TestCase
2021
{
21-
use CreatesSigner;
22+
use CreatesEncrypter;
2223
use HasMoreIntegerAssertions;
2324

24-
private function createEncrypter(?string $key = null, false|Duration $minimumExecutionDuration = false): GenericEncrypter
25-
{
26-
$key ??= EncryptionKey::generate(EncryptionAlgorithm::AES_256_GCM)->toString();
27-
28-
return new GenericEncrypter(
29-
signer: $this->createSigner(new SigningConfig(
30-
algorithm: SigningAlgorithm::SHA256,
31-
key: $key,
32-
minimumExecutionDuration: $minimumExecutionDuration,
33-
)),
34-
config: new EncryptionConfig(
35-
algorithm: EncryptionAlgorithm::AES_256_GCM,
36-
key: $key,
37-
),
38-
);
39-
}
40-
4125
#[TestWith([''])]
4226
#[TestWith(['sensitive data'])]
4327
#[TestWith(['{"foo":"bar"}'])]

packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public function map(mixed $from, mixed $to): GenericRequest
8282
->to(GenericRequest::class);
8383
}
8484

85-
private function requestMethod(mixed $from, array $data): Method
85+
private function requestMethod(PsrRequest $request, array $data): Method
8686
{
87-
$originalMethod = Method::from($from->getMethod());
87+
$originalMethod = Method::from($request->getMethod());
8888
if ($originalMethod !== Method::POST) {
8989
return $originalMethod;
9090
}

packages/http/tests/Mappers/PsrRequestToGenericRequestMapperTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@
1111
use Psr\Http\Message\ServerRequestInterface;
1212
use ReflectionClass;
1313
use ReflectionMethod;
14-
use Tempest\Cryptography\Encryption\Encrypter;
14+
use Tempest\Cryptography\Tests\CreatesEncrypter;
1515
use Tempest\Http\Mappers\PsrRequestToGenericRequestMapper;
1616
use Tempest\Http\Method;
1717

1818
final class PsrRequestToGenericRequestMapperTest extends TestCase
1919
{
20+
use CreatesEncrypter;
21+
2022
private PsrRequestToGenericRequestMapper $mapper;
21-
private Encrypter $encrypter;
2223
private ReflectionMethod $requestMethod;
2324

2425
protected function setUp(): void
2526
{
2627
parent::setUp();
2728

28-
$this->encrypter = $this->createMock(Encrypter::class);
29-
$this->encrypter->method('decrypt')->willReturnArgument(0);
30-
31-
$this->mapper = new PsrRequestToGenericRequestMapper($this->encrypter);
29+
$this->mapper = new PsrRequestToGenericRequestMapper($this->createEncrypter());
3230

3331
$reflection = new ReflectionClass($this->mapper);
3432
$this->requestMethod = $reflection->getMethod('requestMethod');

0 commit comments

Comments
 (0)