|
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') |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
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') |
|
|
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: