Passed
Push — master ( 9251f2...ca8fc4 )
by Herberto
03:14
created

PDependAdapterUnitTest::mine()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
3
namespace Hgraca\Phorensic\Test\Miner\Code\PDepend;
4
5
use Hgraca\Helper\InstanceHelper;
6
use Hgraca\Phorensic\Miner\Code\PDepend\PDependAdapter;
7
use Hgraca\Phorensic\SharedKernel\Port\FileSystem\FileSystemInterface;
8
use Mockery;
9
use Mockery\MockInterface;
10
use PDepend\Engine;
11
use PHPUnit_Framework_TestCase;
12
13
final class PDependAdapterUnitTest extends PHPUnit_Framework_TestCase
14
{
15
    /** @var PDependAdapter */
16
    private $adapter;
17
18
    /** @var MockInterface|FileSystemInterface */
19
    private $fileSystem;
20
21
    /** @var MockInterface|Engine */
22
    private $pdepend;
23
24
    /**
25
     * @before
26
     */
27
    public function setUpAdapter()
28
    {
29
        $this->fileSystem = Mockery::mock(FileSystemInterface::class);
30
        $this->pdepend = Mockery::mock(Engine::class);
31
        $this->adapter = new PDependAdapter($this->pdepend, $this->fileSystem);
32
    }
33
34
    /**
35
     * @test
36
     *
37
     * @small
38
     */
39
    public function mine()
40
    {
41
        $fileA = 'Y/Z/a.php';
42
        $fileB = 'Y/b.php';
43
        $fileC = 'Y/Z/c.php';
44
        $fileD = 'Y/Z/d.php';
45
46
        $this->fileSystem->shouldReceive('readFile')->once()->with('/X/' . $fileA)->andReturn('file that can be handled');
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\Phorensic\SharedK...tem\FileSystemInterface.

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...
47
        $this->fileSystem->shouldReceive('readFile')->once()->with('/X/' . $fileB)->andReturn('file that can NOT be handled class(');
48
        $this->fileSystem->shouldReceive('readFile')->once()->with('/X/' . $fileC)->andReturn('file that can be handled');
49
        $this->fileSystem->shouldReceive('readFile')->once()->with('/X/' . $fileD)->andReturn('file that can be handled');
50
51
        $this->pdepend->shouldReceive('addFile')->once()->with('/X/' . $fileA);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDepend\Engine.

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...
52
        $this->pdepend->shouldNotReceive('addFile')->with('/X/' . $fileB);
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'addFile'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method shouldNotReceive does only exist in Mockery\MockInterface, but not in PDepend\Engine.

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...
53
        $this->pdepend->shouldReceive('addFile')->once()->with('/X/' . $fileC);
54
        $this->pdepend->shouldReceive('addFile')->once()->with('/X/' . $fileD);
55
56
        $this->pdepend->shouldReceive('addReportGenerator')->once()->with($this->adapter);
57
        $adapter = $this->adapter;
58
        $expectedMetrics = [
59
                $fileA => ['metricA' => 1, 'metricB' => 2],
60
                $fileC => ['metricA' => 3, 'metricB' => 4],
61
                $fileD => ['metricA' => 5, 'metricB' => 6],
62
            ];
63
        $this->pdepend->shouldReceive('analyze')->once()->andReturnUsing(
64
            function() use ($adapter, $expectedMetrics) {
65
                InstanceHelper::setProtectedProperty($adapter, 'sourceFileMetricsList', $expectedMetrics);
66
        });
67
68
        $actualMetrics = $this->adapter->mine(
69
            [
70
                $fileA,
71
                $fileB,
72
                $fileC,
73
                $fileD,
74
            ],
75
            '/X'
76
        );
77
78
        self::assertEquals($expectedMetrics, $actualMetrics);
79
    }
80
}
81