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 |
||
9 | class DeleteOldEntriesCommandTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | public function testHelpInfo() |
||
21 | |||
22 | View Code Duplication | public function testDeleteOldEntries_WithoutParams() |
|
23 | { |
||
24 | $application = new Application(); |
||
25 | $application->add(new DeleteOldEntriesCommand()); |
||
26 | $command = $this->getDeleteOldEntriesCommand($application); |
||
27 | |||
28 | $mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService")->disableOriginalConstructor()->getMock(); |
||
29 | self::$days = 60; |
||
30 | self::$count = 14; |
||
31 | self::$type = null; |
||
32 | $mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback'))); |
||
33 | |||
34 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock(); |
||
35 | $containerMock->expects($this->once())->method('get')->with('azine_mailgun.service')->will($this->returnValue($mailgunServiceMock)); |
||
36 | |||
37 | $command->setContainer($containerMock); |
||
38 | $tester = new CommandTester($command); |
||
39 | |||
40 | $tester->execute(array('')); |
||
41 | $display = $tester->getDisplay(); |
||
42 | $this->assertContains('deleting entries of any type.', $display); |
||
43 | $this->assertContains("using default age-limit of '60 days ago'.", $display); |
||
44 | $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display); |
||
45 | $this->assertContains('of any type have been deleted (14).', $display); |
||
46 | } |
||
47 | |||
48 | public static $days; |
||
49 | public static $count; |
||
50 | public static $type; |
||
51 | |||
52 | public static function removeEventsCallback($type, $date) |
||
53 | { |
||
54 | $checkDate = new \DateTime(self::$days.' days ago'); |
||
55 | self::assertSame(self::$type, $type, 'type null expected.'); |
||
56 | self::assertSame($checkDate->format('Y-m-d H:i'), $date->format('Y-m-d H:i'), 'wrong date.'); |
||
57 | |||
58 | return self::$count; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param Application $application |
||
63 | * |
||
64 | * @return DeleteOldEntriesCommand |
||
65 | */ |
||
66 | private function getDeleteOldEntriesCommand($application) |
||
70 | |||
71 | View Code Duplication | public function testDeleteOldEntries_WithDate() |
|
95 | |||
96 | public function testDeleteOldEntries_WithDateAndType() |
||
118 | |||
119 | /** |
||
120 | * @expectedException \InvalidArgumentException |
||
121 | */ |
||
122 | public function testDeleteOldEntries_WithInvalidType() |
||
135 | } |
||
136 |
This check looks for function calls that miss required arguments.