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