Completed
Pull Request — master (#3)
by Harry
03:17
created

MetadataFinderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 4
c 2
b 1
f 1
lcom 1
cbo 8
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testInstanceOf() 0 5 1
A testFindFilesWithNoFiltersWillReturnAllFiles() 0 16 1
B testFindFilesWithWithBasicFiltersWillReturnMatchingFilesOnly() 0 25 1
A testFindFilesReturningFalseWillNotIncludeTheFileInTheResults() 0 12 1
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