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\Loader; |
||
13 | |||
14 | use Mes\Security\CryptoBundle\KeyGenerator\KeyGenerator; |
||
15 | use Mes\Security\CryptoBundle\Loader\CryptoLoader; |
||
16 | use PHPUnit\Framework\TestCase; |
||
17 | |||
18 | /** |
||
19 | * Class CryptoLoaderTest. |
||
20 | */ |
||
21 | class CryptoLoaderTest extends TestCase |
||
22 | { |
||
23 | /** |
||
24 | * @var CryptoLoader |
||
25 | */ |
||
26 | private $loader; |
||
27 | |||
28 | /** |
||
29 | * @var KeyGenerator |
||
30 | */ |
||
31 | private $keyGenerator; |
||
32 | |||
33 | private $tempCryptoFile; |
||
34 | |||
35 | public function testLoadKeyLoadsEncodedKey() |
||
36 | { |
||
37 | $encodedKey = $this->loader->loadKey(); |
||
38 | |||
39 | $key = $this->keyGenerator->generateFromAscii($encodedKey, $this->loader->loadSecret()); |
||
40 | |||
41 | $this->assertInstanceOf('Mes\\Security\\CryptoBundle\\Model\\KeyInterface', $key); |
||
42 | $this->assertTrue(ctype_print($encodedKey), ' is printable'); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @expectedException \UnexpectedValueException |
||
47 | */ |
||
48 | View Code Duplication | public function testLoadKeyThrowsExceptionBecauseKeyIsMissing() |
|
0 ignored issues
–
show
|
|||
49 | { |
||
50 | $this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
||
51 | |||
52 | $handle = fopen($this->tempCryptoFile, 'w+'); |
||
53 | fwrite($handle, <<<'EOF' |
||
54 | secret=ThisIsASecret |
||
55 | EOF |
||
56 | ); |
||
57 | fclose($handle); |
||
58 | |||
59 | $loader = new CryptoLoader($this->tempCryptoFile); |
||
60 | $loader->loadKey(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @expectedException \UnexpectedValueException |
||
65 | */ |
||
66 | View Code Duplication | public function testLoadKeyThrowsExceptionBecauseSecretIsMissing() |
|
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...
|
|||
67 | { |
||
68 | $this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
||
69 | |||
70 | $handle = fopen($this->tempCryptoFile, 'w+'); |
||
71 | fwrite($handle, <<<'EOF' |
||
72 | key=ThisIsAnEncodedKey |
||
73 | EOF |
||
74 | ); |
||
75 | fclose($handle); |
||
76 | |||
77 | $loader = new CryptoLoader($this->tempCryptoFile); |
||
78 | $loader->loadSecret(); |
||
79 | } |
||
80 | |||
81 | public function testLoadSecretLoadsSecret() |
||
82 | { |
||
83 | $secret = $this->loader->loadSecret(); |
||
84 | |||
85 | $this->assertSame('ThisIsASecret', $secret); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @expectedException \InvalidArgumentException |
||
90 | */ |
||
91 | public function test__ConstructFailsBecauseResourceNotExists() |
||
92 | { |
||
93 | $fakeFile = '/var/www/fake'; |
||
94 | |||
95 | new CryptoLoader($fakeFile); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @expectedException \RuntimeException |
||
100 | */ |
||
101 | View Code Duplication | public function test__ConstructFailsBecauseResourceIsBadFormatted() |
|
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...
|
|||
102 | { |
||
103 | $this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
||
104 | |||
105 | $handle = fopen($this->tempCryptoFile, 'w+'); |
||
106 | fwrite($handle, <<<'EOF' |
||
107 | key:ThisIsAKey |
||
108 | secret:ThisIsASecret |
||
109 | EOF |
||
110 | ); |
||
111 | fclose($handle); |
||
112 | |||
113 | new CryptoLoader($this->tempCryptoFile); |
||
114 | } |
||
115 | |||
116 | protected function setUp() |
||
117 | { |
||
118 | $this->loader = new CryptoLoader(__DIR__.'/../key.crypto'); |
||
119 | $this->keyGenerator = new KeyGenerator(); |
||
120 | } |
||
121 | |||
122 | protected function tearDown() |
||
123 | { |
||
124 | $this->loader = null; |
||
125 | $this->keyGenerator = null; |
||
126 | @unlink($this->tempCryptoFile); |
||
127 | } |
||
128 | } |
||
129 |
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.