Test Failed
Push — master ( dde0ed...e2b526 )
by Francesco
03:04
created

DependencyInjection/MesCryptoExtensionTest.php (5 issues)

Upgrade to new PHP Analysis Engine

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