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

EncrypterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 28
c 1
b 0
f 0
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testEncryption() 0 17 1
A testBadWithKey() 0 6 1
A testBadKey() 0 5 1
A testBadData() 0 11 1
A testImmutable() 0 9 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\Encrypter\Encrypter;
16
use Spiral\Encrypter\Exception\DecryptException;
17
use Spiral\Encrypter\Exception\EncrypterException;
18
19
class EncrypterTest extends TestCase
20
{
21
    public function testImmutable(): void
22
    {
23
        $encrypter = new Encrypter($keyA = Key::CreateNewRandomKey()->saveToAsciiSafeString());
24
        $new = $encrypter->withKey($keyB = Key::CreateNewRandomKey()->saveToAsciiSafeString());
25
26
        $this->assertNotSame($encrypter, $new);
27
28
        $this->assertEquals($keyA, $encrypter->getKey());
29
        $this->assertEquals($keyB, $new->getKey());
30
    }
31
32
    /**
33
     * @covers \Spiral\Encrypter\Encrypter::encrypt
34
     */
35
    public function testEncryption(): void
36
    {
37
        $encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString());
38
39
        $encrypted = $encrypter->encrypt('test string');
40
        $this->assertNotEquals('test string', $encrypted);
41
        $this->assertEquals('test string', $encrypter->decrypt($encrypted));
42
43
        $encrypter = $encrypter->withKey(Key::CreateNewRandomKey()->saveToAsciiSafeString());
44
45
        $encrypted = $encrypter->encrypt('test string');
46
        $this->assertNotEquals('test string', $encrypted);
47
        $this->assertEquals('test string', $encrypter->decrypt($encrypted));
48
49
        $encrypted = $encrypter->encrypt('test string');
50
        $this->assertNotEquals('test string', $encrypted);
51
        $this->assertEquals('test string', $encrypter->decrypt($encrypted));
52
    }
53
54
    public function testBadData(): void
55
    {
56
        $this->expectException(DecryptException::class);
57
58
        $encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString());
59
60
        $encrypted = $encrypter->encrypt('test string');
61
        $this->assertNotEquals('test string', $encrypted);
62
        $this->assertEquals('test string', $encrypter->decrypt($encrypted));
63
64
        $encrypter->decrypt('badData.' . $encrypted);
65
    }
66
67
    public function testBadKey(): void
68
    {
69
        $this->expectException(EncrypterException::class);
70
71
        $encrypter = new Encrypter('bad-key');
0 ignored issues
show
Unused Code introduced by
The assignment to $encrypter is dead and can be removed.
Loading history...
72
    }
73
74
    public function testBadWithKey(): void
75
    {
76
        $this->expectException(EncrypterException::class);
77
78
        $encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString());
79
        $encrypter = $encrypter->withKey('bad-key');
0 ignored issues
show
Unused Code introduced by
The assignment to $encrypter is dead and can be removed.
Loading history...
80
    }
81
}
82