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\DependencyInjection; |
||
13 | |||
14 | use Mes\Security\CryptoBundle\DependencyInjection\MesCryptoExtension; |
||
15 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
16 | use Symfony\Component\Yaml\Parser; |
||
17 | |||
18 | /** |
||
19 | * Class MesCryptoExtensionTest. |
||
20 | */ |
||
21 | class MesCryptoExtensionTest extends \PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | /** @var ContainerBuilder */ |
||
24 | private $configuration; |
||
25 | |||
26 | protected function setup() |
||
27 | { |
||
28 | $this->configuration = new ContainerBuilder(); |
||
29 | $this->configuration->setParameter('kernel.root_dir', dirname(dirname(__DIR__))); |
||
30 | } |
||
31 | |||
32 | protected function tearDown() |
||
33 | { |
||
34 | unset($this->configuration); |
||
35 | } |
||
36 | |||
37 | View Code Duplication | public function testContainerWithDefaultValues() |
|
38 | { |
||
39 | $loader = new MesCryptoExtension(); |
||
40 | $config = $this->getEmptyConfig(); |
||
41 | $loader->load(array($config), $this->configuration); |
||
42 | |||
43 | $this->assertHasDefinition('mes_crypto.raw_key'); |
||
44 | $this->assertSame('Defuse\Crypto\Key', $this->configuration->findDefinition('mes_crypto.raw_key') |
||
45 | ->getClass(), 'Defuse\Crypto\Key class is correct'); |
||
46 | |||
47 | $this->assertNotHasDefinition('mes_crypto.loader'); |
||
48 | } |
||
49 | |||
50 | View Code Duplication | public function testContainerWithRandomKeyAndSecret() |
|
51 | { |
||
52 | $loader = new MesCryptoExtension(); |
||
53 | $config = $this->getConfigWithRandomKeyAndSecret(); |
||
54 | $loader->load(array($config), $this->configuration); |
||
55 | |||
56 | $this->assertHasDefinition('mes_crypto.raw_key'); |
||
57 | $this->assertSame('Defuse\Crypto\KeyProtectedByPassword', $this->configuration->findDefinition('mes_crypto.raw_key') |
||
58 | ->getClass(), 'KeyProtectedByPassword class is correct'); |
||
59 | |||
60 | $this->assertNotHasDefinition('mes_crypto.loader'); |
||
61 | } |
||
62 | |||
63 | View Code Duplication | public function testContainerWithInternalKeyAndSecret() |
|
64 | { |
||
65 | $loader = new MesCryptoExtension(); |
||
66 | $config = $this->getConfigWithInternalKeyAndSecret(); |
||
67 | $loader->load(array($config), $this->configuration); |
||
68 | |||
69 | $this->assertHasDefinition('mes_crypto.raw_key'); |
||
70 | $this->assertSame('Defuse\Crypto\KeyProtectedByPassword', $this->configuration->findDefinition('mes_crypto.raw_key') |
||
71 | ->getClass(), 'Defuse\Crypto\KeyProtectedByPassword class is correct'); |
||
72 | $this->assertNotHasDefinition('mes_crypto.loader'); |
||
73 | } |
||
74 | |||
75 | View Code Duplication | public function testContainerWithExternalKeyAndSecret() |
|
0 ignored issues
–
show
|
|||
76 | { |
||
77 | $loader = new MesCryptoExtension(); |
||
78 | $config = $this->getConfigWithExternalKey(); |
||
79 | $loader->load(array($config), $this->configuration); |
||
80 | |||
81 | $this->assertHasDefinition('mes_crypto.raw_key'); |
||
82 | $this->assertSame('Defuse\Crypto\KeyProtectedByPassword', $this->configuration->findDefinition('mes_crypto.raw_key') |
||
83 | ->getClass(), 'Defuse\Crypto\KeyProtectedByPassword class is correct'); |
||
84 | $this->assertHasDefinition('mes_crypto.loader'); |
||
85 | } |
||
86 | |||
87 | public function testContainerWithFullConfigWithExternalKey() |
||
88 | { |
||
89 | $loader = new MesCryptoExtension(); |
||
90 | $config = $this->getFullConfigWithExternalKey(); |
||
91 | $loader->load(array($config), $this->configuration); |
||
92 | |||
93 | $this->assertHasDefinition('mes_crypto.raw_key'); |
||
94 | $this->assertSame('Defuse\Crypto\KeyProtectedByPassword', $this->configuration->findDefinition('mes_crypto.raw_key') |
||
95 | ->getClass(), 'Defuse\Crypto\KeyProtectedByPassword class is correct'); |
||
96 | $this->assertHasDefinition('mes_crypto.loader'); |
||
97 | |||
98 | $this->assertSame('custom_key_storage_service', (string) $this->configuration->getAlias('mes_crypto.key_storage'), 'custom_key_storage_service is correct alias'); |
||
99 | $this->assertSame('custom_key_generator_service', (string) $this->configuration->getAlias('mes_crypto.key_generator'), 'custom_key_generator_service is correct alias'); |
||
100 | $this->assertSame('custom_encryption_service', (string) $this->configuration->getAlias('mes_crypto.encryption'), 'custom_encryption_service is correct alias'); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | private function getEmptyConfig() |
||
107 | { |
||
108 | return array(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return mixed |
||
113 | */ |
||
114 | private function getConfigWithRandomKeyAndSecret() |
||
115 | { |
||
116 | $yaml = <<<'EOF' |
||
117 | secret: "ThisIsASecret" |
||
118 | EOF; |
||
119 | |||
120 | $parser = new Parser(); |
||
121 | |||
122 | return $parser->parse($yaml); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return mixed |
||
127 | */ |
||
128 | private function getConfigWithInternalKeyAndSecret() |
||
129 | { |
||
130 | $yaml = <<<'EOF' |
||
131 | key: "ThisIsEncodedKey" |
||
132 | secret: "ThisIsASecret" |
||
133 | EOF; |
||
134 | |||
135 | $parser = new Parser(); |
||
136 | |||
137 | return $parser->parse($yaml); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return mixed |
||
142 | */ |
||
143 | View Code Duplication | private function getConfigWithExternalKey() |
|
144 | { |
||
145 | $cryptoFile = dirname(__DIR__).'/key.crypto'; |
||
146 | $yaml = <<<EOF |
||
147 | key: $cryptoFile |
||
148 | external_secret: true |
||
149 | EOF; |
||
150 | |||
151 | $parser = new Parser(); |
||
152 | |||
153 | return $parser->parse($yaml); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return mixed |
||
158 | */ |
||
159 | View Code Duplication | private function getFullConfigWithExternalKey() |
|
160 | { |
||
161 | $cryptoFile = dirname(__DIR__).'/key.crypto'; |
||
162 | $yaml = <<<EOF |
||
163 | key: $cryptoFile |
||
164 | external_secret: true |
||
165 | key_storage: custom_key_storage_service |
||
166 | key_generator: custom_key_generator_service |
||
167 | encryption: custom_encryption_service |
||
168 | EOF; |
||
169 | |||
170 | $parser = new Parser(); |
||
171 | |||
172 | return $parser->parse($yaml); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @return mixed |
||
177 | */ |
||
178 | private function getConfigWithExternalLoader() |
||
179 | { |
||
180 | $yaml = <<<'EOF' |
||
181 | loader: |
||
182 | enabled; true |
||
183 | EOF; |
||
184 | |||
185 | $parser = new Parser(); |
||
186 | |||
187 | return $parser->parse($yaml); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $id |
||
192 | */ |
||
193 | private function assertHasDefinition($id) |
||
194 | { |
||
195 | $this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id))); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string $id |
||
200 | */ |
||
201 | private function assertNotHasDefinition($id) |
||
202 | { |
||
203 | $this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id))); |
||
204 | } |
||
205 | } |
||
206 |
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.