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\KeyStorage; |
||
13 | |||
14 | use Defuse\Crypto\KeyProtectedByPassword; |
||
15 | use Mes\Security\CryptoBundle\KeyStorage\KeyStorage; |
||
16 | use Mes\Security\CryptoBundle\KeyStorage\KeyStorageInterface; |
||
17 | use Mes\Security\CryptoBundle\Model\Key; |
||
18 | use PHPUnit\Framework\TestCase; |
||
19 | |||
20 | /** |
||
21 | * Class KeyStorageTest. |
||
22 | */ |
||
23 | class KeyStorageTest extends TestCase |
||
24 | { |
||
25 | /** |
||
26 | * @var KeyStorageInterface |
||
27 | */ |
||
28 | private $keyStorage; |
||
29 | |||
30 | public function testSetKeyStoresKey() |
||
31 | { |
||
32 | $secret = 'ThisIsASecret'; |
||
33 | $key = Key::create(KeyProtectedByPassword::createRandomPasswordProtectedKey($secret), $secret); |
||
34 | |||
35 | $this->keyStorage->setKey($key); |
||
36 | |||
37 | $this->assertInstanceOf('Defuse\Crypto\KeyProtectedByPassword', $this->keyStorage->getKey() |
||
0 ignored issues
–
show
|
|||
38 | ->getRawKey()); |
||
39 | $this->assertSame($secret, $this->keyStorage->getKey() |
||
40 | ->getSecret()); |
||
41 | } |
||
42 | |||
43 | protected function setUp() |
||
44 | { |
||
45 | $this->keyStorage = new KeyStorage(); |
||
46 | } |
||
47 | |||
48 | protected function tearDown() |
||
49 | { |
||
50 | $this->keyStorage = null; |
||
51 | } |
||
52 | } |
||
53 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: