1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Phorensic\Test\SharedKernel\Port\FileSystem\Adapter\FileSystem; |
4
|
|
|
|
5
|
|
|
use Hgraca\FileSystem\Exception\FileNotFoundException; |
6
|
|
|
use Hgraca\FileSystem\Exception\InvalidPathException; |
7
|
|
|
use Hgraca\FileSystem\FileSystemInterface; |
8
|
|
|
use Hgraca\Phorensic\SharedKernel\Port\FileSystem\Adapter\FileSystem\FileSystemAdapter; |
9
|
|
|
use Mockery; |
10
|
|
|
use Mockery\MockInterface; |
11
|
|
|
use PHPUnit_Framework_TestCase; |
12
|
|
|
|
13
|
|
|
final class FileSystemAdapterTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var MockInterface|FileSystemInterface |
17
|
|
|
*/ |
18
|
|
|
private $fileSystemMock; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var FileSystemAdapter |
22
|
|
|
*/ |
23
|
|
|
private $adapter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @before |
27
|
|
|
*/ |
28
|
|
|
public function setUpAdapter() |
29
|
|
|
{ |
30
|
|
|
$this->fileSystemMock = Mockery::mock(FileSystemInterface::class); |
31
|
|
|
$this->adapter = new FileSystemAdapter($this->fileSystemMock); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* |
37
|
|
|
* @small |
38
|
|
|
*/ |
39
|
|
|
public function readFile() |
40
|
|
|
{ |
41
|
|
|
$path = 'some/path/to/file'; |
42
|
|
|
$this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andReturn('some content'); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->adapter->readFile($path); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* |
50
|
|
|
* @small |
51
|
|
|
* |
52
|
|
|
* @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\InvalidPathException |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function readFile_ThrowsExceptionIfInvalidPath() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$path = 'some/path/to/file'; |
57
|
|
|
$this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andThrow(InvalidPathException::class); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->adapter->readFile($path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* |
65
|
|
|
* @small |
66
|
|
|
* |
67
|
|
|
* @expectedException \Hgraca\Phorensic\SharedKernel\Port\FileSystem\Exception\FileNotFoundException |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function readFile_ThrowsExceptionIfPathNotFound() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$path = 'some/path/to/file'; |
72
|
|
|
$this->fileSystemMock->shouldReceive('readFile')->once()->with($path)->andThrow(FileNotFoundException::class); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->adapter->readFile($path); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @dataProvider dataProvider_test_getExtension |
79
|
|
|
*/ |
80
|
|
|
public function test_getExtension(string $path, string $expectedExtension) |
81
|
|
|
{ |
82
|
|
|
$adapter = new FileSystemAdapter(); |
83
|
|
|
|
84
|
|
|
self::assertEquals($expectedExtension, $adapter->getExtension($path)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function dataProvider_test_getExtension() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['/a/dir/fileA', ''], |
91
|
|
|
['/a/dir/fileA.php', 'php'], |
92
|
|
|
['/a/dir/fileA.bladibla', 'bladibla'], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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: