Completed
Push — develop ( 686594...b5844e )
by Florent
03:11
created

JWAManagerTest::testBadArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
use Jose\Algorithm\JWAInterface;
13
use Jose\Algorithm\JWAManager;
14
use Jose\Algorithm\Signature\ES384;
15
use Jose\Factory\AlgorithmManagerFactory;
16
use Jose\Test\TestCase;
17
18
/**
19
 * Class JWAManagerTest.
20
 *
21
 * @group JWA
22
 * @group Unit
23
 */
24
class JWAManagerTest extends TestCase
25
{
26
    public function testAlgorithmIsSupported()
27
    {
28
        $jwa_manager = AlgorithmManagerFactory::createAlgorithmManager(['ES256', 'ES384']);
29
30
        $this->assertTrue($jwa_manager->isAlgorithmSupported('ES256'));
31
        $this->assertTrue($jwa_manager->isAlgorithmSupported('ES384'));
32
33
        $this->assertFalse($jwa_manager->isAlgorithmSupported('ES512'));
34
        $this->assertFalse($jwa_manager->isAlgorithmSupported('HS384'));
35
36
        $this->assertEquals(['ES256', 'ES384'], $jwa_manager->listAlgorithms());
37
        $this->assertInstanceOf(JWAInterface::class, $jwa_manager->getAlgorithm('ES256'));
38
        $this->assertInstanceOf(JWAInterface::class, $jwa_manager->getAlgorithms()['ES256']);
39
40
        $jwa_manager->removeAlgorithm('ES256');
41
        $jwa_manager->removeAlgorithm('ES256');
42
43
        $this->assertNull($jwa_manager->getAlgorithm('ES256'));
44
        $this->assertEquals(['ES384'], $jwa_manager->listAlgorithms());
45
46
        $jwa_manager->removeAlgorithm(new ES384());
0 ignored issues
show
Documentation introduced by
new \Jose\Algorithm\Signature\ES384() is of type object<Jose\Algorithm\Signature\ES384>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
48
        $this->assertNull($jwa_manager->getAlgorithm('HS384'));
49
        $this->assertEquals([], $jwa_manager->listAlgorithms());
50
    }
51
52
    /**
53
     * @expectedException \InvalidArgumentException
54
     * @expectedExceptionMessage Argument must be a string or a JWAInterface object.
55
     */
56
    public function testBadArgument()
57
    {
58
        $jwa_manager = new JWAManager();
59
        $jwa_manager->removeAlgorithm(new \StdClass());
0 ignored issues
show
Documentation introduced by
new \StdClass() is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
    }
61
62
    public function testAllAlgorithms()
63
    {
64
        $algorithms = [
65
            'HS256',
66
            'HS384',
67
            'HS512',
68
            'ES256',
69
            'ES384',
70
            'ES512',
71
            'RS256',
72
            'RS384',
73
            'RS512',
74
            'PS256',
75
            'PS384',
76
            'PS512',
77
            'dir',
78
            'RSA1_5',
79
            'RSA-OAEP',
80
            'RSA-OAEP-256',
81
            'ECDH-ES',
82
            'ECDH-ES+A128KW',
83
            'ECDH-ES+A192KW',
84
            'ECDH-ES+A256KW',
85
            'A128KW',
86
            'A192KW',
87
            'A256KW',
88
            'A128GCMKW',
89
            'A192GCMKW',
90
            'A256GCMKW',
91
            'PBES2-HS256+A128KW',
92
            'PBES2-HS384+A192KW',
93
            'PBES2-HS512+A256KW',
94
            'A128CBC-HS256',
95
            'A192CBC-HS384',
96
            'A256CBC-HS512',
97
            'A128GCM',
98
            'A192GCM',
99
            'A256GCM',
100
        ];
101
        $jwa_manager = AlgorithmManagerFactory::createAlgorithmManager($algorithms);
102
103
        $this->assertEquals($algorithms, $jwa_manager->listAlgorithms());
104
    }
105
}
106