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

NoneSignatureTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 3
c 3
b 1
f 0
lcom 1
cbo 9
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testNoneSignAndVerifyAlgorithm() 0 14 1
A testInvalidKey() 0 11 1
B testNoneSignAndVerifyComplete() 0 26 1
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\Signature\None;
13
use Jose\Factory\SignerFactory;
14
use Jose\Loader;
15
use Jose\Object\JWK;
16
use Jose\Object\JWSInterface;
17
use Jose\Test\TestCase;
18
19
/**
20
 * Class NoneSignatureTest.
21
 *
22
 * @group None
23
 * @group Unit
24
 */
25
class NoneSignatureTest extends TestCase
26
{
27
    public function testNoneSignAndVerifyAlgorithm()
28
    {
29
        $key = new JWK([
30
            'kty' => 'none',
31
        ]);
32
33
        $none = new None();
34
        $data = 'Live long and Prosper.';
35
36
        $signature = $none->sign($key, $data);
37
38
        $this->assertEquals($signature, '');
39
        $this->assertTrue($none->verify($key, $data, $signature));
40
    }
41
42
    /**
43
     * @expectedException \InvalidArgumentException
44
     * @expectedExceptionMessage Wrong key type.
45
     */
46
    public function testInvalidKey()
47
    {
48
        $key = new JWK([
49
            'kty' => 'EC',
50
        ]);
51
52
        $none = new None();
53
        $data = 'Live long and Prosper.';
54
55
        $none->sign($key, $data);
56
    }
57
58
    public function testNoneSignAndVerifyComplete()
59
    {
60
        $jwk = new JWK([
61
            'kty' => 'none',
62
        ]);
63
64
        $jws = \Jose\Factory\JWSFactory::createJWS('Live long and Prosper.');
65
        $jws = $jws->addSignature($jwk, ['alg' => 'none']);
66
67
        $signer = SignerFactory::createSigner(['none']);
68
        $signer->sign($jws);
69
70
        $this->assertEquals(1, $jws->countSignatures());
71
72
        $compact = $jws->toCompactJSON(0);
73
        $this->assertTrue(is_string($compact));
74
75
        $result = Loader::load($compact);
76
77
        $this->assertInstanceOf(JWSInterface::class, $result);
78
79
        $this->assertEquals('Live long and Prosper.', $result->getPayload());
80
        $this->assertEquals(1, $result->countSignatures());
0 ignored issues
show
Bug introduced by
The method countSignatures does only exist in Jose\Object\JWSInterface, but not in Jose\Object\JWEInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
81
        $this->assertTrue($result->getSignature(0)->hasProtectedHeader('alg'));
0 ignored issues
show
Bug introduced by
The method getSignature does only exist in Jose\Object\JWSInterface, but not in Jose\Object\JWEInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
        $this->assertEquals('none', $result->getSignature(0)->getProtectedHeader('alg'));
83
    }
84
}
85