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 |
||
| 8 | class ProPlayerValidatorTest extends TestCase { |
||
| 9 | function setUp() { |
||
| 10 | $this->app = require dirname(__FILE__).'/../../../../api/config/_app.php'; |
||
| 11 | $draftDataRepository = new DraftDataRepository($this->app); |
||
| 12 | $this->sut = new ProPlayerValidator($this->app); |
||
| 13 | $this->app['phpdraft.DraftDataRepository'] = $draftDataRepository; |
||
| 14 | |||
| 15 | $this->uploadedFileClass = 'Symfony\Component\HttpFoundation\File\UploadedFile'; |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testEnsuresSportIsNotEmpty() { |
||
| 19 | $mockFile = $this->getMockBuilder($this->uploadedFileClass) |
||
| 20 | ->enableOriginalConstructor() |
||
| 21 | ->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
||
| 22 | ->getMock(); |
||
| 23 | |||
| 24 | $sport = ""; |
||
| 25 | |||
| 26 | $result = $this->sut->IsUploadSportValid($sport, $mockFile); |
||
| 27 | |||
| 28 | $this->assertFalse($result->success); |
||
| 29 | $this->assertContains("One or more missing fields.", $result->errors); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testDraftIdAndSecondsArePresent() { |
||
| 33 | $mockFile = $this->getMockBuilder($this->uploadedFileClass) |
||
| 34 | ->enableOriginalConstructor() |
||
| 35 | ->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
||
| 36 | ->getMock(); |
||
| 37 | |||
| 38 | $mockFile->expects($this->once()) |
||
| 39 | ->method('getError') |
||
| 40 | ->will($this->returnValue(1)); |
||
| 41 | |||
| 42 | $sport = "NFL"; |
||
| 43 | |||
| 44 | $result = $this->sut->IsUploadSportValid($sport, $mockFile); |
||
| 45 | |||
| 46 | $this->assertFalse($result->success); |
||
| 47 | $this->assertContains("Upload error - 1", $result->errors); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testValidateSportValues() { |
||
| 51 | $mockFile = $this->getMockBuilder($this->uploadedFileClass) |
||
| 52 | ->enableOriginalConstructor() |
||
| 53 | ->setConstructorArgs([tempnam(sys_get_temp_dir(), ''), 'dummy']) |
||
| 54 | ->getMock(); |
||
| 55 | |||
| 56 | $sport = "QVC"; |
||
| 57 | |||
| 58 | $result = $this->sut->IsUploadSportValid($sport, $mockFile); |
||
| 59 | |||
| 60 | $this->assertFalse($result->success); |
||
| 61 | $this->assertContains("Sport QVC is an invalid value.", $result->errors); |
||
| 62 | } |
||
| 63 | } |