|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\Signature; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use ProxyManager\Signature\Exception\InvalidSignatureException; |
|
10
|
|
|
use ProxyManager\Signature\Exception\MissingSignatureException; |
|
11
|
|
|
use ProxyManager\Signature\SignatureChecker; |
|
12
|
|
|
use ProxyManager\Signature\SignatureGeneratorInterface; |
|
13
|
|
|
use ReflectionClass; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Tests for {@see \ProxyManager\Signature\SignatureChecker} |
|
17
|
|
|
* |
|
18
|
|
|
* @covers \ProxyManager\Signature\SignatureChecker |
|
19
|
|
|
* @group Coverage |
|
20
|
|
|
*/ |
|
21
|
|
|
final class SignatureCheckerTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
private string $signatureExample = 'valid-signature'; |
|
|
|
|
|
|
24
|
|
|
private SignatureChecker $signatureChecker; |
|
25
|
|
|
|
|
26
|
|
|
/** @var SignatureGeneratorInterface&MockObject */ |
|
27
|
|
|
private SignatureGeneratorInterface $signatureGenerator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritDoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function setUp() : void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->signatureGenerator = $this->createMock(SignatureGeneratorInterface::class); |
|
35
|
|
|
$this->signatureChecker = new SignatureChecker($this->signatureGenerator); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testCheckSignatureWithValidKey() : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this |
|
41
|
|
|
->signatureGenerator |
|
42
|
|
|
->expects(self::atLeastOnce()) |
|
43
|
|
|
->method('generateSignatureKey') |
|
44
|
|
|
->with(['foo' => 'bar']) |
|
45
|
|
|
->willReturn('Example'); |
|
46
|
|
|
$this |
|
47
|
|
|
->signatureGenerator |
|
48
|
|
|
->expects(self::atLeastOnce()) |
|
49
|
|
|
->method('generateSignature') |
|
50
|
|
|
->with(['foo' => 'bar']) |
|
51
|
|
|
->willReturn('valid-signature'); |
|
52
|
|
|
|
|
53
|
|
|
$this->signatureChecker->checkSignature(new ReflectionClass($this), ['foo' => 'bar']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testCheckSignatureWithInvalidKey() : void |
|
57
|
|
|
{ |
|
58
|
|
|
$this |
|
59
|
|
|
->signatureGenerator |
|
60
|
|
|
|
|
61
|
|
|
->method('generateSignatureKey') |
|
62
|
|
|
->with(['foo' => 'bar']) |
|
63
|
|
|
->willReturn('InvalidKey'); |
|
64
|
|
|
$this |
|
65
|
|
|
->signatureGenerator |
|
66
|
|
|
->method('generateSignature') |
|
67
|
|
|
->with(['foo' => 'bar']) |
|
68
|
|
|
->willReturn('valid-signature'); |
|
69
|
|
|
|
|
70
|
|
|
$this->expectException(MissingSignatureException::class); |
|
71
|
|
|
|
|
72
|
|
|
$this->signatureChecker->checkSignature(new ReflectionClass($this), ['foo' => 'bar']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testCheckSignatureWithInvalidValue() : void |
|
76
|
|
|
{ |
|
77
|
|
|
$this |
|
78
|
|
|
->signatureGenerator |
|
79
|
|
|
->method('generateSignatureKey') |
|
80
|
|
|
->with(['foo' => 'bar']) |
|
81
|
|
|
->willReturn('Example'); |
|
82
|
|
|
$this |
|
83
|
|
|
->signatureGenerator |
|
84
|
|
|
->method('generateSignature') |
|
85
|
|
|
->with(['foo' => 'bar']) |
|
86
|
|
|
->willReturn('invalid-signature'); |
|
87
|
|
|
|
|
88
|
|
|
$this->expectException(InvalidSignatureException::class); |
|
89
|
|
|
|
|
90
|
|
|
$this->signatureChecker->checkSignature(new ReflectionClass($this), ['foo' => 'bar']); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|