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 |
||
| 32 | class MergeFilesTest extends AbstractFileTestCase |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var BuilderInterface|m\MockInterface |
||
| 36 | */ |
||
| 37 | protected $builder; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MergeFiles |
||
| 41 | */ |
||
| 42 | private $merge; |
||
| 43 | |||
| 44 | public function setUp() |
||
| 45 | { |
||
| 46 | $this->builder = m::mock(Builder::class)->makePartial(); |
||
| 47 | $this->merge = new MergeFiles(); |
||
| 48 | $this->merge->setBuilder($this->builder); |
||
|
|
|||
| 49 | } |
||
| 50 | |||
| 51 | public function testInstanceOf() |
||
| 55 | |||
| 56 | public function testCanContractAcceptsFileNodeCollectionInterface() |
||
| 57 | { |
||
| 58 | $collection = m::mock(FileNodeCollectionInterface::class); |
||
| 59 | $collection->shouldReceive('getIterator') |
||
| 60 | ->andReturn([]); |
||
| 61 | |||
| 62 | $node = m::mock(LocalFile::class); |
||
| 63 | static::assertTrue($this->merge->canContract($collection, $node)); |
||
| 64 | } |
||
| 65 | |||
| 66 | View Code Duplication | public function testCanContractOnlyAcceptsLocalFiles() |
|
| 67 | { |
||
| 68 | $collection = new FileNodeCollection(); |
||
| 69 | $file1 = m::mock(LocalFile::class); |
||
| 70 | $file1->shouldReceive('exists') |
||
| 71 | ->andReturn(true); |
||
| 72 | $file1->shouldReceive('getCompression') |
||
| 73 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 74 | $collection->add($file1); |
||
| 75 | |||
| 76 | $out = m::mock(LocalFile::class); |
||
| 77 | |||
| 78 | static::assertTrue($this->merge->canContract($collection, $out)); |
||
| 79 | |||
| 80 | $file2 = m::mock(FileNodeInterface::class); |
||
| 81 | $file2->shouldReceive('getCompression') |
||
| 82 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 83 | $collection->add($file2); |
||
| 84 | |||
| 85 | static::assertFalse($this->merge->canContract($collection, $out)); |
||
| 86 | } |
||
| 87 | |||
| 88 | View Code Duplication | public function testCanContractOnlyWithFilesThatExist() |
|
| 89 | { |
||
| 90 | $collection = new FileNodeCollection(); |
||
| 91 | $file1 = m::mock(LocalFile::class); |
||
| 92 | $file1->shouldReceive('exists') |
||
| 93 | ->andReturn(true); |
||
| 94 | $file1->shouldReceive('getCompression') |
||
| 95 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 96 | $collection->add($file1); |
||
| 97 | |||
| 98 | $out = m::mock(LocalFile::class); |
||
| 99 | |||
| 100 | static::assertTrue($this->merge->canContract($collection, $out)); |
||
| 101 | |||
| 102 | $file2 = m::mock(LocalFile::class); |
||
| 103 | $file2->shouldReceive('exists') |
||
| 104 | ->andReturn(false); |
||
| 105 | $file2->shouldReceive('getCompression') |
||
| 106 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 107 | $collection->add($file2); |
||
| 108 | |||
| 109 | static::assertFalse($this->merge->canContract($collection, $out)); |
||
| 110 | } |
||
| 111 | |||
| 112 | View Code Duplication | public function testCanContractOnlyUncompressedFiles() |
|
| 113 | { |
||
| 114 | $collection = new FileNodeCollection(); |
||
| 115 | $file1 = m::mock(LocalFile::class); |
||
| 116 | $file1->shouldReceive('exists') |
||
| 117 | ->andReturn(true); |
||
| 118 | $file1->shouldReceive('getCompression') |
||
| 119 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 120 | $collection->add($file1); |
||
| 121 | |||
| 122 | $out = m::mock(LocalFile::class); |
||
| 123 | |||
| 124 | static::assertTrue($this->merge->canContract($collection, $out)); |
||
| 125 | |||
| 126 | $file2 = m::mock(LocalFile::class); |
||
| 127 | $file2->shouldReceive('exists') |
||
| 128 | ->andReturn(true); |
||
| 129 | $file2->shouldReceive('getCompression') |
||
| 130 | ->andReturn(Gzip::NAME); |
||
| 131 | $collection->add($file2); |
||
| 132 | |||
| 133 | static::assertFalse($this->merge->canContract($collection, $out)); |
||
| 134 | } |
||
| 135 | |||
| 136 | View Code Duplication | public function testCallingContractWithAFileThatCannotBeContractedWillThrowAnException() |
|
| 137 | { |
||
| 138 | $collection = new FileNodeCollection(); |
||
| 139 | $file = m::mock(LocalFile::class); |
||
| 140 | $file->shouldReceive('exists') |
||
| 141 | ->andReturn(false); |
||
| 142 | $file->shouldReceive('getCompression') |
||
| 143 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 144 | $collection->add($file); |
||
| 145 | |||
| 146 | $target = m::mock(LocalFile::class); |
||
| 147 | |||
| 148 | $this->expectException(InvalidArgumentException::class); |
||
| 149 | |||
| 150 | $this->merge->contract($collection, $target); |
||
| 151 | } |
||
| 152 | |||
| 153 | View Code Duplication | public function testCallingContractWithANonLocalTargetWillThrowAnException() |
|
| 154 | { |
||
| 155 | $collection = new FileNodeCollection(); |
||
| 156 | $file = m::mock(LocalFile::class); |
||
| 157 | $file->shouldReceive('exists') |
||
| 158 | ->andReturn(true); |
||
| 159 | $file->shouldReceive('getCompression') |
||
| 160 | ->andReturn(CompressionFactory::TYPE_NONE); |
||
| 161 | $collection->add($file); |
||
| 162 | |||
| 163 | $target = m::mock(FileNodeInterface::class); |
||
| 164 | |||
| 165 | static::assertFalse($this->merge->canContract($collection, $target)); |
||
| 166 | |||
| 167 | $this->expectException(InvalidArgumentException::class); |
||
| 168 | |||
| 169 | $this->merge->contract($collection, $target); |
||
| 170 | } |
||
| 171 | |||
| 172 | View Code Duplication | public function testSimpleMergeFiles() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $rootDir |
||
| 205 | * @param int $numFiles |
||
| 206 | * |
||
| 207 | * @return FileNodeCollectionInterface |
||
| 208 | */ |
||
| 209 | private function createCollection($rootDir, $numFiles) |
||
| 221 | |||
| 222 | View Code Duplication | public function testCallingMergeWithKeepOldFilesAsFalseDeletesAllTheFilesInTheCollection() |
|
| 223 | { |
||
| 224 | $collection = $this->createCollection('simple.merge.delete/', 3); |
||
| 225 | |||
| 226 | $outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output'); |
||
| 227 | |||
| 228 | $file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]); |
||
| 229 | |||
| 230 | static::assertSame($file, $outputFile); |
||
| 231 | static::assertEquals( |
||
| 232 | [ |
||
| 233 | "File 1 Line 1", |
||
| 234 | "File 1 Line 2", |
||
| 235 | "File 1 Line 3", |
||
| 236 | "File 2 Line 1", |
||
| 237 | "File 2 Line 2", |
||
| 238 | "File 2 Line 3", |
||
| 239 | "File 3 Line 1", |
||
| 240 | "File 3 Line 2", |
||
| 241 | "File 3 Line 3", |
||
| 242 | ], |
||
| 243 | $file->getContents() |
||
| 244 | ); |
||
| 245 | |||
| 246 | $exists = $collection->filter(function (FileNodeInterface $item) { |
||
| 247 | return $item->exists(); |
||
| 248 | }); |
||
| 249 | |||
| 250 | static::assertCount(0, $exists); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function testProcessFailedThrowException() |
||
| 254 | { |
||
| 255 | $process = m::mock('Symfony\Component\Process\Process')->makePartial(); |
||
| 256 | $this->builder->shouldReceive('build') |
||
| 257 | ->andReturn($process); |
||
| 258 | |||
| 259 | $process->shouldReceive('isSuccessful')->andReturn(false); |
||
| 260 | |||
| 261 | // set exception as no guarantee process will run on local system |
||
| 262 | $this->expectException(ProcessFailedException::class); |
||
| 263 | |||
| 264 | $collection = $this->createCollection('simple.merge/', 3); |
||
| 265 | |||
| 266 | $outputFile = new LocalFile(static::$dir . 'simple.merge.output'); |
||
| 267 | |||
| 268 | $this->merge->contract($collection, $outputFile); |
||
| 269 | } |
||
| 270 | |||
| 271 | View Code Duplication | public function testCallingContractWillPassThroughOptions() |
|
| 305 | |||
| 306 | public function testDeleteOldFilesWillDeleteAnyEmptyDirectories() |
||
| 339 | } |
||
| 340 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: