|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework, SpiralScout LLC. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Spiral\Tests\Encrypter; |
|
12
|
|
|
|
|
13
|
|
|
use Defuse\Crypto\Key; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Core\Container; |
|
16
|
|
|
use Spiral\Encrypter\Config\EncrypterConfig; |
|
17
|
|
|
use Spiral\Encrypter\Encrypter; |
|
18
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
|
19
|
|
|
use Spiral\Encrypter\EncrypterInterface; |
|
20
|
|
|
use Spiral\Encrypter\EncryptionInterface; |
|
21
|
|
|
use Spiral\Encrypter\Exception\EncrypterException; |
|
22
|
|
|
|
|
23
|
|
|
class EncrypterFactoryTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testInjection(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$key = Key::CreateNewRandomKey()->saveToAsciiSafeString(); |
|
28
|
|
|
|
|
29
|
|
|
$container = new Container(); |
|
30
|
|
|
$container->bind(EncrypterInterface::class, Encrypter::class); |
|
31
|
|
|
|
|
32
|
|
|
//Manager must be created automatically |
|
33
|
|
|
$container->bind( |
|
34
|
|
|
EncrypterConfig::class, |
|
35
|
|
|
new EncrypterConfig(['key' => $key]) |
|
|
|
|
|
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertInstanceOf( |
|
39
|
|
|
EncrypterInterface::class, |
|
40
|
|
|
$container->get(EncrypterInterface::class) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf(Encrypter::class, $container->get(EncrypterInterface::class)); |
|
44
|
|
|
|
|
45
|
|
|
$encrypter = $container->get(EncrypterInterface::class); |
|
46
|
|
|
$this->assertSame($key, $encrypter->getKey()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testGetEncrypter(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$key = Key::CreateNewRandomKey()->saveToAsciiSafeString(); |
|
52
|
|
|
|
|
53
|
|
|
$container = new Container(); |
|
54
|
|
|
$container->bind(EncrypterInterface::class, Encrypter::class); |
|
55
|
|
|
$container->bind(EncryptionInterface::class, EncrypterFactory::class); |
|
56
|
|
|
|
|
57
|
|
|
//Manager must be created automatically |
|
58
|
|
|
$container->bind( |
|
59
|
|
|
EncrypterConfig::class, |
|
60
|
|
|
new EncrypterConfig(['key' => $key]) |
|
|
|
|
|
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertInstanceOf( |
|
64
|
|
|
EncryptionInterface::class, |
|
65
|
|
|
$container->get(EncryptionInterface::class) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertInstanceOf( |
|
69
|
|
|
EncrypterFactory::class, |
|
70
|
|
|
$container->get(EncryptionInterface::class) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$encrypter = $container->get(EncryptionInterface::class)->getEncrypter(); |
|
74
|
|
|
$this->assertSame($key, $encrypter->getKey()); |
|
75
|
|
|
$this->assertSame($key, $container->get(EncryptionInterface::class)->getKey()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testExceptionKey(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->expectException(EncrypterException::class); |
|
81
|
|
|
|
|
82
|
|
|
$config = new EncrypterConfig([]); |
|
83
|
|
|
|
|
84
|
|
|
$factory = new EncrypterFactory($config); |
|
85
|
|
|
echo $factory->getKey(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @covers \Spiral\Encrypter\EncrypterFactory::generateKey |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testGenerateKey(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$key = Key::CreateNewRandomKey()->saveToAsciiSafeString(); |
|
94
|
|
|
|
|
95
|
|
|
$manager = new EncrypterFactory(new EncrypterConfig([ |
|
96
|
|
|
'key' => $key, |
|
97
|
|
|
])); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertNotSame($key, $manager->generateKey()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|