Passed
Push — master ( 21ea66...db5c4e )
by Kirill
02:53
created

EncrypterFactoryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 37
c 1
b 0
f 0
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateKey() 0 9 1
A testGetEncrypter() 0 27 1
A testExceptionKey() 0 8 1
A testInjection() 0 22 1
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])
0 ignored issues
show
Bug introduced by
new Spiral\Encrypter\Con...g(array('key' => $key)) of type Spiral\Encrypter\Config\EncrypterConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            /** @scrutinizer ignore-type */ new EncrypterConfig(['key' => $key])
Loading history...
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])
0 ignored issues
show
Bug introduced by
new Spiral\Encrypter\Con...g(array('key' => $key)) of type Spiral\Encrypter\Config\EncrypterConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            /** @scrutinizer ignore-type */ new EncrypterConfig(['key' => $key])
Loading history...
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