|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Test\Unit\Finder; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\ArrayFilter\AllOfFilter; |
|
6
|
|
|
use Graze\ArrayFilter\ArrayFilterInterface; |
|
7
|
|
|
use Graze\DataFile\Finder\FileFinderInterface; |
|
8
|
|
|
use Graze\DataFile\Finder\MetadataFinder; |
|
9
|
|
|
use Graze\DataFile\Node\FileNode; |
|
10
|
|
|
use Graze\DataFile\Node\FileNodeCollection; |
|
11
|
|
|
use Graze\DataFile\Test\TestCase; |
|
12
|
|
|
use Hamcrest\Core\AllOf; |
|
13
|
|
|
use Mockery as m; |
|
14
|
|
|
|
|
15
|
|
|
class MetadataFinderTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testInstanceOf() |
|
18
|
|
|
{ |
|
19
|
|
|
$finder = new MetadataFinder(new AllOfFilter()); |
|
20
|
|
|
static::assertInstanceOf(FileFinderInterface::class, $finder); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testFindFilesWithNoFiltersWillReturnAllFiles() |
|
24
|
|
|
{ |
|
25
|
|
|
$file = m::mock(FileNode::class); |
|
26
|
|
|
$file->shouldReceive('getMetadata') |
|
27
|
|
|
->andReturn([ |
|
28
|
|
|
'name' => 'test', |
|
29
|
|
|
]); |
|
30
|
|
|
$collection = new FileNodeCollection(); |
|
31
|
|
|
$collection->add($file); |
|
32
|
|
|
|
|
33
|
|
|
$filter = m::mock(ArrayFilterInterface::class); |
|
34
|
|
|
$filter->shouldReceive('matches')->andReturn(true); |
|
35
|
|
|
$finder = new MetadataFinder($filter); |
|
36
|
|
|
|
|
37
|
|
|
static::assertEquals($collection->getAll(), $finder->findFiles($collection)->getAll()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testFindFilesWithWithBasicFiltersWillReturnMatchingFilesOnly() |
|
41
|
|
|
{ |
|
42
|
|
|
$file = m::mock(FileNode::class); |
|
43
|
|
|
$file->shouldReceive('getMetadata') |
|
44
|
|
|
->andReturn([ |
|
45
|
|
|
'name' => 'test', |
|
46
|
|
|
]); |
|
47
|
|
|
$file2 = m::mock(FileNode::class); |
|
48
|
|
|
$file2->shouldReceive('getMetadata') |
|
49
|
|
|
->andReturn([ |
|
50
|
|
|
'name' => 'test2', |
|
51
|
|
|
]); |
|
52
|
|
|
$collection = new FileNodeCollection(); |
|
53
|
|
|
$collection->add($file); |
|
54
|
|
|
$collection->add($file2); |
|
55
|
|
|
|
|
56
|
|
|
$filter = m::mock(ArrayFilterInterface::class); |
|
57
|
|
|
$filter->shouldReceive('matches')->with(['name' => 'test'])->andReturn(true); |
|
58
|
|
|
$filter->shouldReceive('matches')->with(['name' => 'test2'])->andReturn(false); |
|
59
|
|
|
$finder = new MetadataFinder($filter); |
|
60
|
|
|
|
|
61
|
|
|
$found = $finder->findFiles($collection); |
|
62
|
|
|
static::assertCount(1, $found->getAll()); |
|
63
|
|
|
static::assertEquals([$file], $found->getAll()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testFindFilesReturningFalseWillNotIncludeTheFileInTheResults() |
|
67
|
|
|
{ |
|
68
|
|
|
$file = m::mock(FileNode::class); |
|
69
|
|
|
$file->shouldReceive('getMetadata') |
|
70
|
|
|
->andReturn(false); |
|
71
|
|
|
$collection = new FileNodeCollection(); |
|
72
|
|
|
$collection->add($file); |
|
73
|
|
|
|
|
74
|
|
|
$finder = new MetadataFinder(new AllOfFilter()); |
|
75
|
|
|
|
|
76
|
|
|
static::assertEquals(0, $finder->findFiles($collection)->count()); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|