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 |
||
27 | class AttachmentDownloadResponseTest extends \PHPUnit_Framework_TestCase { |
||
28 | |||
29 | /** |
||
30 | * @dataProvider providesResponseData |
||
31 | * @param $content |
||
32 | * @param $filename |
||
33 | * @param $contentType |
||
34 | */ |
||
35 | public function testIt($content, $filename, $contentType) { |
||
36 | $resp = new AttachmentDownloadResponse($content, $filename, $contentType); |
||
37 | $headers = $resp->getHeaders(); |
||
38 | $this->assertEquals($content, $resp->render()); |
||
39 | $this->assertArrayHasKey('Content-Type', $headers); |
||
40 | $this->assertEquals($contentType, $headers['Content-Type']); |
||
41 | $this->assertArrayHasKey('Content-Disposition', $headers); |
||
42 | $pos = strpos($headers['Content-Disposition'], $filename); |
||
43 | $this->assertTrue($pos > 0); |
||
44 | } |
||
45 | |||
46 | public function providesResponseData() { |
||
47 | return [ |
||
48 | ['1234567890', 'test.txt', 'text/plain'] |
||
49 | ]; |
||
50 | } |
||
51 | } |
||
52 |