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 |
||
| 19 | class JobManagerTest extends BaseJobManagerTest |
||
| 20 | { |
||
| 21 | use AutoRetryTrait; |
||
| 22 | use RetryableTrait; |
||
| 23 | public static $beanstalkd; |
||
| 24 | |||
| 25 | public static function setUpBeforeClass() |
||
| 26 | { |
||
| 27 | $host = getenv('BEANSTALKD_HOST'); |
||
| 28 | $className = 'Dtc\QueueBundle\Beanstalkd\Job'; |
||
| 29 | $jobTimingClass = 'Dtc\QueueBundle\Model\JobTiming'; |
||
| 30 | $runClass = 'Dtc\QueueBundle\Model\Run'; |
||
| 31 | |||
| 32 | self::$beanstalkd = new Pheanstalk($host); |
||
| 33 | self::$jobTimingManager = new JobTimingManager($jobTimingClass, false); |
||
| 34 | self::$runManager = new RunManager($runClass); |
||
| 35 | self::$jobManager = new JobManager(self::$runManager, self::$jobTimingManager, $className); |
||
| 36 | self::$jobManager->setBeanstalkd(self::$beanstalkd); |
||
| 37 | self::$worker = new FibonacciWorker(); |
||
| 38 | |||
| 39 | $drained = 0; |
||
| 40 | do { |
||
| 41 | $beanJob = self::$beanstalkd->reserve(1); |
||
| 42 | if ($beanJob) { |
||
| 43 | self::$beanstalkd->delete($beanJob); |
||
| 44 | ++$drained; |
||
| 45 | } |
||
| 46 | } while ($beanJob); |
||
| 47 | |||
| 48 | if ($drained) { |
||
| 49 | echo "\nbeanstalkd: drained $drained prior to test\n"; |
||
| 50 | } |
||
| 51 | parent::setUpBeforeClass(); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testGetJobByWorker() |
||
| 55 | { |
||
| 56 | $failed = false; |
||
| 57 | try { |
||
| 58 | self::$jobManager->getJob(self::$worker->getName()); |
||
| 59 | $failed = true; |
||
| 60 | } catch (\Exception $exception) { |
||
| 61 | self::assertTrue(true); |
||
| 62 | } |
||
| 63 | self::assertFalse($failed); |
||
| 64 | } |
||
| 65 | } |
||
| 66 |