Passed
Push — master ( 15bdaa...c2dfbc )
by Saulius
10:02
created

CryptTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 32.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 19
loc 58
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper;
14
15
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
16
use PHPUnit\Framework\TestCase;
17
18
class CryptTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function should_encode_and_decode_data()
24
    {
25
26
        $data = [
27
            'custom' => 'data',
28
            'collection' => [
29
                'a' => 'b',
30
                'c' => 'd',
31
            ],
32
            'float' => 5.669
33
        ];
34
35
        $hash = data_encrypt($data, 'test');
36
37
        $this->assertTrue(\is_string($hash));
38
39
        $this->assertSame($data, data_decrypt($hash, 'test'));
40
41
        $this->assertArrayHasKey('collection', $data);
42
    }
43
44
    /**
45
     * @test
46
     *
47
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
48
     * @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException
49
     */
50
    public function should_fail_to_decode_data_with_wrong_key()
51
    {
52
        $this->expectException(WrongKeyOrModifiedCiphertextException::class);
53
54
        $data = "test data";
55
56
        $hash = data_encrypt($data, 'testkey');
57
58
        data_decrypt($hash, 'mykey');
59
    }
60
61
    /**
62
     * @test
63
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
64
     * @throws WrongKeyOrModifiedCiphertextException
65
     */
66
    public function should_fail_to_decode_data_with_modified_cypher_text()
67
    {
68
        $this->expectException(WrongKeyOrModifiedCiphertextException::class);
69
        $data = "test data";
70
71
        $hash = data_encrypt($data, 'testkey') . 'HgtQQQ';
72
73
        data_decrypt($hash, 'testkey');
74
    }
75
}
76