FilesRepositoryTest::storePhpFilesMetrics()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace Hgraca\Phorensic\Test\SharedKernel\Repository;
4
5
use Hgraca\Phorensic\SharedKernel\Port\Database\DatabaseClientInterface;
6
use Hgraca\Phorensic\SharedKernel\Repository\FilesRepository;
7
use Mockery;
8
use Mockery\MockInterface;
9
use PHPUnit_Framework_TestCase;
10
11
final class FilesRepositoryTest extends PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var MockInterface|DatabaseClientInterface
15
     */
16
    private $databaseClientMock;
17
18
    /**
19
     * @var FilesRepository
20
     */
21
    private $repository;
22
23
    /**
24
     * @before
25
     */
26
    public function setUpAdapter()
27
    {
28
        $this->databaseClientMock = Mockery::mock(DatabaseClientInterface::class);
29
        $this->repository = new FilesRepository($this->databaseClientMock);
30
    }
31
32
    /**
33
     * @test
34
     *
35
     * @small
36
     */
37
    public function storeFilesChangeRate()
38
    {
39
        $data = [
40
            ['a/path/to/file.php', 'c', 'e'],
41
            ['some/path/to/file.txt', 'c', 'e'],
42
            ['another/path/to/file.bladibla', 'c', 'e'],
43
            ['another/path/to/file', 'c', 'e'],
44
        ];
45
        $expected = [
46
            'a/path/to/file.php' => [FilesRepository::TBL_FILES_COL_PATH => 'a/path/to/file.php', FilesRepository::TBL_FILES_COL_TYPE => 'php', FilesRepository::TBL_FILES_COL_COMMITS => 'c', FilesRepository::TBL_FILES_COL_ACTIVE_DAYS => 'e'],
47
            'some/path/to/file.txt' => [FilesRepository::TBL_FILES_COL_PATH => 'some/path/to/file.txt', FilesRepository::TBL_FILES_COL_TYPE => 'txt', FilesRepository::TBL_FILES_COL_COMMITS => 'c', FilesRepository::TBL_FILES_COL_ACTIVE_DAYS => 'e'],
48
            'another/path/to/file.bladibla' => [FilesRepository::TBL_FILES_COL_PATH => 'another/path/to/file.bladibla', FilesRepository::TBL_FILES_COL_TYPE => 'bladibla', FilesRepository::TBL_FILES_COL_COMMITS => 'c', FilesRepository::TBL_FILES_COL_ACTIVE_DAYS => 'e'],
49
            'another/path/to/file' => [FilesRepository::TBL_FILES_COL_PATH => 'another/path/to/file', FilesRepository::TBL_FILES_COL_TYPE => '', FilesRepository::TBL_FILES_COL_COMMITS => 'c', FilesRepository::TBL_FILES_COL_ACTIVE_DAYS => 'e'],
50
        ];
51
        $this->databaseClientMock->shouldReceive('create')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\Phorensic\SharedK...DatabaseClientInterface.

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
            ->once()
53
            ->with(FilesRepository::TBL_FILES, $expected);
54
55
        $this->repository->storeFilesChangeRate($data);
56
    }
57
58
    /**
59
     * @test
60
     *
61
     * @small
62
     */
63
    public function storePhpFilesMetrics()
64
    {
65
        $data = [
66
            'a' => ['b'],
67
            'c' => ['d'],
68
            'e' => ['f'],
69
        ];
70
        foreach ($data as $path => $criteria) {
71
            $this->databaseClientMock->shouldReceive('update')->once()->with(
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\Phorensic\SharedK...DatabaseClientInterface.

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...
72
                FilesRepository::TBL_FILES,
73
                $criteria,
74
                ['path' => $path]
75
            );
76
        }
77
78
        $this->repository->storePhpFilesMetrics($data);
79
    }
80
81
    /**
82
     * @test
83
     *
84
     * @small
85
     */
86
    public function findPhpFiles()
87
    {
88
        $data = [
89
            [FilesRepository::TBL_FILES_COL_PATH => 'a', FilesRepository::TBL_FILES_COL_TYPE => 'b',],
90
            [FilesRepository::TBL_FILES_COL_PATH => 'c', FilesRepository::TBL_FILES_COL_TYPE => 'd',],
91
            [FilesRepository::TBL_FILES_COL_PATH => 'e', FilesRepository::TBL_FILES_COL_TYPE => 'f',],
92
        ];
93
        $expectedResult = ['a', 'c', 'e'];
94
95
        $this->databaseClientMock->shouldReceive('read')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Hgraca\Phorensic\SharedK...DatabaseClientInterface.

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...
96
            ->once()
97
            ->with(
98
                FilesRepository::TBL_FILES,
99
                [FilesRepository::TBL_FILES_COL_TYPE => 'php'],
100
                [],
101
                null
102
            )
103
            ->andReturn($data);
104
105
        self::assertEquals($expectedResult, $this->repository->findPhpFiles());
106
    }
107
}
108