1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Test\Unit\Finder; |
15
|
|
|
|
16
|
|
|
use Graze\ArrayFilter\AllOfFilter; |
17
|
|
|
use Graze\ArrayFilter\ArrayFilterInterface; |
18
|
|
|
use Graze\DataFile\Finder\FileFinderInterface; |
19
|
|
|
use Graze\DataFile\Finder\MetadataFinder; |
20
|
|
|
use Graze\DataFile\Node\FileNode; |
21
|
|
|
use Graze\DataFile\Node\FileNodeCollection; |
22
|
|
|
use Graze\DataFile\Test\TestCase; |
23
|
|
|
use Mockery as m; |
24
|
|
|
|
25
|
|
|
class MetadataFinderTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testInstanceOf() |
28
|
|
|
{ |
29
|
|
|
$finder = new MetadataFinder(new AllOfFilter()); |
30
|
|
|
static::assertInstanceOf(FileFinderInterface::class, $finder); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testFindFilesWithNoFiltersWillReturnAllFiles() |
34
|
|
|
{ |
35
|
|
|
$file = m::mock(FileNode::class); |
36
|
|
|
$file->shouldReceive('getMetadata') |
37
|
|
|
->andReturn([ |
38
|
|
|
'name' => 'test', |
39
|
|
|
]); |
40
|
|
|
$collection = new FileNodeCollection(); |
41
|
|
|
$collection->add($file); |
42
|
|
|
|
43
|
|
|
$filter = m::mock(ArrayFilterInterface::class); |
44
|
|
|
$filter->shouldReceive('matches')->andReturn(true); |
45
|
|
|
$finder = new MetadataFinder($filter); |
46
|
|
|
|
47
|
|
|
static::assertEquals($collection->getAll(), $finder->findFiles($collection)->getAll()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testFindFilesWithWithBasicFiltersWillReturnMatchingFilesOnly() |
51
|
|
|
{ |
52
|
|
|
$file = m::mock(FileNode::class); |
53
|
|
|
$file->shouldReceive('getMetadata') |
54
|
|
|
->andReturn([ |
55
|
|
|
'name' => 'test', |
56
|
|
|
]); |
57
|
|
|
$file2 = m::mock(FileNode::class); |
58
|
|
|
$file2->shouldReceive('getMetadata') |
59
|
|
|
->andReturn([ |
60
|
|
|
'name' => 'test2', |
61
|
|
|
]); |
62
|
|
|
$collection = new FileNodeCollection(); |
63
|
|
|
$collection->add($file); |
64
|
|
|
$collection->add($file2); |
65
|
|
|
|
66
|
|
|
$filter = m::mock(ArrayFilterInterface::class); |
67
|
|
|
$filter->shouldReceive('matches')->with(['name' => 'test'])->andReturn(true); |
68
|
|
|
$filter->shouldReceive('matches')->with(['name' => 'test2'])->andReturn(false); |
69
|
|
|
$finder = new MetadataFinder($filter); |
70
|
|
|
|
71
|
|
|
$found = $finder->findFiles($collection); |
72
|
|
|
static::assertCount(1, $found->getAll()); |
73
|
|
|
static::assertEquals([$file], $found->getAll()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testFindFilesReturningFalseWillNotIncludeTheFileInTheResults() |
77
|
|
|
{ |
78
|
|
|
$file = m::mock(FileNode::class); |
79
|
|
|
$file->shouldReceive('getMetadata') |
80
|
|
|
->andReturn(false); |
81
|
|
|
$collection = new FileNodeCollection(); |
82
|
|
|
$collection->add($file); |
83
|
|
|
|
84
|
|
|
$finder = new MetadataFinder(new AllOfFilter()); |
85
|
|
|
|
86
|
|
|
static::assertEquals(0, $finder->findFiles($collection)->count()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|