Completed
Push — master ( c93584...7286b1 )
by Francesco
04:37
created

getConfigWithInternalKeyAndSecret()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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