Completed
Pull Request — master (#5)
by Arnaud
09:37
created

FileIndexerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 56.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 75
loc 132
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JK\SamBundle\Tests\Indexer;
4
5
use Exception;
6
use JK\SamBundle\Watcher\Indexer\FileIndexer;
7
use PHPUnit\Framework\TestCase;
8
use SplFileInfo;
9
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10
use Symfony\Component\Finder\Finder;
11
12
class FileIndexerTest extends WebTestCase
13
{
14
    /**
15
     * An invalid directory should throw an Exception.
16
     */
17
    public function testIndexException()
18
    {
19
        $indexer = new FileIndexer();
20
        $exceptionRaised = false;
21
22
        try {
23
            $indexer->index([
24
                __DIR__.'/assets'
25
            ]);
26
        } catch (Exception $exception) {
27
            $exceptionRaised = true;
28
        }
29
        $this->assertTrue($exceptionRaised);
30
    }
31
32
    /**
33
     * The files in the given directory to index should be added to the index during indexing.
34
     */
35
    public function testIndex()
36
    {
37
        $indexer = new FileIndexer();
38
        $indexer->index([
39
            $this->getAssetsDirectory()
40
        ]);
41
        $finder = new Finder();
42
        $finder
43
            ->files()
44
            ->in($this->getAssetsDirectory())
45
        ;
46
47
        foreach ($finder as $fileInfo) {
48
            $this->assertTrue($indexer->has($fileInfo->getRealPath()));
49
            $this->assertInstanceOf(SplFileInfo::class, $indexer->get($fileInfo->getRealPath()));
50
        }
51
    }
52
53
    /**
54
     * The files in the given directory to index should be added to the index during indexing.
55
     */
56
    public function testIndexFilteredByExtensions()
57
    {
58
        $indexer = new FileIndexer();
59
        $indexer->index([
60
            $this->getAssetsDirectory()
61
        ], [
62
            'txt'
63
        ]);
64
        $finder = new Finder();
65
        $finder
66
            ->files()
67
            ->in($this->getAssetsDirectory())
68
            ->name('*.txt')
69
        ;
70
71
        foreach ($finder as $fileInfo) {
72
            $this->assertTrue($indexer->has($fileInfo->getRealPath()));
73
            $this->assertInstanceOf(SplFileInfo::class, $indexer->get($fileInfo->getRealPath()));
74
        }
75
    }
76
77
    /**
78
     * Adding a invalid file should throw an exception.
79
     */
80
    public function testAddException()
81
    {
82
        $indexer = new FileIndexer();
83
        $exceptionRaised = false;
84
85
        try {
86
            $indexer->add(new SplFileInfo('/some_path/is_wrong'));
87
        } catch (Exception $exception) {
88
            $exceptionRaised = true;
89
        }
90
        $this->assertTrue($exceptionRaised);
91
    }
92
93
    /**
94
     * Adding a invalid file should throw an exception.
95
     */
96
    public function testGetException()
97
    {
98
        $indexer = new FileIndexer();
99
        $exceptionRaised = false;
100
101
        try {
102
            $indexer->get('/some_path/is_wrong');
103
        } catch (Exception $exception) {
104
            $exceptionRaised = true;
105
        }
106
        $this->assertTrue($exceptionRaised);
107
    }
108
109
    /**
110
     * The reindex should only put in the change set the modified files.
111
     */
112
    public function testReIndex()
113
    {
114
        $modifiedFile = $this->getAssetsDirectory().'/test.scss';
115
        $indexer = new FileIndexer();
116
117
        // index for the first time
118
        $indexer->index([
119
            $this->getAssetsDirectory()
120
        ], [
121
            'scss'
122
        ]);
123
124
        // modify a file
125
        touch($modifiedFile);
126
127
        // reindex
128
        $indexer->index([
129
            $this->getAssetsDirectory()
130
        ]);
131
132
        // one file must be found in the change set
133
        $this->assertCount(1, $indexer->getChangedEntries());
134
        $this->assertTrue($indexer->hasChangedEntries());
135
        $this->assertEquals($modifiedFile, $indexer->get($modifiedFile)->getRealPath());
136
137
    }
138
139
    protected function getAssetsDirectory()
140
    {
141
        return realpath(__DIR__.'/../fixtures/assets');
142
    }
143
}
144