Carteni /
crypto-bundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 Symfony\Component\Filesystem\Filesystem; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class EncryptionWrapperTest. |
||
| 22 | */ |
||
| 23 | class EncryptionWrapperTest extends \PHPUnit_Framework_TestCase |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var \Mes\Security\CryptoBundle\EncryptionWrapper |
||
| 27 | */ |
||
| 28 | private $wrapper; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 32 | */ |
||
| 33 | private $encryption; |
||
| 34 | |||
| 35 | public function testEncryptEncryptsPlaintext() |
||
| 36 | { |
||
| 37 | $this->encryption->expects($this->once()) |
||
| 38 | ->method('encrypt') |
||
| 39 | ->with('The quick brown fox jumps over the lazy dog', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')) |
||
| 40 | ->will($this->returnValue('ThisIsACipherText')); |
||
| 41 | |||
| 42 | $ciphertext = $this->wrapper->encrypt('The quick brown fox jumps over the lazy dog', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
| 43 | |||
| 44 | $this->assertTrue(ctype_print($ciphertext), 'is printable'); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function testEncryptEncryptsPlaintextThrowsException() |
|
| 51 | { |
||
| 52 | try { |
||
| 53 | $this->encryption->expects($this->once()) |
||
| 54 | ->method('encrypt') |
||
| 55 | ->with('The quick brown fox jumps over the lazy dog', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')) |
||
| 56 | ->will($this->throwException(new EnvironmentIsBrokenException())); |
||
| 57 | } catch (EnvironmentIsBrokenException $e) { |
||
| 58 | $this->throwException(new CryptoException()); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->wrapper->encrypt('The quick brown fox jumps over the lazy dog', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testDecryptDecryptsCiphertext() |
||
| 65 | { |
||
| 66 | $this->encryption->expects($this->once()) |
||
| 67 | ->method('decrypt') |
||
| 68 | ->with('ThisIsACipherText', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')) |
||
| 69 | ->will($this->returnValue('The quick brown fox jumps over the lazy dog')); |
||
| 70 | |||
| 71 | $decryptedText = $this->wrapper->decrypt('ThisIsACipherText', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
| 72 | |||
| 73 | $this->assertSame('The quick brown fox jumps over the lazy dog', $decryptedText); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function testDecryptDecryptsCiphertextThrowsException() |
|
| 80 | { |
||
| 81 | try { |
||
| 82 | $this->encryption->expects($this->once()) |
||
| 83 | ->method('decrypt') |
||
| 84 | ->with('ThisIsACipherText', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')) |
||
| 85 | ->will($this->throwException(new BaseCryptoException())); |
||
| 86 | } catch (BaseCryptoException $e) { |
||
| 87 | $this->throwException(new CryptoException()); |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->wrapper->decrypt('ThisIsACipherText', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testEncryptFileEncryptsFile() |
||
| 94 | { |
||
| 95 | $this->encryption->expects($this->once()) |
||
| 96 | ->method('encryptFile') |
||
| 97 | ->will($this->returnCallback(function ($input, $output) { |
||
| 98 | $fs = new Filesystem(); |
||
| 99 | $fs->dumpFile($output, ''); |
||
| 100 | })); |
||
| 101 | |||
| 102 | $this->wrapper->encryptFile(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
|
0 ignored issues
–
show
|
|||
| 103 | |||
| 104 | $this->assertFileExists(__DIR__.'/file.crypto'); |
||
| 105 | |||
| 106 | unlink(__DIR__.'/file.crypto'); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testDecryptFileDecryptsEncryptedFile() |
||
| 110 | { |
||
| 111 | $this->encryption->expects($this->once()) |
||
| 112 | ->method('decryptFile') |
||
| 113 | ->will($this->returnCallback(function ($input, $output) { |
||
| 114 | $fs = new Filesystem(); |
||
| 115 | $fs->dumpFile($output, 'Plain text'); |
||
| 116 | })); |
||
| 117 | |||
| 118 | $this->wrapper->decryptFile(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
|
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 119 | |||
| 120 | $this->assertFileExists(__DIR__.'/file.txt'); |
||
| 121 | $this->assertContains('Plain text', file_get_contents(__DIR__.'/file.txt')); |
||
| 122 | |||
| 123 | unlink(__DIR__.'/file.txt'); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function testEncryptFileThrowsException() |
|
|
0 ignored issues
–
show
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...
|
|||
| 130 | { |
||
| 131 | try { |
||
| 132 | $this->encryption->expects($this->once()) |
||
| 133 | ->method('encryptFile') |
||
| 134 | ->will($this->throwException(new BaseCryptoException())); |
||
| 135 | } catch (BaseCryptoException $e) { |
||
| 136 | $this->throwException(new CryptoException()); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->wrapper->encryptFile(__DIR__.'/file.txt', __DIR__.'/file.crypto', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
|
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @expectedException \Mes\Security\CryptoBundle\Exception\CryptoException |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function testDecryptFileThrowsException() |
|
|
0 ignored issues
–
show
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...
|
|||
| 146 | { |
||
| 147 | try { |
||
| 148 | $this->encryption->expects($this->once()) |
||
| 149 | ->method('decryptFile') |
||
| 150 | ->will($this->throwException(new BaseCryptoException())); |
||
| 151 | } catch (BaseCryptoException $e) { |
||
| 152 | $this->throwException(new CryptoException()); |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->wrapper->decryptFile(__DIR__.'/file.crypto', __DIR__.'/file.txt', $this->getMock('Mes\Security\CryptoBundle\Model\KeyInterface')); |
||
|
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. Loading history...
|
|||
| 156 | } |
||
| 157 | |||
| 158 | protected function setUp() |
||
| 159 | { |
||
| 160 | $this->encryption = $this->getMockBuilder('Mes\Security\CryptoBundle\EncryptionInterface') |
||
| 161 | ->getMock(); |
||
| 162 | $this->wrapper = new EncryptionWrapper($this->encryption); |
||
| 163 | } |
||
| 164 | |||
| 165 | protected function tearDown() |
||
| 166 | { |
||
| 167 | $this->wrapper = null; |
||
| 168 | $this->encryption = null; |
||
| 169 | } |
||
| 170 | } |
||
| 171 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.