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 StreamLoaderTest extends AbstractTest |
||
| 12 | { |
||
| 13 | public function testThrowsIfInvalidPathGivenOnFind() |
||
| 14 | { |
||
| 15 | $loader = new StreamLoader('file://'); |
||
| 16 | |||
| 17 | $path = $this->tempDir.'/invalid.jpeg'; |
||
| 18 | |||
| 19 | $this->setExpectedException( |
||
| 20 | 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
||
| 21 | 'Source image file://'.$path.' not found.' |
||
| 22 | ); |
||
| 23 | |||
| 24 | $loader->find($path); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testReturnImageContentOnFind() |
||
| 28 | { |
||
| 29 | $expectedContent = file_get_contents($this->fixturesDir.'/assets/cats.jpeg'); |
||
| 30 | |||
| 31 | $loader = new StreamLoader('file://'); |
||
| 32 | |||
| 33 | $this->assertSame( |
||
| 34 | $expectedContent, |
||
| 35 | $loader->find($this->fixturesDir.'/assets/cats.jpeg') |
||
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testReturnImageContentWhenStreamContextProvidedOnFind() |
||
| 40 | { |
||
| 41 | $expectedContent = file_get_contents($this->fixturesDir.'/assets/cats.jpeg'); |
||
| 42 | |||
| 43 | $context = stream_context_create(); |
||
| 44 | |||
| 45 | $loader = new StreamLoader('file://', $context); |
||
| 46 | |||
| 47 | $this->assertSame( |
||
| 48 | $expectedContent, |
||
| 49 | $loader->find($this->fixturesDir.'/assets/cats.jpeg') |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testThrowsIfInvalidResourceGivenInConstructor() |
||
| 54 | { |
||
| 55 | $this->setExpectedException('InvalidArgumentException', 'The given context is no valid resource.'); |
||
| 56 | |||
| 57 | new StreamLoader('not valid resource', true); |
||
| 58 | } |
||
| 59 | } |
||
| 60 |