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 |
||
10 | class ScheduleTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | public function testSimple() |
||
13 | { |
||
14 | $schedule = new Schedule(['value' => '@daily']); |
||
15 | |||
16 | $this->assertEquals('0 0 * * *', $schedule->runEvery); |
||
17 | $this->assertTrue($schedule->active); |
||
18 | } |
||
19 | |||
20 | public function testInterval() |
||
21 | { |
||
22 | $schedule = new Schedule(['runEvery' => '@daily']); |
||
23 | |||
24 | $this->assertEquals('0 0 * * *', $schedule->runEvery); |
||
25 | $this->assertTrue($schedule->active); |
||
26 | } |
||
27 | |||
28 | public function testIntervalActiveParams() |
||
29 | { |
||
30 | $schedule = new Schedule([ |
||
31 | 'runEvery' => '@daily', |
||
32 | 'active' => false, |
||
33 | 'params' => [1], |
||
34 | ]); |
||
35 | |||
36 | $this->assertEquals('0 0 * * *', $schedule->runEvery); |
||
37 | $this->assertEquals([1], $schedule->params); |
||
38 | $this->assertFalse($schedule->active); |
||
39 | } |
||
40 | |||
41 | public function testParamsNonArray() |
||
42 | { |
||
43 | $this->setExpectedException('\InvalidArgumentException'); |
||
44 | |||
45 | new Schedule([ |
||
46 | 'interval' => '@daily', |
||
47 | 'active' => false, |
||
48 | 'params' => 'foo', |
||
49 | ]); |
||
50 | } |
||
51 | |||
52 | public function testInvalidProperty() |
||
53 | { |
||
54 | $this->setExpectedException('\InvalidArgumentException'); |
||
55 | |||
56 | new Schedule(['fooo' => '@daily']); |
||
57 | } |
||
58 | |||
59 | public function testInvalidInterval() |
||
60 | { |
||
61 | $this->setExpectedException('\InvalidArgumentException'); |
||
62 | |||
63 | new Schedule(['runEvery' => 'fdsfds fds']); |
||
64 | } |
||
65 | |||
66 | public function testMissingInvalid() |
||
67 | { |
||
68 | $this->setExpectedException('\InvalidArgumentException'); |
||
69 | |||
70 | new Schedule([]); |
||
71 | } |
||
72 | |||
73 | public function testInvalidTimeout() |
||
74 | { |
||
75 | $this->setExpectedException('\InvalidArgumentException'); |
||
76 | |||
77 | new Schedule(['interval' => '@daily', 'timeout' => 'x']); |
||
78 | } |
||
79 | } |
||
80 |