PackagesTrustCheckFailedTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testFromFailedPackageVerificationsWithSinglePackage() 0 20 1
B testFromFailedPackageVerificationsWithMultiplePackages() 0 34 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ComposerGpgVerify\Exception;
6
7
use LogicException;
8
use PHPUnit\Framework\TestCase;
9
use Roave\ComposerGpgVerify\Exception\PackagesTrustCheckFailed;
10
use Roave\ComposerGpgVerify\Package\PackageVerification;
11
12
/**
13
 * @covers \Roave\ComposerGpgVerify\Exception\PackagesTrustCheckFailed
14
 */
15
final class PackagesTrustCheckFailedTest extends TestCase
16
{
17
    public function testFromFailedPackageVerificationsWithSinglePackage() : void
18
    {
19
        /* @var $verification PackageVerification|\PHPUnit_Framework_MockObject_MockObject */
20
        $verification = $this->createMock(PackageVerification::class);
21
22
        $verification->expects(self::any())->method('printReason')->willReturn('foo');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Roave\ComposerGpgVerify\...age\PackageVerification.

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...
23
24
        $exception = PackagesTrustCheckFailed::fromFailedPackageVerifications($verification);
25
26
        self::assertInstanceOf(PackagesTrustCheckFailed::class, $exception);
27
        self::assertInstanceOf(LogicException::class, $exception);
28
        self::assertSame(
29
            <<<'EXPECTED'
30
The following packages need to be signed and verified, or added to exclusions: 
31
foo
32
EXPECTED
33
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
34
            $exception->getMessage()
35
        );
36
    }
37
38
    public function testFromFailedPackageVerificationsWithMultiplePackages() : void
39
    {
40
        /* @var $verification1 PackageVerification|\PHPUnit_Framework_MockObject_MockObject */
41
        $verification1 = $this->createMock(PackageVerification::class);
42
        /* @var $verification2 PackageVerification|\PHPUnit_Framework_MockObject_MockObject */
43
        $verification2 = $this->createMock(PackageVerification::class);
44
        /* @var $verification3 PackageVerification|\PHPUnit_Framework_MockObject_MockObject */
45
        $verification3 = $this->createMock(PackageVerification::class);
46
47
        $verification1->expects(self::any())->method('printReason')->willReturn('foo');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Roave\ComposerGpgVerify\...age\PackageVerification.

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...
48
        $verification2->expects(self::any())->method('printReason')->willReturn('bar');
49
        $verification3->expects(self::any())->method('printReason')->willReturn('baz');
50
51
        $exception = PackagesTrustCheckFailed::fromFailedPackageVerifications(
52
            $verification1,
53
            $verification2,
54
            $verification3
55
        );
56
57
        self::assertInstanceOf(PackagesTrustCheckFailed::class, $exception);
58
        self::assertInstanceOf(LogicException::class, $exception);
59
        self::assertSame(
60
            <<<'EXPECTED'
61
The following packages need to be signed and verified, or added to exclusions: 
62
foo
63
64
bar
65
66
baz
67
EXPECTED
68
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
69
            $exception->getMessage()
70
        );
71
    }
72
}
73