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

Tests/KeyManagerTest.php (2 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;
13
14
use Mes\Security\CryptoBundle\KeyManager;
15
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16
17
/**
18
 * Class KeyManagerTest.
19
 */
20
class KeyManagerTest extends TestCase
21
{
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockObject
24
     */
25
    private $keyStorage;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $keyGenerator;
31
32
    /**
33
     * @var \Mes\Security\CryptoBundle\KeyManager
34
     */
35
    private $keyManager;
36
37 View Code Duplication
    public function testGenerateCreatesKey()
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...
38
    {
39
        $this->keyGenerator->expects($this->once())
40
                           ->method('generate')
41
                           ->with(null)
42
                           ->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
43
                                                          ->getMock()));
44
45
        $key = $this->keyManager->generate();
46
47
        $this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key);
48
    }
49
50 View Code Duplication
    public function testGeneratedFromAsciiCreatesKeyFromAscii()
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...
51
    {
52
        $key_encoded = 'key_encoded';
53
        $secret = 'ThisIsASecretPassword';
54
55
        $this->keyGenerator->expects($this->once())
56
                           ->method('generateFromAscii')
57
                           ->with($key_encoded, $secret)
58
                           ->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
59
                                                          ->getMock()));
60
61
        $key = $this->keyManager->generateFromAscii($key_encoded, $secret);
62
63
        $this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key);
64
    }
65
66
    public function testGetKeyReadsNotExistingKey()
67
    {
68
        $this->keyStorage->expects($this->once())
69
                         ->method('getKey')
70
                         ->will($this->returnValue(null));
71
72
        $this->keyGenerator->expects($this->once())
73
                           ->method('generate')
74
                           ->with(null)
75
                           ->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
76
                                                          ->getMock()));
77
78
        $this->keyStorage->expects($this->once())
79
                         ->method('setKey');
80
81
        $key = $this->keyManager->getKey();
82
83
        $this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key);
84
    }
85
86
    public function testGetKeyReadsExistingKey()
87
    {
88
        $this->keyStorage->expects($this->never())
89
                         ->method('setKey');
90
91
        $this->keyGenerator->expects($this->never())
92
                           ->method('generate');
93
94
        $this->keyStorage->expects($this->once())
95
                         ->method('getKey')
96
                         ->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
97
                                                        ->getMock()));
98
99
        $key = $this->keyManager->getKey();
100
101
        $this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key);
102
    }
103
104
    public function testSetKeyStoresKey()
105
    {
106
        $this->keyStorage->expects($this->once())
107
                         ->method('setKey')
108
                         ->with($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
109
                                     ->getMock());
110
111
        $this->keyManager->setKey($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')
112
                                       ->getMock());
113
    }
114
115
    public function testSetSecretStoreSecret()
116
    {
117
        $this->keyManager->setSecret('ThisIsASecret');
118
119
        $this->assertSame('ThisIsASecret', $this->keyManager->getSecret());
120
    }
121
122
    protected function setUp()
123
    {
124
        $this->keyStorage = $this->getMockBuilder('Mes\Security\CryptoBundle\KeyStorage\KeyStorageInterface')
125
                                 ->getMock();
126
127
        $this->keyGenerator = $this->getMockBuilder('Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface')
128
                                   ->getMock();
129
130
        $this->keyManager = new KeyManager($this->keyStorage, $this->keyGenerator);
131
    }
132
133
    protected function tearDown()
134
    {
135
        $this->keyGenerator = null;
136
        $this->keyStorage = null;
137
        $this->keyManager = null;
138
    }
139
}
140