Completed
Push — master ( 23cd7e...7877b2 )
by Francesco
02:54
created

EncryptionWrapperTest::testDecryptWithKeyFileThrowsException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mes\Security\CryptoBundle\Tests;
13
14
use Defuse\Crypto\Exception\CryptoException as BaseCryptoException;
15
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
16
use Mes\Security\CryptoBundle\EncryptionWrapper;
17
use Mes\Security\CryptoBundle\Exception\CryptoException;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
/**
22
 * Class EncryptionWrapperTest.
23
 */
24
class EncryptionWrapperTest extends TestCase
25
{
26
    /**
27
     * @var \Mes\Security\CryptoBundle\EncryptionWrapper
28
     */
29
    private $wrapper;
30
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject
33
     */
34
    private $encryption;
35
36
    protected function setUp()
37
    {
38
        $this->encryption = $this->getMockBuilder('Mes\Security\CryptoBundle\EncryptionInterface')
39
                                 ->getMock();
40
        $this->wrapper = new EncryptionWrapper($this->encryption);
41
    }
42
43
    protected function tearDown()
44
    {
45
        $this->wrapper = null;
46
        $this->encryption = null;
47
    }
48
49
    /**
50
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
51
     */
52 View Code Duplication
    public function testEncryptWithKeyEncryptsPlaintext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $this->encryption->expects($this->once())
55
                         ->method('encryptWithKey')
56
                         ->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
57
                         ->will($this->returnValue('ThisIsACipherText'));
58
59
        $ciphertext = $this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
60
61
        $this->assertTrue(ctype_print($ciphertext), 'is printable');
62
    }
63
64
    /**
65
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
66
     */
67 View Code Duplication
    public function testEncryptWithKeyEncryptsPlaintextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        try {
70
            $this->encryption->expects($this->once())
71
                             ->method('encryptWithKey')
72
                             ->with('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
73
                             ->will($this->throwException(new EnvironmentIsBrokenException()));
74
        } catch (EnvironmentIsBrokenException $e) {
75
            $this->throwException(new CryptoException());
76
        }
77
78
        $this->wrapper->encryptWithKey('The quick brown fox jumps over the lazy dog', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
79
    }
80
81
    /**
82
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
83
     */
84 View Code Duplication
    public function testDecryptWithKeyDecryptsCiphertext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $this->encryption->expects($this->once())
87
                         ->method('decryptWithKey')
88
                         ->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
89
                         ->will($this->returnValue('The quick brown fox jumps over the lazy dog'));
90
91
        $decryptedText = $this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
92
93
        $this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText);
94
    }
95
96
    /**
97
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
98
     */
99 View Code Duplication
    public function testDecryptWithKeyDecryptsCiphertextThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        try {
102
            $this->encryption->expects($this->once())
103
                             ->method('decryptWithKey')
104
                             ->with('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())
105
                             ->will($this->throwException(new BaseCryptoException()));
106
        } catch (BaseCryptoException $e) {
107
            $this->throwException(new CryptoException());
108
        }
109
110
        $this->wrapper->decryptWithKey('ThisIsACipherText', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
111
    }
112
113
    /**
114
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
115
     */
116
    public function testEncryptWithKeyFileEncryptsFile()
117
    {
118
        $this->encryption->expects($this->once())
119
                         ->method('encryptFileWithKey')
120
                         ->will($this->returnCallback(function ($input, $output) {
121
                             $fs = new Filesystem();
122
                             $fs->dumpFile($output, '');
123
                         }));
124
125
        $this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
126
127
        $this->assertFileExists(__DIR__.'/file.crypto');
128
129
        unlink(__DIR__.'/file.crypto');
130
    }
131
132
    /**
133
     * @throws \Mes\Security\CryptoBundle\Exception\CryptoException
134
     */
135
    public function testDecryptWithKeyFileDecryptsEncryptedFile()
136
    {
137
        $this->encryption->expects($this->once())
138
                         ->method('decryptFileWithKey')
139
                         ->will($this->returnCallback(function ($input, $output) {
140
                             $fs = new Filesystem();
141
                             $fs->dumpFile($output, 'Plain text');
142
                         }));
143
144
        $this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
145
146
        $this->assertFileExists(__DIR__.'/file.txt');
147
        $this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt'));
148
149
        unlink(__DIR__.'/file.txt');
150
    }
151
152
    /**
153
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
154
     */
155 View Code Duplication
    public function testEncryptWithKeyFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        try {
158
            $this->encryption->expects($this->once())
159
                             ->method('encryptFileWithKey')
160
                             ->will($this->throwException(new BaseCryptoException()));
161
        } catch (BaseCryptoException $e) {
162
            $this->throwException(new CryptoException());
163
        }
164
165
        $this->wrapper->encryptFileWithKey(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
166
    }
167
168
    /**
169
     * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException
170
     */
171 View Code Duplication
    public function testDecryptWithKeyFileThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        try {
174
            $this->encryption->expects($this->once())
175
                             ->method('decryptFileWithKey')
176
                             ->will($this->throwException(new BaseCryptoException()));
177
        } catch (BaseCryptoException $e) {
178
            $this->throwException(new CryptoException());
179
        }
180
181
        $this->wrapper->decryptFileWithKey(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock());
182
    }
183
}
184