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 |
||
| 7 | class ActionScheduler_CronSchedule_Test extends ActionScheduler_UnitTestCase { |
||
| 8 | public function test_creation() { |
||
| 9 | $time = as_get_datetime_object('tomorrow'); |
||
| 10 | $cron = CronExpression::factory('@daily'); |
||
| 11 | $schedule = new ActionScheduler_CronSchedule(as_get_datetime_object(), $cron); |
||
| 12 | $this->assertEquals( $time, $schedule->next() ); |
||
| 13 | } |
||
| 14 | |||
| 15 | public function test_next() { |
||
| 16 | $time = as_get_datetime_object('2013-06-14'); |
||
| 17 | $cron = CronExpression::factory('@daily'); |
||
| 18 | $schedule = new ActionScheduler_CronSchedule($time, $cron); |
||
| 19 | $this->assertEquals( as_get_datetime_object('tomorrow'), $schedule->next( as_get_datetime_object() ) ); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function test_is_recurring() { |
||
| 23 | $schedule = new ActionScheduler_CronSchedule(as_get_datetime_object('2013-06-14'), CronExpression::factory('@daily')); |
||
| 24 | $this->assertTrue( $schedule->is_recurring() ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function test_cron_format() { |
||
| 28 | $time = as_get_datetime_object('2014-01-01'); |
||
| 29 | $cron = CronExpression::factory('0 0 10 10 *'); |
||
| 30 | $schedule = new ActionScheduler_CronSchedule($time, $cron); |
||
| 31 | $this->assertEquals( as_get_datetime_object('2014-10-10'), $schedule->next() ); |
||
| 32 | |||
| 33 | $cron = CronExpression::factory('0 0 L 1/2 *'); |
||
| 34 | $schedule = new ActionScheduler_CronSchedule($time, $cron); |
||
| 35 | $this->assertEquals( as_get_datetime_object('2014-01-31'), $schedule->next() ); |
||
| 36 | $this->assertEquals( as_get_datetime_object('2014-07-31'), $schedule->next( as_get_datetime_object('2014-06-01') ) ); |
||
| 37 | $this->assertEquals( as_get_datetime_object('2028-11-30'), $schedule->next( as_get_datetime_object('2028-11-01') ) ); |
||
| 38 | |||
| 39 | $cron = CronExpression::factory('30 14 * * MON#3 *'); |
||
| 40 | $schedule = new ActionScheduler_CronSchedule($time, $cron); |
||
| 41 | $this->assertEquals( as_get_datetime_object('2014-01-20 14:30:00'), $schedule->next() ); |
||
| 42 | $this->assertEquals( as_get_datetime_object('2014-05-19 14:30:00'), $schedule->next( as_get_datetime_object('2014-05-01') ) ); |
||
| 43 | } |
||
| 44 | } |
||
| 45 |